DragGen with AutoCAD .NET API
问题:
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
{
private static extern PromptStatus acedSSGet(string str, IntPtr pt1, IntPtr pt2, IntPtr filter, out long ss);
private static extern PromptStatus acedSSFree(ref long ss);
private static extern PromptStatus acedSSLength(ref long ss, out int len);
private static extern PromptStatus acedSSName(ref long ss, int i, out long name);
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!)
//
//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!)
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;
}
private static extern PromptStatus acedDragGen(ref long ss, string prompt, int cursor, Sampler callback, out Point3d pt);
Point3d m_basePoint;
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 - m_basePoint;
mat.a13 = pt - m_basePoint;
mat.a23 = pt - m_basePoint;
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.
页:
[1]