找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1163|回复: 1

[分享] Using Transient graphics

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-25 03:37:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

×
Using Transient graphics                                                        By Balaji Ramamoorthy
In this post we looked at using graphics functions provided by AutoCAD API to show temporary graphics. In this post we will look at using the transient graphics API to draw temporary graphics. This API provides us much better control over the display of the graphics as compared to the graphics functions.
Here is a sample code that uses transient graphics to mark the intersection points of rectangle and a ray. To try this, draw a rectangle and select an internal point and the ray direction when prompted.
  1. using Autodesk.AutoCAD.GraphicsInterface;
  2. DBObjectCollection _markers = null;
  3. [CommandMethod("Test")]
  4. public void TestMethod()
  5. {
  6.     Document activeDoc
  7.         = Application.DocumentManager.MdiActiveDocument;
  8.     Database db = activeDoc.Database;
  9.     Editor ed = activeDoc.Editor;
  10.     PromptEntityOptions peo
  11.         = new PromptEntityOptions("Select a polyline : ");
  12.     peo.SetRejectMessage("Not a polyline");
  13.     peo.AddAllowedClass
  14.         (
  15.             typeof(Autodesk.AutoCAD.DatabaseServices.Polyline),
  16.             true
  17.         );
  18.     PromptEntityResult per = ed.GetEntity(peo);
  19.     if (per.Status != PromptStatus.OK)
  20.         return;
  21.     ObjectId plOid = per.ObjectId;
  22.     PromptPointResult ppr =
  23.         ed.GetPoint
  24.         (
  25.             new PromptPointOptions("Select an internal point : ")
  26.         );
  27.     if (ppr.Status != PromptStatus.OK)
  28.         return;
  29.     Point3d testPoint = ppr.Value;
  30.     PromptAngleOptions pao
  31.         = new PromptAngleOptions("Specify ray direction");
  32.     pao.BasePoint = testPoint;
  33.     pao.UseBasePoint = true;
  34.     PromptDoubleResult rayAngle = ed.GetAngle(pao);

  35.     if (rayAngle.Status != PromptStatus.OK)
  36.         return;
  37.     Point3d tempPoint = testPoint.Add(Vector3d.XAxis);
  38.     tempPoint
  39.         = tempPoint.RotateBy(
  40.                                 rayAngle.Value,
  41.                                 Vector3d.ZAxis,
  42.                                 testPoint
  43.                             );
  44.     Vector3d rayDir = tempPoint - testPoint;
  45.     ClearTransientGraphics();
  46.     _markers = new DBObjectCollection();
  47.     using (Transaction tr
  48.                 = db.TransactionManager.StartTransaction())
  49.     {
  50.         Curve plcurve = tr.GetObject(
  51.                                         plOid,
  52.                                         OpenMode.ForRead

  53.                                     ) as Curve;
  54.         for (int cnt = 0; cnt < 2; cnt++)
  55.         {
  56.             if (cnt == 1)
  57.                 rayDir = rayDir.Negate();
  58.             using (Ray ray = new Ray())
  59.             {
  60.                 ray.BasePoint = testPoint;
  61.                 ray.UnitDir = rayDir;
  62.                 Point3dCollection intersectionPts
  63.                                     = new Point3dCollection();
  64.                 plcurve.IntersectWith(
  65.                                        ray,
  66.                                         Intersect.OnBothOperands,
  67.                                         intersectionPts,
  68.                                         IntPtr.Zero,
  69.                                         IntPtr.Zero
  70.                                      );
  71.                 foreach (Point3d pt in intersectionPts)
  72.                 {
  73.                     Circle marker
  74.                         = new Circle(pt, Vector3d.ZAxis, 0.2);
  75.                     marker.Color = Color.FromRgb(0, 255, 0);
  76.                     _markers.Add(marker);
  77.                     IntegerCollection intCol
  78.                                     = new IntegerCollection();
  79.                     TransientManager tm
  80.                         = TransientManager.CurrentTransientManager;
  81.                     tm.AddTransient
  82.                         (
  83.                             marker,
  84.                             TransientDrawingMode.Highlight,
  85.                             128,
  86.                             intCol
  87.                         );
  88.                     ed.WriteMessage("\n" + pt.ToString());
  89.                 }
  90.             }
  91.         }
  92.         tr.Commit();
  93.     }
  94. }
  95. void ClearTransientGraphics()
  96. {
  97.     TransientManager tm
  98.             = TransientManager.CurrentTransientManager;
  99.     IntegerCollection intCol = new IntegerCollection();
  100.     if (_markers != null)
  101.     {
  102.         foreach (DBObject marker in _markers)
  103.         {
  104.             tm.EraseTransient(
  105.                                 marker,
  106.                                 intCol
  107.                              );
  108.             marker.Dispose();
  109.         }
  110.     }
  111. }


评分

参与人数 1D豆 +2 收起 理由
ScmTools + 2 很给力! 资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 1632个

财富等级: 堆金积玉

发表于 2013-5-25 14:34:06 | 显示全部楼层
支持! 谢谢分享
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2025-9-6 12:19 , Processed in 0.373138 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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