Code Red > .NET
SymbolUtilityServices.GetBlockModelSpaceId
Kerry:
From my Saturday reading ..
public sealed class SymbolUtilityServices
Name: Autodesk.AutoCAD.DatabaseServices.SymbolUtilityServices
Assembly: acdbmgd, Version=17.1.0.0
has a Method
public static ObjectId GetBlockModelSpaceId(Database databasePointer);
In the past , to open Modelspace for Write, I've used something like the following, in one of several variations
--- Code: --- ObjectId objId = new ObjectId();
Database Db = HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager Tm = Db.TransactionManager;
using (Transaction tr = Tm.StartTransaction())
{
BlockTable tb = (BlockTable)Tm.GetObject
(Db.BlockTableId, OpenMode.ForRead, false);
objId = ((BlockTableRecord)Tm.GetObject
(tb[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false)).AppendEntity(ent);
Tm.AddNewlyCreatedDBObject(entToWriteToDataBase, true);
tr.Commit();
}
--- End code ---
however, using SymbolUtilityServices.GetBlockModelSpaceId seems a little easier ..
--- Code: --- ObjectId objId = new ObjectId();
Database Db = HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager Tm = Db.TransactionManager;
using (Transaction tr = Tm.StartTransaction())
{
objId = ((BlockTableRecord)Tm.GetObject(AcDb.SymbolUtilityServices.GetBlockModelSpaceId(Db),
OpenMode.ForWrite, false)).AppendEntity(ent);
Tm.AddNewlyCreatedDBObject(ent, true);
tr.Commit();
}
--- End code ---
edit for fatFingerSpelling
Kerry:
So, an Entity extension Class ( similar to previously posted by Daniel M )
--- Code: ---// CodeHimBelongaKwb © Dec 2007
// D:\Development\Visual Studio 2008\CAD Projects\Sat1215\Sat1215\EntityExtensions.cs
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
namespace Sat1215
{
public static partial class EntityExtensions
{
internal static ObjectId AddToModelSpace(this Entity ent)
{
ObjectId objId = new ObjectId();
Database Db = HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager Tm = Db.TransactionManager;
using (Transaction tr = Tm.StartTransaction())
{
objId = ((BlockTableRecord)Tm.GetObject(AcDb.SymbolUtilityServices.GetBlockModelSpaceId(Db),
OpenMode.ForWrite, false)).AppendEntity(ent);
Tm.AddNewlyCreatedDBObject(ent, true);
tr.Commit();
}
return objId;
}
internal static ObjectId AddToModelSpace(this Entity ent , Database Db)
{
ObjectId objId = new ObjectId();
AcDb.TransactionManager Tm = Db.TransactionManager;
using (Transaction tr = Tm.StartTransaction())
{
objId = ((BlockTableRecord)Tm.GetObject( AcDb.SymbolUtilityServices.GetBlockModelSpaceId(Db),
OpenMode.ForWrite, false)).AppendEntity(ent);
Tm.AddNewlyCreatedDBObject(ent, true);
tr.Commit();
}
return objId;
}
}
}
--- End code ---
and to Test it ....
--- Code: ---// CodeHimBelongaKwb © Dec 2007
// D:\Development\Visual Studio 2008\CAD Projects\Sat1215\Sat1215\Commands.cs
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
[assembly: CommandClass(typeof(Sat1215.Commands))]
namespace Sat1215
{
class Commands
{
[CommandMethod("test")]
static public void cmdtest()
{
Line ln = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0));
ln.AddToModelSpace();
Database Db = HostApplicationServices.WorkingDatabase;
ln = new Line(new Point3d(100, 100, 0), new Point3d(100, 200, 0));
ln.AddToModelSpace(Db);
}
}
}
--- End code ---
The partial classes are irrelevant to the use of the Method, I'm just playing with them here..
MP:
I'll be getting back into C# down the road, so I'm thanking you, Daniel ... in advance, for surely this stuff will be useful to me.
Kerry:
and a piccy of some of the OTHER Methods and Properties available
Kerry:
Though, thinking of extension classes ...
This is pretty efficient AND cool ..
--- Code: --- internal static ObjectId ModelSpaceId(this Database Db)
{
return SymbolUtilityServices.GetBlockModelSpaceId(Db);
}
--- End code ---
Piccy Added ..
Navigation
[0] Message Index
[#] Next page
Go to full version