I agree.
By my side, to get some beers at the store, I'd rather use AutoLISP with which you can easily script native commands.
Anyway, here's a little helper to use acedCmd with managed code (inspired by Tony Tanzillo's CommandLine (thanks again to him).
The Command method build the resultbuffer with the passed arguments.
EDIT: with A2013, replace "acad.exe" with "accore.dll" in the DllImport attribute arguments.
C#
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", EntryPoint = "acedCmd",
CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
extern static private int acedCmd(IntPtr resbuf);
/// <summary>
/// Call an AutoCAD command (runs synchronously).
/// </summary>
/// <param name="args">The command name followed by command inputs.</param>
public static void Command(params object[] args)
{
ResultBuffer resbuf = new ResultBuffer();
foreach (object obj in args)
{
switch (obj.GetType().Name)
{
case "String":
resbuf.Add(new TypedValue((int)LispDataType.Text, obj)); break;
case "Int16":
resbuf.Add(new TypedValue((int)LispDataType.Int16, obj)); break;
case "Int32":
resbuf.Add(new TypedValue((int)LispDataType.Int32, obj)); break;
case "Double":
resbuf.Add(new TypedValue((int)LispDataType.Double, obj)); break;
case "Point2d":
resbuf.Add(new TypedValue((int)LispDataType.Point2d, obj)); break;
case "Point3d":
resbuf.Add(new TypedValue((int)LispDataType.Point3d, obj)); break;
case "ObjectId":
resbuf.Add(new TypedValue((int)LispDataType.ObjectId, obj)); break;
case "ObjectId[]":
foreach (ObjectId id in (ObjectId[])obj)
resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id));
break;
case "ObjectIdCollection":
foreach (ObjectId id in (ObjectIdCollection)obj)
resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id));
break;
case "SelectionSetDelayMarshalled":
case "SelectionSetFullyMarshalled":
resbuf.Add(new TypedValue((int)LispDataType.SelectionSet, obj)); break;
default:
throw new InvalidOperationException("Unsupported type in Command() method");
}
}
acedCmd(resbuf.UnmanagedObject);
} VB
<System.Security.SuppressUnmanagedCodeSecurity()> _
<DllImport("acad.exe", EntryPoint:="acedCmd", CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function acedCmd(resbuf As IntPtr) As Integer
End Function
'''<summary>
''' Call an AutoCAD command (runs synchronously).
''' </summary>
''' <param name="args">The command name followed by command inputs.</param>
Public Shared Sub Command(ParamArray args As Object())
Dim resbuf As New ResultBuffer()
For Each obj As Object In args
Select Case obj.GetType().Name
Case "String"
resbuf.Add(New TypedValue(CInt(LispDataType.Text), obj))
Exit Select
Case "Int16"
resbuf.Add(New TypedValue(CInt(LispDataType.Int16), obj))
Exit Select
Case "Int32"
resbuf.Add(New TypedValue(CInt(LispDataType.Int32), obj))
Exit Select
Case "Double"
resbuf.Add(New TypedValue(CInt(LispDataType.[Double]), obj))
Exit Select
Case "Point2d"
resbuf.Add(New TypedValue(CInt(LispDataType.Point2d), obj))
Exit Select
Case "Point3d"
resbuf.Add(New TypedValue(CInt(LispDataType.Point3d), obj))
Exit Select
Case "ObjectId"
resbuf.Add(New TypedValue(CInt(LispDataType.ObjectId), obj))
Exit Select
Case "ObjectId[]"
For Each id As ObjectId In DirectCast(obj, ObjectId())
resbuf.Add(New TypedValue(CInt(LispDataType.ObjectId), id))
Next
Exit Select
Case "ObjectIdCollection"
For Each id As ObjectId In DirectCast(obj, ObjectIdCollection)
resbuf.Add(New TypedValue(CInt(LispDataType.ObjectId), id))
Next
Exit Select
Case "SelectionSetDelayMarshalled", "SelectionSetFullyMarshalled"
resbuf.Add(New TypedValue(CInt(LispDataType.SelectionSet), obj))
Exit Select
Case Else
Throw New InvalidOperationException("Unsupported type in Command() method")
End Select
Next
acedCmd(resbuf.UnmanagedObject)
End Sub
Using examples (VB):
Draws a line.
Command("_.line", New Point3d(10.0, 20.0, 0.0), New Point3d(80.0, 50.0, 0.0), "") Freezes the "0" layer in the selected viewport.
Command("_.vplayer", "_freeze", "0", "_select", "\", "", "") Note:
- an empty string ("") means a validation (Enter)
- an anti-slash ("\" with VB or "\\" with C#) means a pause for user input.