Ho cercato una buona ora e non ho ancora trovato qualcosa che possa essere d'aiuto. Sto lavorando per aprire AutoCAD dall'API .NET in VS2013 utilizzando C#, ma per qualche motivo, non riesco mai ad avviare AutoCAD. Sto utilizzando il seguente codice:Come posso aprire AutoCAD 2015 tramite l'API .NET
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 so it will run with any version
const string strProgId = "AutoCAD.Application";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch //// STOPS HERE
{
// If an instance of AutoCAD is not created then message and exit
// NOTE: always shows this box and never opens AutoCAD
System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
" could not be created.");
return;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
" version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your 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");
}
}
Purtroppo, si ferma sempre al nidificato catch
dichiarazione e visualizza sempre la finestra di popup senza aprire AutoCAD. Qualche suggerimento su come rendere almeno AutoCAD aperto per me?
EDIT: Messaggio di errore
Non sei molto preciso su quale sia il problema esatto. Ad ogni modo, un paio di suggerimenti: cambia le istruzioni "catch" in "catch (Exception e1)" e "catch (Exception e2)". Eseguire il programma sotto il debugger di Visual Studio e inserire i breakpoint sulla prima istruzione in ogni clausola catch. Quando viene rilevata l'eccezione dovresti essere in grado di vedere il tipo esatto dell'eccezione, e questo dovrebbe (si spera) darti qualche indizio. – RenniePet
@RenniePet Sì, l'ho capito e ho incluso l'output dell'errore. – Archer
È sul primo o secondo fermo? – RenniePet