测试代码:
[CommandMethod("test")] public static void test() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; //定义所有的视口ID集合 ObjectIdCollection viewportIDs = new ObjectIdCollection(); viewportIDs = db.GetViewports(true); List<ObjectId> objectsIdsErase = new List<ObjectId>(); PromptEntityOptions peo = new PromptEntityOptions("\n请选择一个视口!"); peo.SetRejectMessage("\n当前选择的图元不是视口!"); peo.AddAllowedClass(typeof(Viewport), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { //获取当前空间的块表记录 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); //获取当前空间中的当前布局 Layout lay = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForRead); //布局名称 ed.WriteMessage("\nLayoutName:" + lay.LayoutName); //布局ID ed.WriteMessage("\nId:" + lay.Id.ToString()); ed.WriteMessage("\nObjectId:" + lay.ObjectId.ToString()); ed.WriteMessage("\nbtr.LayoutId:" + btr.LayoutId); //当前空间ID ed.WriteMessage("\nCurrentSpaceId:" + lay.BlockTableRecordId.ToString()); //通过选择集方式访问视口对象 Viewport vp = (Viewport)tr.GetObject(per.ObjectId, OpenMode.ForRead); ed.WriteMessage("\nViewPort OwnerId:" + vp.OwnerId); ed.WriteMessage("\nLayout OwnerId:" + lay.OwnerId); ed.WriteMessage("\nbtr OwnerId:" + btr.OwnerId); //取得图纸空间中所有浮动视口对象 Viewport Vports = (Viewport)tr.GetObject(db.PaperSpaceVportId, OpenMode.ForRead); foreach (ObjectId id in viewportIDs) { //通过遍历视口ID方式访问视口对象 Viewport vp1 = id.GetObject(OpenMode.ForRead) as Viewport; } Matrix3d PsToMsTransForm = PaperToModel(vp); //取得当前图形中所有的BlockTable BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead); //取得当前图形中所有的BlockTable BlockTable bt_other = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); //取得当前空间的所有块表记录 BlockTableRecord btr_CurrentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead); ed.WriteMessage("\n" + btr_CurrentSpace.Name); //取得模型空间中所有的块表记录 BlockTableRecord btr_model = (BlockTableRecord)bt["*Model_Space"].GetObject(OpenMode.ForRead); ed.WriteMessage("\n" + btr_model.Name); //取得图纸空间中所有的块表记录 BlockTableRecord btr_paper = (BlockTableRecord)bt["*Paper_Space"].GetObject(OpenMode.ForRead); ed.WriteMessage("\n" + btr_paper.Name); //取得块名为TestBlock的记录 BlockTableRecord btr_block = (BlockTableRecord)bt["TestBlock"].GetObject(OpenMode.ForRead); ed.WriteMessage("\n块名为:{0},块属于{1}" , btr_block.Name,btr_block.OwnerId); tr.Commit(); } }
测试结果:
LayoutName:Layout1
Id:(8796087794144)
ObjectId:(8796087794144)
btr.LayoutId:(8796087794144)
CurrentSpaceId:(8796087794096)
ViewPort
OwnerId:(8796087794096)
Layout OwnerId:(8796087794080)
btr
OwnerId:(8796087793696)
*Paper_Space
*Model_Space
*Paper_Space
块名为:TestBlock,块属于(8796087793696)