马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
http://www.cppblog.com/mzty/archive/2008/06/17/53649.html
CSharp启动AutoCAD
一 我们可以通过AutoCAD安装以后提供的COM接口启动AutoCAD。
COM组件为:
AutoCAD/ObjectDBX Common 17.1 Type Library
(Autodesk.AutoCAD.Interop.Common.dll).
AutoCAD 2009 Type Library (Autodesk.AutoCAD.Interop.dll)
- public static void Way1()
- {
- const string progID = "AutoCAD.Application.17.1";
- AcadApplication acApp = null;
- try
- {
- acApp = (AcadApplication)Marshal.GetActiveObject(progID);
- }
- catch
- {
- try
- {
- Type acType = Type.GetTypeFromProgID(progID);
- acApp = (AcadApplication)Activator.CreateInstance(acType,true);
- }
- catch
- {
- MessageBox.Show("Cannot create object of type "" +progID + """);
- }
- }
- if (acApp != null)
- {
- // By the time this is reached AutoCAD is fully
- // functional and can be interacted with through code
- acApp.Visible = true;
- acApp.ActiveDocument.SendCommand("_MYCOMMAND ");
- }
- }
复制代码
- public static void Way2()
- {
- const string progID = "AutoCAD.Application.17.1";
- const string exePath = @"E:\Program Files\Autodesk\ACADM 2009\acad.exe";
- AcadApplication acApp = null;
- // Let's first check we don't have AutoCAD already running
- try
- {
- acApp =(AcadApplication)Marshal.GetActiveObject(progID);
- }
- catch { }
- if (acApp != null)
- {
- MessageBox.Show("An instance of AutoCAD is already running.");
- }
- else
- {
- try
- {
- // Use classes from the System.Diagnostics namespace
- // to launch our AutoCAD process with command-line
- // options
- ProcessStartInfo psi = new ProcessStartInfo(exePath, "/p myprofile");
- psi.WorkingDirectory = @"c:\temp";
- Process pr = Process.Start(psi);
- // Wait for AutoCAD to be ready for input
- // This doesn't wait until AutoCAD is ready
- // to receive COM requests, it seems
- pr.WaitForInputIdle();
- // Connect to our process using COM
- // We're going to loop infinitely until we get the
- // AutoCAD object.
- // A little risky, unless we implement a timeout
- // mechanism or let the user cancel
- while (acApp == null)
- {
- try
- {
- acApp = (AcadApplication)Marshal.GetActiveObject(progID);
- }
- catch
- {
- // Let's let the application check its message
- // loop, in case the user has exited or cancelled
- Application.DoEvents();
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Cannot create or attach to AutoCAD object: "+ ex.Message);
- }
- }
- if (acApp != null)
- {
- acApp.Visible = true;
- acApp.ActiveDocument.SendCommand("_MYCOMMAND ");
- }
- }
|