Hello I have a problem, I want to use the AutoCAD COM interop without referencing the "Interop.AutoCAD" dll. This code works fine when I reference the "Interop.AutoCAD" dll in the project.
class Program
{
static dynamic acApp;
static dynamic doc;
static dynamic modelSpace;
static void Main(string[] args)
{
if (AcadConnection())
{
object pickedObject = null;
object pickPoint = new object[3];
doc.Utility.GetEntity(out pickedObject, out pickPoint, "Sélectionnez un objet texte :");
if (pickedObject != null)
{
// TODO
}
}
}
static bool AcadConnection()
{
const string strProgId = "AutoCAD.Application";
try
{
acApp = (dynamic)ExMarshal.GetActiveObject(strProgId);
}
catch
{
try
{
acApp = (dynamic)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch
{
Console.WriteLine("CannotCreateAnInstanceAutoCADApplication");
return false;
}
}
if (acApp != null)
{
string version = acApp.Version;
try
{
while (true)
{
try { acApp.Visible = true; break; }
catch { }
}
doc = acApp.ActiveDocument;
modelSpace = doc.ModelSpace;
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
return false;
}
}
But at this row:
doc.Utility.GetEntity(out pickedObject, out pickPoint, "Sélectionnez un objet texte :");
I get this error: "Could not convert argument 2 for call to GetEntity."
Could you help me, thank you.
Firstly, in your AcadConnection() method, after getting the instance of an AcadApplication, you may want to test if its ActiveDocument propery is null or not. For example, when the code creates a new instance of running AutoCAD in the "catch..." clause, there could be no document is opened in the AcadApplication instance. If you you may want to let your code to open one (either a new drawing, or an existing drawing file).
As for the issue in hand, try to pass in "dynamic" as the arguments, not "object". You also want to handle the case when user hit Esc to cancel the picking, which raises an exception. So, you may want to try this:
dynamic pickedObject;
dynamic pickPoint;
try
{
doc.Utility.GetEntity(out pickedObject, out pickPoint, "\nSelect xxxxx:");
}
catch
{
pickedObject = null;
}
if (pickedObject == null)
{
// user cancelled the picking, you want to exit here
return;
}
// Do something with the picked entity ...
That's not the problem, as I was saying, it's the GetEntity row that's giving me an error. If I reference the Interop.AutoCAD dll, this code works, but if I use dynamic without referencing the dll, it doesn't work. I'll provide an example of 2 projects, one with a reference and one without.
In my example, I have an open AutoCAD instance and an open document.
You've declared pickPoint as an object, but you assigned an array to it. To set values of an array, you have to set each element separately, but the Utility.GetEntity method attempts to assign a 3D point to a variant. So, in this case, it isn't necessary to assign a value when you declare the variable. Just leave it empty like you did for pickedObject. The GetEntity method will take care of setting the variable.
Can't find what you're looking for? Ask the community or share your knowledge.