So, an Entity extension Class ( similar to previously posted by Daniel M )
// 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;
}
}
}
and to Test it ....
// 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);
}
}
}
The partial classes are irrelevant to the use of the Method, I'm just playing with them here..