Hello Everyone,
I am new to programming AutoCad using .Net. I am trying to create a standalone C#/.Net Application to manipulate the dwg files.
While creating an instance of AcadApplication using following code, I am getting a crash which I can't comprehend completely.
Type acType = Type.GetTypeFromProgID("AutoCAD.Application.18.1");
AcadApplication acApp = null; acApp = (AcadApplication)Activator.CreateInstance(acType,true);
I get the followng error message.
Unable to cast COM object of type 'Autodesk.AutoCAD.Interop.AcadApplicationMinorVersion1Class' to interface type 'AutoCAD.AcadApplication'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{070AA05D-DFC1-4E64-8379-432269B48B07}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Although, I can see an instance of acad.exe getting started in taskmanager, but value of acApp is still null.
Other informations:
Visual Studio Version : VS 2012 Express Edition
AutoCaD Version : AutoCAD 2012 x64 version
OS : Windows 7, x64 version.
Thanks in advance,
Durgesh
To avoid referencing a specific AutoCAD version, you can try late binding.
This works for any version of AutoCAD, 32 or 64bit:
public static List<String> GetOpenFiles() { List<String> result = new List<String>(); try { // retrieve running AutoCAD, generic class Object acadApp = Marshal.GetActiveObject("AutoCAD.Application"); if (acadApp == null) return null; // Recupera active document list object objDoc = acadApp.GetType().InvokeMember("Documents", BindingFlags.IgnoreCase | BindingFlags.GetProperty, null, acadApp, null); if (objDoc == null) return null; Int64 iCount = Convert.ToInt64(objDoc.GetType().InvokeMember("Count", BindingFlags.IgnoreCase | BindingFlags.GetProperty, null, objDoc, null)); for (int i = 0; i < iCount; i++) { object[] dataArray = new object[1]; dataArray[0] = i; object obj = objDoc.GetType().InvokeMember("Item", BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, null, objDoc, dataArray); if (obj != null) { object objName = obj.GetType().InvokeMember("FullName", BindingFlags.IgnoreCase | BindingFlags.GetProperty, null, obj, null); String szName = objName.ToString(); //object objRead = obj.GetType().InvokeMember("ReadOnly", BindingFlags.IgnoreCase | BindingFlags.GetProperty, null, obj, null); //bool bReadOnly = Convert.ToBoolean(objRead); if (szName.Length > 0) result.Add(szName); } } } catch (System.Exception ex) { result = null; } return result; }
Hi,
I better switch back to VB6 for AutoCAD Programming. Late binding is looking very scary and difficult to implement.
Anyways Thanks, Is there any way I can implement it as COM Interop.
Regards,
Durgesh
Can't find what you're looking for? Ask the community or share your knowledge.