找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 910|回复: 0

DragGen with AutoCAD .NET API

[复制链接]

已领礼包: 40个

财富等级: 招财进宝

发表于 2021-1-12 18:03:29 | 显示全部楼层 |阅读模式

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

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

×

问题:
Is there an equivalent API in AutoCAD .NET of ARX global function acedDragGen()?

解答:
No, there is not a straightforward method in AutoCAD .NET API. We can use P/Invoke to invoke the acedDragGen() function. However, we need to work around a few limitations and utilize a new feature of Visual Studio 2005. Please check the following code which will drag a selection set to a new location:


  1.     public class DragGen2006
  2.     {
  3.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
  4.         private static extern PromptStatus acedSSGet(string str, IntPtr pt1, IntPtr pt2, IntPtr filter, out long ss);

  5.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
  6.         private static extern PromptStatus acedSSFree(ref long ss);

  7.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
  8.         private static extern PromptStatus acedSSLength(ref long ss, out int len);

  9.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
  10.         private static extern PromptStatus acedSSName(ref long ss, int i, out long name);

  11.         [DllImport("acdb16.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
  12.         private static extern ErrorStatus acdbGetObjectId(out ObjectId id, ref long name);

  13.         //The following doesn't work yet: will work by postrio FCS.
  14.         //Cause: Matrix3d has automatic layout. Applying LayoutKind.Sequential
  15.         //to Matrix3d causes the VC7.x compiler to crash.
  16.         //
  17.         //This attribute is a .NET 2.0 feature (required!)
  18.         //[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  19.         //private delegate PromptStatus Sampler(ref Point3d pt, ref Matrix3d mat);
  20.         //

  21.         //workaround:
  22.         struct InteropMatrix3d
  23.         {
  24.             public double a00, a01, a02, a03;
  25.             public double a10, a11, a12, a13;
  26.             public double a20, a21, a22, a23;
  27.             public double a30, a31, a32, a33;
  28.         }

  29.         //This attribute is a .NET 2.0 feature (required!)
  30.         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  31.         private delegate PromptStatus Sampler(ref Point3d pt, ref InteropMatrix3d mat);

  32.         // Set the matrix as identity
  33.         void ident_init(ref InteropMatrix3d mat)
  34.         {
  35.             mat = new InteropMatrix3d();

  36.             mat.a00 = mat.a01 = mat.a02 = mat.a03 = 0;
  37.             mat.a10 = mat.a11 = mat.a12 = mat.a13 = 0;
  38.             mat.a20 = mat.a21 = mat.a22 = mat.a23 = 0;
  39.             mat.a30 = mat.a31 = mat.a32 = mat.a33 = 0;

  40.             mat.a00 = mat.a11 = mat.a22 = mat.a33 = 1;
  41.         }

  42.         [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
  43.         private static extern PromptStatus acedDragGen(ref long ss, string prompt, int cursor, Sampler callback, out Point3d pt);

  44.         Point3d m_basePoint;
  45.         [CommandMethod("TestDragGen2006")]
  46.         public void DoIt()
  47.         {
  48.             long ss;
  49.             if (acedSSGet(null, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out ss) != PromptStatus.OK)
  50.                 return;
  51.             try
  52.             {
  53.                 PromptPointResult res = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint("Base point:");
  54.                 if (res.Status != PromptStatus.OK)
  55.                     return;
  56.                 m_basePoint = res.Value;
  57.                 Point3d pt;
  58.                 acedDragGen(ref ss, "Do something:", 0, new Sampler(Callback), out pt);
  59.                 Matrix3d mat = Matrix3d.Displacement(pt - m_basePoint);
  60.                 int len;
  61.                 acedSSLength(ref ss, out len);
  62.                 using (Transaction t = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
  63.                 {
  64.                     for (int i = 0; i < len; ++i)
  65.                     {
  66.                         long name;
  67.                         if (acedSSName(ref ss, i, out name) == PromptStatus.OK)
  68.                         {
  69.                             ObjectId id;
  70.                             if (acdbGetObjectId(out id, ref name) == ErrorStatus.OK)
  71.                             {
  72.                                 Entity ent = (Entity)t.GetObject(id, OpenMode.ForWrite);
  73.                                 ent.TransformBy(mat);
  74.                             }
  75.                         }
  76.                     }
  77.                     t.Commit();
  78.                 }
  79.             }
  80.             catch (System.Exception ex) {
  81.                 System.Windows.Forms.MessageBox.Show(ex.Message);
  82.             }
  83.             finally
  84.             {
  85.                 acedSSFree(ref ss);
  86.             }

  87.         }
  88.         private PromptStatus Callback(ref Point3d pt, ref InteropMatrix3d mat)
  89.         {
  90.             ident_init(ref mat);
  91.             mat.a03 = pt[0] - m_basePoint[0];
  92.             mat.a13 = pt[1] - m_basePoint[1];
  93.             mat.a23 = pt[2] - m_basePoint[2];
  94.             return PromptStatus.OK;
  95.         }
  96.     }


Please note that Visual Studio 2005 has to be used as the attribute UnmanagedFunctionPointer is only availabe in this version. And we need to create an interop matrix InteropMatrix3d by ourselves.

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

本版积分规则

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

GMT+8, 2024-4-29 04:30 , Processed in 0.151527 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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