using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.ApplicationServices; using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application; namespace ClassLibrary { public class PrmrClass { [CommandMethod("cdb", CommandFlags.Session)] public void cdb() { CreateNewDBTest(@"C:\foo.dwg"); } public void CreateNewDBTest(string filePath) { Database ndb = new Database(); ndb.SaveAs(filePath, DwgVersion.Current); ndb.Dispose(); Application.DocumentManager.Open(filePath, false); Database db = Application.DocumentManager.MdiActiveDocument.Database; Point3d startpt = new Point3d(4.0, 2.0, 0.0); Point3d endpt = new Point3d(10.0, 7.0, 0.0); Line line = new Line(startpt, endpt); Document doc = AcadApp.DocumentManager.MdiActiveDocument; DocumentLock docLock = doc.LockDocument(); Transaction trans = doc.TransactionManager.StartTransaction(); using (docLock) using (trans) { BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); ObjectId blkRecId = bt[BlockTableRecord.ModelSpace]; BlockTableRecord btr = (BlockTableRecord)trans.GetObject(blkRecId, OpenMode.ForWrite); btr.AppendEntity(line); trans.AddNewlyCreatedDBObject(line, true); trans.Commit(); } AcadApp.DocumentManager.MdiActiveDocument.SendStringToExecute ("_Zoom _E ", false, true, false); } } }