试了好多次,还就这种写法没问题,块可以正常定义和更新,大神们有什么更简便的写法么?
代码如下:
- [CommandMethod("kdy")]
- public void kdy()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- PromptSelectionOptions pso = new PromptSelectionOptions();
- pso.MessageForAdding = "块定义";
- PromptSelectionResult psr = ed.GetSelection(pso);
- if (psr.Status != PromptStatus.OK) { return; }
- PromptPointOptions ppo = new PromptPointOptions("选择块原点");
- ppo.AllowNone = true;
- PromptPointResult ppr = ed.GetPoint(ppo);
- Point3d insertPoint = new Point3d();
- if (ppr.Status != PromptStatus.OK)
- {
- return;
- }
- else
- {
- insertPoint = ppr.Value;
- }
- db.DefineABlock("MINE", psr, insertPoint);
- }
- [CommandMethod("crk")]
- public void crk()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- // 是否存在 MINE 块记录
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- if (!bt.Has("MINE")) return;
- }
- // 确定块插入点
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- PromptPointOptions ppo = new PromptPointOptions("选择块插入点");
- ppo.AllowNone = true;
- PromptPointResult ppr = ed.GetPoint(ppo);
- Point3d insertPoint = new Point3d();
- if (ppr.Status != PromptStatus.OK)
- {
- return;
- }
- else
- {
- insertPoint = ppr.Value;
- }
- // 模型空间中插入块参照
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
- BlockTableRecord btr = bt["MINE"].GetObject(OpenMode.ForRead) as BlockTableRecord;
- BlockReference blkRef = new BlockReference(insertPoint, btr.ObjectId);
- BlockTableRecord modelSpace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
- modelSpace.AppendEntity(blkRef);
- trans.AddNewlyCreatedDBObject(blkRef, true);
- trans.Commit();
- }
- }
|