明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4362|回复: 8

[JIG] Jig示例——拖动圆心

[复制链接]
发表于 2009-5-20 19:42 | 显示全部楼层 |阅读模式
ARX帮助中的示例文件中,有一个用C#画椭圆的Jig范例,这里我写了一个更简单的一个Jig范例,希望对您能有所帮助。
我们希望有这样的效果,如何实现呢?

一、建立一个C#类库工程,注册一个“JigCircle”命令,生成代码如下:

  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.GraphicsInterface;
  6. using Autodesk.AutoCAD.Runtime;
  7. namespace JigTest
  8. {
  9.     public class JigClass : DrawJig
  10.     {
  11.         [CommandMethod("JigCircle")]
  12.         public void MyTest()
  13.         {
  14.             
  15.         }
  16.     }
  17. }
二、在类名JigClass后面键入“: DrawJig”(注意空格),然后在DrawJig这几个字母上右击,选择“实现抽象类”。自动生成生成两个重载的函数,这时完整代码如下:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.GraphicsInterface;
  6. using Autodesk.AutoCAD.Runtime;
  7. namespace JigTest
  8. {
  9.     public class JigClass : DrawJig
  10.     {
  11.         [CommandMethod("JigCircle")]
  12.         public void MyTest()
  13.         {
  14.             
  15.         }
  16.         protected override bool WorldDraw(WorldDraw draw)
  17.         {
  18.             throw new System.NotImplementedException();
  19.         }
  20.         protected override SamplerStatus Sampler(JigPrompts prompts)
  21.         {
  22.             throw new System.NotImplementedException();
  23.         }
  24.     }
  25. }
三、添加两个全局量:
  1. private Circle mCircle;
  2. private Point3d mCenterPt;
四、为各个函数书写代码,并添加一个添加图元到数据库的函数AppendEntity。最后完整代码如下:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.GraphicsInterface;
  6. using Autodesk.AutoCAD.Runtime;
  7. namespace JigTest
  8. {
  9.     public class JigClass : DrawJig
  10.     {
  11.         private Circle mCircle;
  12.         private Point3d mCenterPt;
  13.         [CommandMethod("JigCircle")]
  14.         public void MyTest()
  15.         {
  16.             Database db = HostApplicationServices.WorkingDatabase;
  17.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  18.             Matrix3d mt = ed.CurrentUserCoordinateSystem;
  19.             mCircle = new Circle();
  20.             mCircle.Radius = 100;
  21.             mCircle.Normal = db.Ucsxdir.CrossProduct(db.Ucsydir);
  22.             PromptResult resJig = ed.Drag(this);
  23.             if (resJig.Status == PromptStatus.OK)
  24.             {
  25.                 AppendEntity(mCircle);
  26.             }
  27.         }
  28.         protected override bool WorldDraw(WorldDraw draw)
  29.         {
  30.             draw.Geometry.Draw(mCircle);
  31.             return true;
  32.         }
  33.         protected override SamplerStatus Sampler(JigPrompts prompts)
  34.         {
  35.             JigPromptPointOptions optJigDis = new JigPromptPointOptions
  36.                 ("\n请指定圆的圆心");
  37.             // 设置对拖拽的约束.
  38.             optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
  39.             PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
  40.             Point3d tempPt = resJigDis.Value;
  41.             if (resJigDis.Status == PromptStatus.Cancel)
  42.             {
  43.                 return SamplerStatus.Cancel;
  44.             }
  45.             if (mCenterPt != tempPt)
  46.             {
  47.                 mCenterPt = tempPt;
  48.                 mCircle.Center = mCenterPt;
  49.                 return SamplerStatus.OK;
  50.             }
  51.             else
  52.             {
  53.                 return SamplerStatus.NoChange;
  54.             }
  55.         }
  56.         private ObjectId AppendEntity(Entity ent)
  57.         {
  58.             ObjectId entId;
  59.             Database db = HostApplicationServices.WorkingDatabase;
  60.             using (Transaction trans = db.TransactionManager.StartTransaction())
  61.             {
  62.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
  63.                     OpenMode.ForRead);
  64.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject
  65.                     (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  66.                 entId = btr.AppendEntity(ent);
  67.                 trans.AddNewlyCreatedDBObject(ent, true);
  68.                 trans.Commit();
  69.             }
  70.             return entId;
  71.         }
  72.     }
  73. }

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1威望 +1 明经币 +5 金钱 +20 贡献 +5 激情 +5 收起 理由
雪山飞狐_lzh + 1 + 5 + 20 + 5 + 5 【精华】好程序

