明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2477|回复: 5

[JIG] [求助]Jig下如何使用正交模式(向lzh741206 版主求助)

[复制链接]
发表于 2009-6-30 16:10 | 显示全部楼层 |阅读模式
lzh741206 版主求助,
以下是一个动态画一段多段线的代码。
跟LISP相比,其实已经方便多了,可以在动态的情况下实现捕捉。
但是,打开正交模式后,以下代码不能有正交的效果,请指正。
  1. using System;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.GraphicsInterface;
  7. using Autodesk.AutoCAD.Runtime;
  8. namespace CsMgd29
  9. {
  10.     public class Class1 : DrawJig
  11.     {
  12.         // 声明多段线对象
  13.         private Polyline ent;
  14.         // 声明多段线的起点、终点
  15.         private Point3d pt1, pt2;
  16.         [CommandMethod("TT")]
  17.         public void Test()
  18.         {
  19.             Database db = HostApplicationServices.WorkingDatabase;
  20.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  21.             // 普通的点交互操作.
  22.             PromptPointOptions optPoint = new PromptPointOptions("\n请指定起点: ");
  23.             PromptPointResult resPoint = ed.GetPoint(optPoint);
  24.             if (resPoint.Status != PromptStatus.OK)
  25.                 return;
  26.             pt1 = resPoint.Value;
  27.             Point2d[] pt = new Point2d[2];
  28.             pt[0] = new Point2d(0, 0);
  29.             pt[1] = new Point2d(0, 0);
  30.             Point2dCollection pts = new Point2dCollection(pt);
  31.             ent = (Polyline)new Polyline();
  32.             for (int i = 0; i <= 1; i++)
  33.                 ent.AddVertexAt(i, pts[i], 0, 0, 0);
  34.             using (Transaction trans = db.TransactionManager.StartTransaction())
  35.             {
  36.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  37.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  38.                 // 开始拖拽.
  39.                 PromptResult resJig = ed.Drag(this);
  40.                 if (resJig.Status == PromptStatus.OK)
  41.                 {
  42.                     // 将对象加入到图形数据库中.
  43.                     btr.AppendEntity(ent);
  44.                     trans.AddNewlyCreatedDBObject(ent, true);
  45.                     trans.Commit();
  46.                 }
  47.             }
  48.         }
  49.         // Sampler函数用于检测用户的输入.
  50.         protected override SamplerStatus Sampler(JigPrompts prompts)
  51.         {
  52.             Database db = HostApplicationServices.WorkingDatabase;
  53.             // 定义一个点拖动交互类.
  54.             JigPromptPointOptions optJigPoint = new JigPromptPointOptions("\n请指终点:");
  55.             // 设置拖拽光标类型.
  56.             optJigPoint.Cursor = CursorType.RotatedCrosshair;
  57.             // 用AcquirePoint函数得到用户输入的点.
  58.             PromptPointResult resJigPoint1 = prompts.AcquirePoint(optJigPoint);
  59.             Point3d curPt = resJigPoint1.Value;
  60.             if (curPt != pt2)
  61.             {
  62.                 // 重新设置参数--------------------------------------------.
  63.                 Point2d p0 = new Point2d(pt1.X, pt1.Y);
  64.                 Point2d p1 = new Point2d(curPt[0], curPt[1]);
  65.                 ent.SetPointAt(0, p0);
  66.                 ent.SetPointAt(1, p1);
  67.                 ent.SetStartWidthAt(0, 30);
  68.                 ent.SetEndWidthAt(0, 30);
  69.                 ent.SetStartWidthAt(1, 30);
  70.                 ent.SetEndWidthAt(1, 30);
  71.                 pt2 = curPt;
  72.                 return SamplerStatus.OK;
  73.             }
  74.             else
  75.                 return SamplerStatus.NoChange;
  76.         }
  77.         // WorldDraw函数用于刷新屏幕上显示的图形.
  78.         protected override bool WorldDraw(WorldDraw draw)
  79.         {
  80.             // 刷新画面.
  81.             draw.Geometry.Draw(ent);
  82.             return true;
  83.         }
  84.     }
  85. }
