我使用的是visual studio 2010,我有一个想要在autocad中打开的.DWG文件。到目前为止,我一直在使用这个。
Process p = new Process();
ProcessStartInfo s = new ProcessStartInfo("D:/Test File/" + fileName);
p.StartInfo = s;
p.Start();但我想要的是关闭Autocad内部的文件,而不是autocad本身。(意味着atocad.exe应该保持运行)。
到目前为止,我已经使用了这个,但是它关闭了acad.exe,而不是文件。
foreach (Process Proc in Process.GetProcesses())
{
if (Proc.ProcessName.Equals("acad"))
{
Proc.CloseMainWindow();
Proc.Kill();
}
}发布于 2013-08-13 15:20:28
要关闭文件,最好的方法是按照此ObjectARX SDK for c#中的步骤进行操作,并使用以下代码更改以下代码。
[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);
}
}
}发布于 2013-07-19 20:45:51
从Autodesk站点(http://usa.autodesk.com/adsk/servlet/index?id=773204&siteID=123112)获取Autocad .NET库
然后,您将能够使用Application和Document类。它们将使您能够完全控制在应用程序中打开和关闭文档。
你可以找到许多关于这方面的文章,并可以提出更多的问题。
发布于 2013-07-20 08:19:59
AutoCAD确实有一个应用程序接口。有4个部件。两个用于进程内,两个用于COM。
进程中: acdbmgd.dll acmgd.dll
COMInterop : Autodesk.Autocad.Interop.dll Autodesk.Autocad.Interop.Common.dll
这是一个方法,它将打开一个新的AutoCAD实例,或者它将连接到现有的正在运行的AutoCAD实例。
您需要将这些.dlls加载到您的项目引用中。
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);
}
}
}
}
}关闭它就像这样简单
Application.Exit();原谅我的代码。这太可怕了,这是我刚开始开发时最早的方法之一……
https://stackoverflow.com/questions/17741728
复制相似问题