查看全部评分

 楼主| 发表于 2009-5-20 19:46 | 显示全部楼层

后话,请把这个例子改为插入块。

发表于 2009-5-23 12:31 | 显示全部楼层

看过几个示例及专题。。。

发现一个共同的问题。

没有注释。

或是只有几句原作者的英文注释。

确实看不懂。

发表于 2009-6-5 17:57 | 显示全部楼层
就是,没有注释看水懂啦
发表于 2009-6-22 15:42 | 显示全部楼层

能否来个Jig写的:

加引线的序号球。

1.指定起点ptA

2.在指定第二点的之前,动态显示序号球及序号。

 动态输入改变序号,动态指定第二点的位置。

发表于 2009-6-22 15:59 | 显示全部楼层

序号球看看这里吧

动态输入改变序号的话,这个要改动一下

http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75604

发表于 2011-1-10 16:06 | 显示全部楼层
不错不错,学习学习
发表于 2015-8-8 20:40 | 显示全部楼层
   学习    ~         
发表于 2015-8-8 21:22 | 显示全部楼层
  1. //一个Jig插入块的实例
  2.     public class jigInsertBlock : DrawJig
  3.     {
  4.         private BlockReference blkRef;
  5.         [CommandMethod("t2")]
  6.         public void t2()
  7.         {
  8.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  9.             Database db = HostApplicationServices.WorkingDatabase;
  10.             Editor ed = doc.Editor;
  11.             ObjectId blkRec;
  12.             //这里创建一个事务, 但是不能直接在这个事务里面进行操作,否则会造成出错.出错的原因是块引用为Null,可能是因为Using后直接被销毁了?
  13.             using (Transaction trans = db.TransactionManager.StartTransaction())
  14.             {
  15.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  16.                 BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  17.                 if (bt.Has("tt"))
  18.                 {
  19.                     blkRec = bt["tt"];
  20.                 }
  21.                 else
  22.                 {
  23.                     ed.WriteMessage("图中没有tt图块的定义,程序退出.\n");
  24.                     return;
  25.                 }
  26.             }
  27.             blkRef = new BlockReference(new Point3d(0, 0, 0), blkRec);
  28.             PromptResult promptRes = ed.Drag(this);
  29.             if (promptRes.Status == PromptStatus.OK)
  30.             {
  31.                 //绘图事物
  32.                 using (Transaction trans = db.TransactionManager.StartTransaction())
  33.                 {
  34.                     BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  35.                     BlockTableRecord modelSpace = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  36.                     modelSpace.AppendEntity(blkRef);
  37.                     trans.AddNewlyCreatedDBObject(blkRef, true);
  38.                     trans.Commit();
  39.                 }
  40.             }
  41.         }
  42.         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  43.         {
  44.             //提供的功能有可能产生几何不为不同的视口变化的能力。 draw   应该是类似Lisp中的 (redraw)
  45.             draw.Geometry.Draw(blkRef);
  46.             return true;
  47.         }

  48.         protected override SamplerStatus Sampler(JigPrompts prompts)
  49.         {
  50.             JigPromptPointOptions optJigDis = new JigPromptPointOptions
  51.                 ("\n请指定块的位置");
  52.             // 设置对拖拽的约束.
  53.             //这里我尝试使用基点,但是没有成功
  54.             optJigDis.UseBasePoint = true;
  55.             optJigDis.BasePoint = new Point3d(0, 0, 0);
  56.             optJigDis.UserInputControls = UserInputControls.Accept3dCoordinates;
  57.             PromptPointResult resJigDis = prompts.AcquirePoint(optJigDis);
  58.             Point3d tempPt = resJigDis.Value;
  59.             if (resJigDis.Status == PromptStatus.Cancel)
  60.             {
  61.                 return SamplerStatus.Cancel;
  62.             }
  63.             //这里我直接使用了块参照的基点
  64.             if (blkRef.Position != tempPt)
  65.             {
  66.                 blkRef.Position = tempPt;
  67.                 return SamplerStatus.OK;
  68.             }
  69.             else
  70.             {
  71.                 return SamplerStatus.NoChange;
  72.             }
  73.         }
  74.     }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-20 02:14 , Processed in 0.623618 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表