|
|
2
1
从连接到
AutoDesk
关于这个主题的论坛,听起来您需要在保存后关闭对象…并删除空值…如果我是您,我会将代码包装为
我不得不质疑using子句的用法,因为你
using (AutoCad cad = AutoCad.Instance)
{
try{
// Launch AutoCAD
cad.Launch();
// Open drawing
cad.OpenDrawing(@"C:\Scratch\Drawing01.dwg");
// Save it
cad.SaveAs(@"C:\Scratch\Drawing01B.dwg");
}catch(COMException ex){
// Handle the exception here
}
}
public void Launch()
{
this._acadApplication = null;
const string ProgramId = "AutoCAD.Application.18";
try
{
// Connect to a running instance
this._acadApplication = (AcadApplication)Marshal.GetActiveObject(ProgramId);
}
catch (COMException)
{
/* No instance running, launch one */
try
{
this._acadApplication = (AcadApplication)Activator.CreateInstance(
Type.GetTypeFromProgID(ProgramId),
true);
}
catch (COMException exception)
{
// Failed - is AutoCAD installed?
throw new AutoCadNotFoundException(exception);
}
}
/* Listen for the events we need and make the application visible */
this._acadApplication.BeginOpen += this.OnAcadBeginOpen;
this._acadApplication.BeginSave += this.OnAcadBeginSave;
this._acadApplication.EndOpen += this.OnAcadEndOpen;
this._acadApplication.EndSave += this.OnAcadEndSave;
#if DEBUG
this._acadApplication.Visible = true;
#else
this._acadApplication.Visible = false;
#endif
// Get the active document
// this._acadDocument = this._acadApplication.ActiveDocument;
// Comment ^^^ out? as you're instantiating an ActiveDocument below when opening the drawing?
}
public void OpenDrawing(string path)
{
try{
// Request AutoCAD to open the document
this._acadApplication.Documents.Open(path, false, null);
// Update our reference to the new document
this._acadDocument = this._acadApplication.ActiveDocument;
}catch(COMException ex){
// Handle the exception here
}
}
public void SaveAs(string fullPath)
{
try{
this._acadDocument.SaveAs(fullPath, null, null);
}catch(COMException ex){
// Handle the exception here
}finally{
this._acadDocument.Close();
}
}
我想我会为你提供一些链接。
希望这有帮助, 最好的问候, 汤姆。 |
|
|
3
0
我已经设法以一种非最优的、非常不完美的方式解决了这个问题,所以我仍然有兴趣知道是否有人知道为什么saveas方法会崩溃autocad并挂起我的应用程序。 我是这样做的: 打开文档或创建新文档时,关闭“打开/保存”对话框:
保存文档时,发出以“2010”作为格式和文件名(fullpath)传递的\u saveas命令:
退出AutoCAD“打开文件”对话框时,再次提示(可能不需要,但只需确保):
|
|
|
4
0
对于C和COM,当存在可选参数时,需要使用
但自Visual Studio 2010以来,您只需省略可选参数:
|