- UID
- 5280
- 积分
- 9466
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-5-18
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
问题:
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:
-
- public class DragGen2006
- {
- [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern PromptStatus acedSSGet(string str, IntPtr pt1, IntPtr pt2, IntPtr filter, out long ss);
- [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern PromptStatus acedSSFree(ref long ss);
- [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern PromptStatus acedSSLength(ref long ss, out int len);
- [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern PromptStatus acedSSName(ref long ss, int i, out long name);
- [DllImport("acdb16.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern ErrorStatus acdbGetObjectId(out ObjectId id, ref long name);
- //The following doesn't work yet: will work by postrio FCS.
- //Cause: Matrix3d has automatic layout. Applying LayoutKind.Sequential
- //to Matrix3d causes the VC7.x compiler to crash.
- //
- //This attribute is a .NET 2.0 feature (required!)
- //[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- //private delegate PromptStatus Sampler(ref Point3d pt, ref Matrix3d mat);
- //
- //workaround:
- struct InteropMatrix3d
- {
- public double a00, a01, a02, a03;
- public double a10, a11, a12, a13;
- public double a20, a21, a22, a23;
- public double a30, a31, a32, a33;
- }
- //This attribute is a .NET 2.0 feature (required!)
- [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
- private delegate PromptStatus Sampler(ref Point3d pt, ref InteropMatrix3d mat);
- // Set the matrix as identity
- void ident_init(ref InteropMatrix3d mat)
- {
- mat = new InteropMatrix3d();
- mat.a00 = mat.a01 = mat.a02 = mat.a03 = 0;
- mat.a10 = mat.a11 = mat.a12 = mat.a13 = 0;
- mat.a20 = mat.a21 = mat.a22 = mat.a23 = 0;
- mat.a30 = mat.a31 = mat.a32 = mat.a33 = 0;
- mat.a00 = mat.a11 = mat.a22 = mat.a33 = 1;
- }
- [DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, ExactSpelling = true)]
- private static extern PromptStatus acedDragGen(ref long ss, string prompt, int cursor, Sampler callback, out Point3d pt);
- Point3d m_basePoint;
- [CommandMethod("TestDragGen2006")]
- public void DoIt()
- {
- long ss;
- if (acedSSGet(null, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, out ss) != PromptStatus.OK)
- return;
- try
- {
- PromptPointResult res = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint("Base point:");
- if (res.Status != PromptStatus.OK)
- return;
- m_basePoint = res.Value;
- Point3d pt;
- acedDragGen(ref ss, "Do something:", 0, new Sampler(Callback), out pt);
- Matrix3d mat = Matrix3d.Displacement(pt - m_basePoint);
- int len;
- acedSSLength(ref ss, out len);
- using (Transaction t = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
- {
- for (int i = 0; i < len; ++i)
- {
- long name;
- if (acedSSName(ref ss, i, out name) == PromptStatus.OK)
- {
- ObjectId id;
- if (acdbGetObjectId(out id, ref name) == ErrorStatus.OK)
- {
- Entity ent = (Entity)t.GetObject(id, OpenMode.ForWrite);
- ent.TransformBy(mat);
- }
- }
- }
- t.Commit();
- }
- }
- catch (System.Exception ex) {
- System.Windows.Forms.MessageBox.Show(ex.Message);
- }
- finally
- {
- acedSSFree(ref ss);
- }
- }
- private PromptStatus Callback(ref Point3d pt, ref InteropMatrix3d mat)
- {
- ident_init(ref mat);
- mat.a03 = pt[0] - m_basePoint[0];
- mat.a13 = pt[1] - m_basePoint[1];
- mat.a23 = pt[2] - m_basePoint[2];
- return PromptStatus.OK;
- }
- }
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.
|
|