public static void Main(string[] args) { AcadApplication acAppComObj = null; //Query your Regedit Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD to get the correctly suffix that specifies the version const string strProgId = "AutoCAD.Application.20"; // 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 { // If an instance of AutoCAD is not created then message and exit System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created."); return; } } // Display the application if (null != acAppComObj) { try { int i = 0; AcadState appState = app.GetAcadState(); while (!appState.IsQuiescent) { if (i == 120) { Environment.Exit(-1); } // Wait .25s Thread.Sleep(250); i++; } app.Visible = true; var docs = app.Documents; docs.Add("acadiso.dwt"); } catch (COMException err) { if (err.ErrorCode.ToString() == "-2147417846") { Thread.Sleep(5000); } } catch (Exception ex) { throw new Exception("Falha durante a obtenção do documento ativo.", ex); } } else { throw new Exception("Erro to open first document."); } // Open AutoCAD project file, use this code if all DWGs is associated with a AutoCAD Project with Server Database #region ' Open Project ' acDocComObj.SendCommand("FILEDIA", "0"); acDocComObj.SendCommand("-OPENPROJECT", "C:\\\\Users\\<username>\\Documents\\ProjectFolder\\Project.xml"); acDocComObj.SendCommand("FILEDIA", "1"); #endregion string[] dwgFiles = //To do: add here the rule that list all full path DWG files AcadDocuments docs = app.Documents; foreach (string dwgPath in dwgFiles) { docs.Open(dwgPath, true); Thread.Sleep(3000); AcadDocument acadDoc = acAppComObj.ActiveDocument; acDocComObj.SendCommand("FILEDIA", "0"); acadDoc.SendCommand("JPGOUT ", "C:\\\\Users\\<username>\\Images\\" + Path.GetFileName(dwgPath) + ".jpg"); acDocComObj.SendCommand("FILEDIA", "1"); } }
private void Test1(object sender, RoutedEventArgs e) { //Start AutoCAD Process acadProc = new Process(); acadProc.StartInfo.FileName = "C:/Program Files/Autodesk/AutoCAD 2018/acad.exe"; acadProc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; acadProc.Start(); if (!acadProc.WaitForInputIdle(300000)) { throw new ApplicationException("AutoCAD takes too much time to start."); } this.Title = "[Waiting for AutoCAD ...]"; while (true) { try { // Getting running AutoCAD instance by Marshalling by passing Programmatic ID as a string, AutoCAD.Application is the Programmatic ID for AutoCAD. app = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application"); break; } catch (COMException ex) { const uint MK_E_UNAVAILABLE = 0x800401e3; if ((uint)ex.ErrorCode != MK_E_UNAVAILABLE) { throw; } Thread.Sleep(1000); } } //wait for AutoCAD while (true) { try { AcadState state = app.GetAcadState(); if (state.IsQuiescent) { break; } Thread.Sleep(1000); } catch { Thread.Sleep(1000); } } //Create new Document app.Documents.Add(); this.Title = "[Drawing ...]"; //This is the main Drawing Part double[] CenterOfCircle = new double[3]; CenterOfCircle[0] = 5; CenterOfCircle[1] = 5; CenterOfCircle[2] = 0; double RadiusOfCircle = 1; app.ActiveDocument.ModelSpace.AddCircle(CenterOfCircle, RadiusOfCircle); double[] p1 = new double[3] { 6, 5, 0 }; double[] p2 = new double[3] { 7, 5, 0 }; app.ActiveDocument.ModelSpace.AddLine(p1, p2); double[] c2 = new double[3] { 8, 5, 0 }; app.ActiveDocument.ModelSpace.AddCircle(c2, 1); app.ActiveDocument.ModelSpace.AddLine(new double[] { 9, 5, 0 }, new double[] { 10, 5, 0 }); app.ActiveDocument.ModelSpace.AddCircle(new double[] { 11, 5, 0 }, 1); this.Title = "[Saving Output ...]"; SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "AutoCAD Drawing File (*.dwg)|*.dwg|All files (*.*)|*.*"; if (sfd.ShowDialog() == true) { string filename = sfd.FileName; app.ActiveDocument.SaveAs(filename); } this.Title = "[Finalizing ...]"; //Close AutoCAD app.ActiveDocument.Close(); app.Quit(); //Close Me Environment.Exit(0); }