Question

I am using visual studio 2010 and I am having a .DWG file which I want to open in autocad. Till now I have used this.

Process p = new Process();
ProcessStartInfo s = new ProcessStartInfo("D:/Test File/" + fileName);
p.StartInfo = s;
p.Start();

But what I want is to close the file inside the Autocad but not the autocad itself. (Means atocad.exe should be kept running).

Till now I hve used this but its closing the acad.exe not the file.

foreach (Process Proc in Process.GetProcesses())
{
    if (Proc.ProcessName.Equals("acad"))
    {
        Proc.CloseMainWindow();
        Proc.Kill();
    }
}
Was it helpful?

Solution 6

To perform the closing of file, best way out is to follow the steps at this ObjectARX SDK for c# and change the following code with the below code.

            [CommandMethod("CD", CommandFlags.Session)]
            static public void CloseDocuments()
            {
                DocumentCollection docs = Application.DocumentManager;
                foreach (Document doc in docs)
                {
                    // First cancel any running command
                    if (doc.CommandInProgress != "" &&
                        doc.CommandInProgress != "CD")
                    {
                        AcadDocument oDoc =
                          (AcadDocument)doc.AcadDocument;
                        oDoc.SendCommand("\x03\x03");
                    }

                    if (doc.IsReadOnly)
                    {
                        doc.CloseAndDiscard();
                    }
                    else
                    {
                        // Activate the document, so we can check DBMOD
                        if (docs.MdiActiveDocument != doc)
                        {
                            docs.MdiActiveDocument = doc;
                        }
                        int isModified =
                          System.Convert.ToInt32(
                            Application.GetSystemVariable("DBMOD")
                          );

                        // No need to save if not modified
                        if (isModified == 0)
                        {
                            doc.CloseAndDiscard();
                        }
                        else
                        {
                            // This may create documents in strange places
                            doc.CloseAndSave(doc.Name);
                        }
                    }
                }

OTHER TIPS

Take the Autocad .NET libraries from Autodesk Sites (http://usa.autodesk.com/adsk/servlet/index?id=773204&siteID=123112)

Then you will be able to use Application and Document classes. They will give you full control over opening and closing documents within the application.

You can find many articles on that, and can ask further questions.

AutoCAD does have an api. there are 4 assemblys. Two for in-process and two for COM.

inprocess : acdbmgd.dll acmgd.dll

COMInterop : Autodesk.Autocad.Interop.dll Autodesk.Autocad.Interop.Common.dll

this is a method that will open a new instance of AutoCAD or it will connect to an existing running instance of AutoCAD.

you will need to load these .dlls into your project references.

using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace YourNameSpace {

public class YourClass {

AcadApplication AcApp;
private const string progID = "AutoCAD.Application.18.2";// this is AutoCAD 2012 program id
private string profileName = "<<Unnamed Profile>>";
private const string acadPath = @"C:\Program Files\Autodesk\AutoCAD 2012 - English\acad.exe";

public void GetAcApp()
    {
        try
        {
            AcApp = (AcadApplication)Marshal.GetActiveObject(progID);

        } catch {
            try {
                var acadProcess = new Process();
                acadProcess.StartInfo.Arguments = string.Format("/nologo /p \"{0}\"", profileName);
                acadProcess.StartInfo.FileName = (@acadPath);
                acadProcess.Start();
                while(AcApp == null)
                {
                    try { AcApp = (AcadApplication)Marshal.GetActiveObject(progID); }
                    catch { }
                }
            } catch(COMException) {
                MessageBox.Show(String.Format("Cannot create object of type \"{0}\"",progID));
            }
        }
        try {
            int i = 0;
            var appState = AcApp.GetAcadState();
            while (!appState.IsQuiescent)
            {
                if(i == 120)
                {
                    Application.Exit();
                }
                // Wait .25s
                Thread.Sleep(250);
                i++;
            }
            if(AcApp != null){
                // set visibility
                AcApp.Visible = true;
            }
        } catch (COMException err) {
            if(err.ErrorCode.ToString() == "-2147417846"){
                Thread.Sleep(5000);
            }
        }
    }
    }
}

closeing it is as simple as

Application.Exit();

and forgive the code. its atrocious, this was one of my first methods when i just started developing...

I doubt you will be able to do this unless AutoCAD has an API that you can hook into and ask it to close the file for you.

Your c# app can only do things to the process (acad.exe) , it doesn't have access to the internal operations of that process.

Also, you shouldn't use Kill unless the process has become unresponsive and certainly not immediately after CloseMainWindow.

CloseMainWindow is the polite way to ask an application to close itself. Kill is like pulling the power lead from the socket. You aren't giving it the chance to clean up after itself and exit cleanly.

There is one other possibility - this will only work if your C# code is running on the same machine as the AutoCAD process and it is not really recommended, but, if you are really stuck and are prepared to put up with the hassle of window switching you can send key strokes to an application using the SendKeys command.

MSDN articles here: http://msdn.microsoft.com/EN-US/library/ms171548(v=VS.110,d=hv.2).aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

Using this you could send the key strokes to simulate the user using the menu commands to close the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top