C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

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


        }

四 当然也可以使用C++去调用COM接口。

posted on 2008-06-17 08:46 梦在天涯 阅读(5438) 评论(0)  编辑 收藏 引用 所属分类: ARX/DBX


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1797009
  • 排名 - 5

最新评论

阅读排行榜