- //一个Jig插入块的实例
- public class jigInsertBlock : DrawJig
- {
- private BlockReference blkRef;
- [CommandMethod("t2")]
- public void t2()
- {
- Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- Database db = HostApplicationServices.WorkingDatabase;
- Editor ed = doc.Editor;
- ObjectId blkRec;
- //这里创建一个事务, 但是不能直接在这个事务里面进行操作,否则会造成出错.出错的原因是块引用为Null,可能是因为Using后直接被销毁了?
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- if (bt.Has("tt"))
- {
- blkRec = bt["tt"];
- }
- else
- {
- ed.WriteMessage("图中没有tt图块的定义,程序退出.\n");
- return;
- }
- }
- blkRef = new BlockReference(new Point3d(0, 0, 0), blkRec);
- PromptResult promptRes = ed.Drag(this);
- if (promptRes.Status == PromptStatus.OK)
- {
- //绘图事物
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- modelSpace.AppendEntity(blkRef);
- trans.AddNewlyCreatedDBObject(blkRef, true);
- trans.Commit();
- }
- }
- }
- protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
- {
- //提供的功能有可能产生几何不为不同的视口变化的能力。 draw 应该是类似Lisp中的 (redraw)
- draw.Geometry.Draw(blkRef);
- return true;
- }
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- JigPromptPointOptions optJigDis = new JigPromptPointOptions
- ("\n请指定块的位置");
- // 设置对拖拽的约束.
- //这里我尝试使用基点,但是没有成功
- optJigDis.UseBasePoint = true;
- optJigDis.BasePoint = new Point3d(0, 0, 0);
- optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
- PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
- Point3d tempPt = resJigDis.Value;
- if (resJigDis.Status == PromptStatus.Cancel)
- {
- return SamplerStatus.Cancel;
- }
- //这里我直接使用了块参照的基点
- if (blkRef.Position != tempPt)
- {
- blkRef.Position = tempPt;
- return SamplerStatus.OK;
- }
- else
- {
- return SamplerStatus.NoChange;
- }
- }
- }
|