- public static ObjectId ToSpace(this Entity ent, Database db = null, string space = null)
- {
- db = db ?? Application.DocumentManager.MdiActiveDocument.Database;
- var id = ObjectId.Null;
- using (var trans = db.TransactionManager.StartTransaction())
- {
- var blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- var mdlSpc = trans.GetObject(blkTbl[space ?? BlockTableRecord.ModelSpace],
- OpenMode.ForWrite) as BlockTableRecord;
- id = mdlSpc.AppendEntity(ent);
-
- trans.AddNewlyCreatedDBObject(ent, true);
- trans.Commit();
- }
- return id;
- }
- /// <summary>
- /// 将实体集合添加到特定空间。
- /// </summary>
- /// <param name="ents"></param>
- /// <param name="db"></param>
- /// <param name="space"></param>
- /// <returns></returns>
- public static ObjectIdCollection ToSpace(this IEnumerable<Entity> ents,
- Database db = null, string space = null)
- {
- db = db ?? Application.DocumentManager.MdiActiveDocument.Database;
- var ids = new ObjectIdCollection();
- using (var trans = db.TransactionManager.StartTransaction())
- {
- var blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- var mdlSpc = trans.GetObject(blkTbl[space ?? BlockTableRecord.ModelSpace],
- OpenMode.ForWrite) as BlockTableRecord;
- foreach (var ent in ents)
- {
- ids.Add(mdlSpc.AppendEntity(ent));
- trans.AddNewlyCreatedDBObject(ent, true);
- }
- trans.Commit();
- }
- return ids;
- }
|