发表于 2009-6-30 18:13 | 显示全部楼层
大版主不在线,小二子顶一下。没处理UCS,也没进行仔细的调试。
  1. using System;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.GraphicsInterface;
  7. using Autodesk.AutoCAD.Runtime;
  8. namespace CsMgd29
  9. {
  10.     public class Class1 : DrawJig
  11.     {
  12.         // 声明多段线对象
  13.         private Polyline ent;
  14.         // 声明多段线的起点、终点
  15.         private Point3d pt1, pt2;
  16.         [CommandMethod("abc")]
  17.         public void Testabc()
  18.         {
  19.             Database db = HostApplicationServices.WorkingDatabase;
  20.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  21.             // 普通的点交互操作.
  22.             PromptPointOptions optPoint = new PromptPointOptions("\n请指定起点: ");
  23.             PromptPointResult resPoint = ed.GetPoint(optPoint);
  24.             if (resPoint.Status != PromptStatus.OK)
  25.                 return;
  26.             pt1 = resPoint.Value;
  27.             Point2d[] pt = new Point2d[2];
  28.             pt[0] = new Point2d(0, 0);
  29.             pt[1] = new Point2d(0, 0);
  30.             Point2dCollection pts = new Point2dCollection(pt);
  31.             ent = (Polyline)new Polyline();
  32.             for (int i = 0; i <= 1; i++)
  33.                 ent.AddVertexAt(i, pts[i], 0, 0, 0);
  34.             using (Transaction trans = db.TransactionManager.StartTransaction())
  35.             {
  36.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  37.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  38.                 // 开始拖拽.
  39.                 PromptResult resJig = ed.Drag(this);
  40.                 if (resJig.Status == PromptStatus.OK)
  41.                 {
  42.                     // 将对象加入到图形数据库中.
  43.                     btr.AppendEntity(ent);
  44.                     trans.AddNewlyCreatedDBObject(ent, true);
  45.                     trans.Commit();
  46.                 }
  47.             }
  48.         }
  49.         // Sampler函数用于检测用户的输入.
  50.         protected override SamplerStatus Sampler(JigPrompts prompts)
  51.         {
  52.             Database db = HostApplicationServices.WorkingDatabase;
  53.             // 定义一个点拖动交互类.
  54.             JigPromptPointOptions optJigPoint = new JigPromptPointOptions("\n请指终点:");
  55.             // 设置拖拽光标类型.
  56.             optJigPoint.Cursor = CursorType.RotatedCrosshair;
  57.             // 用AcquirePoint函数得到用户输入的点.
  58.             PromptPointResult resJigPoint1 = prompts.AcquirePoint(optJigPoint);
  59.             Point3d curPt = resJigPoint1.Value;
  60.             short ORTHOMODE = (short)(Application.GetSystemVariable("ORTHOMODE"));
  61.             if (ORTHOMODE == 1)
  62.             {
  63.                 double dx = Math.Abs(curPt.X - pt1.X);
  64.                 double dy = Math.Abs(curPt.Y - pt1.Y);
  65.                 if (dx >= dy)
  66.                 {
  67.                     curPt = new Point3d(curPt.X, pt1.Y, curPt.Z);
  68.                 }
  69.                 else
  70.                 {
  71.                     curPt = new Point3d(pt1.X, curPt.Y, curPt.Z);
  72.                 }
  73.             }
  74.         
  75.             if (curPt != pt2)
  76.             {
  77.                 // 重新设置参数--------------------------------------------.
  78.                 Point2d p0 = new Point2d(pt1.X, pt1.Y);
  79.                 Point2d p1 = new Point2d(curPt[0], curPt[1]);
  80.                 ent.SetPointAt(0, p0);
  81.                 ent.SetPointAt(1, p1);
  82.                 ent.SetStartWidthAt(0, 30);
  83.                 ent.SetEndWidthAt(0, 30);
  84.                 ent.SetStartWidthAt(1, 30);
  85.                 ent.SetEndWidthAt(1, 30);
  86.                 pt2 = curPt;
  87.                 return SamplerStatus.OK;
  88.             }
  89.             else
  90.                 return SamplerStatus.NoChange;
  91.         }
  92.         // WorldDraw函数用于刷新屏幕上显示的图形.
  93.         protected override bool WorldDraw(WorldDraw draw)
  94.         {
  95.             // 刷新画面.
  96.             draw.Geometry.Draw(ent);
  97.             return true;
  98.         }
  99.     }
  100. }

 楼主| 发表于 2009-6-30 19:51 | 显示全部楼层

 

原来是用算法,我以为有现成的函数。

谢谢ahlzl 版主

发表于 2009-6-30 20:25 | 显示全部楼层

似乎这样就可以了?

                optJigPoint.UseBasePoint = true;
                optJigPoint.BasePoint = pt1;

这两句我只要用Jig就加上,现在才知道是正交和极轴要用的,汗

发表于 2009-6-30 21:16 | 显示全部楼层
lzh741206发表于2009-6-30 20:25:00似乎这样就可以了?                optJigPoint.UseBasePoint = true;      &n

这两句我在JIG中几乎都用的,因为我从来不用正交(讨厌正交),所以没发现它在正交中的作用(我只用极轴)。哈哈!!!

 楼主| 发表于 2009-7-1 09:12 | 显示全部楼层
lzh741206发表于2009-6-30 20:25:00似乎这样就可以了?                optJigPoint.UseBasePoint = true;      &n

这个答案是我的发这个贴子的初衷。谢谢版主。

也谢谢ahlzl,提供的方法,其实就是LISP中用GRREAD模仿正交的思路。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-2 10:52 , Processed in 0.622565 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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