Thursday, 15 August 2013

c# - How can I open AutoCAD 2015 through the .NET API -



c# - How can I open AutoCAD 2015 through the .NET API -

i've been browsing hr , have yet find help this. i'm working on opening autocad .net api in vs2013 using c#, reason, can never autocad launch. i'm using next code:

using system; using system.runtime.interopservices; using autodesk.autocad.interop; using autodesk.autocad.runtime; using autodesk.autocad.applicationservices; namespace ioautocadhandler { public static class acaddocumentmanagement { [commandmethod("connecttoacad")] public static void connecttoacad() { acadapplication acappcomobj = null; // no version number run version const string strprogid = "autocad.application"; // running instance of autocad seek { acappcomobj = (acadapplication)marshal.getactiveobject(strprogid); } grab // error occurs if no instance running { seek { // create new instance of autocad acappcomobj = (acadapplication)activator.createinstance(type.gettypefromprogid(strprogid), true); } grab //// stops here { // if instance of autocad not created message , exit // note: shows box , never opens autocad system.windows.forms.messagebox.show("instance of 'autocad.application'" + " not created."); return; } } // display application , homecoming name , version acappcomobj.visible = true; system.windows.forms.messagebox.show("now running " + acappcomobj.name + " version " + acappcomobj.version); // active document acaddocument acdoccomobj; acdoccomobj = acappcomobj.activedocument; // optionally, load assembly , start command or if assembly // demandloaded, start command of in-process assembly. acdoccomobj.sendcommand("(command " + (char)34 + "netload" + (char)34 + " " + (char)34 + @"c:\users\administrator\documents\all code\main-libraries\ioautocadhandler\bin\debug\ioautocadhandler.dll" + (char)34 + ") "); acdoccomobj.sendcommand("drawcomponent"); } }

unfortunately, stops @ nested catch statement , displays popup box without opening autocad. suggestions on how @ to the lowest degree create autocad open me?

edit: error message

the issue you're coding (correctly) autocad interop interface. recommend against (due potential version changes).

the other issue documentation autocad plugins using newer .net api plugins when autocad running.

final issue programme id of autcad mystery. have resorted making configurable setting, default "autocad.application", take registered autocad.application on production machine. if there multiple versions installed on machine , want specific, append version number (which you'll need research) progid like: "autocad.application.19", or "autocad.application.20" 2015.

for first issue, 1 technique utilize dynamics autocad objects, particularly creating instances. have used objectarx api creating application in dummy project, , switching dynamics when i'm happy properties , method names.

in standalone .net application starts autocad utilize like:

// comment these out in production //using autodesk.autocad.interop; //using autodesk.autocad.interop.common; //... //private static acadapplication _application; private static dynamic _application; static string _autocadclassid = "autocad.application"; private static void getautocad() { _application = marshal.getactiveobject(_autocadclassid); } private static void startautocad() { var t = type.gettypefromprogid(_autocadclassid, true); // create new instance autocad. var obj = activator.createinstance(t, true); // no need casting dynamics _application = obj; } public static void ensureautocadisrunning(string classid) { if (!string.isnullorempty(classid) && classid != _autocadclassid) _autocadclassid = classid; log.activity("loading autocad: {0}", _autocadclassid); if (_application == null) { seek { getautocad(); } grab (comexception ex) { seek { startautocad(); } grab (exception e2x) { log.error(e2x); throwcomexception(ex); } } grab (exception ex) { throwcomexception(ex); } } }

c# .net visual-studio-2013 autocad

No comments:

Post a Comment