本文介绍了如何使用 C# 关闭 Autocad 中的文件以保持 acad.exe 运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是visual studio 2010,我有一个.DWG 文件,我想在autocad 中打开它.到现在我都用过这个.

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();

但我想要的是关闭 Autocad 内的文件而不是 autocad 本身.(意味着atocad.exe应该保持运行).

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

直到现在我都使用过它,但它关闭了 acad.exe 而不是文件.

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();
    }
}

推荐答案

要关闭文件,最好的方法是按照这里的步骤操作 ObjectARX SDK for c# 并使用以下代码更改以下代码.

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("x03x03");
                    }

                    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);
                        }
                    }
                }

这篇关于如何使用 C# 关闭 Autocad 中的文件以保持 acad.exe 运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:29