C++ Builder开发AutoCAD应用程序的方法

※ 编程技巧 应用实践 ※

C++ Builder开发AutoCAD应用程序的方法

周永军(水利部山西水利水电勘测设计研究院  030024)

【摘要】进行AutoCAD二次开发的程序设计方法为数众多,本文着重介绍用C++ Builder方法开发AutoCAD应用程序的一种通用方法。用这种方法开发的应用程序移植性较好,且不需要其它辅助系统支持,应用方便快捷。本文介绍的程序设计方法是目前较为流行的ActiveX组件方法,在C++ Builder中也叫做组件对象模型(Component Object Model)。

【关键字】COM程序设计 ActiveX接口 类型库 C++ Builder  AutoCAD

1 C++ Builder开发平台简介

C++ Builder是美国Borland公司在1997年推出的一个优秀的可视化编程环境。它在32位Windows环境下为我们提供了一种极具吸引力的快速应用程序开发系统(RAD);是基于最流行的面向对象的程序设计语言C++,并拥有广泛的程序员基础;它提供了对ActiveX无懈可击的支持,对Windows API和DirectX也进行了全面包装。C++ Builder的易用性远超过Visual C++和Borland C++等开发环境,但它强大的开发功能并没有因之而削弱,可以说C++ Builder在软件开发的功能性和易用性之间架起了一座桥梁。

C++Builder是在Borland C++和Delphi基础上发展起来的。它采用具世界领先水平的Borland C++的编译器和高度可视化的图形平台,可开发出非常高效和可独立执行的程序。与AutoCAD其它开发工具相比,可使程序员摆脱AutoLISP繁多括号的困扰和解释执行的低效率,也可开发出比VBA程序更高效和保密性更强的二进制代码。

C++ Builder具有代表性的版本是3.0和5.0版。2002年2月推出的6.0版是目前的最新版本。本文介绍的程序开发是基于5.0版本。

2 AutoCAD版本的发展

AutoDesk公司1996年推出AutoCAD R14版本,使得AutoCAD成为标准Windows应用程序。AutoCAD 2000版本开始正式成为Microsoft标准的多文档管理程序。另一个特征是嵌入了VBA(Visual Basic Application),使AutoCAD的开发和定制功能大大增强。2002和2003版本主要是使AutoCAD在易用性和网络功能方面的功能得到加强。

3 为什么选用C++ Builder开发AutoCAD应用程序

本文推荐使用C++ Builder开发AutoCAD应用程序主要原因如下。

(1) C++ Builder开发平台是非常易用且功能强大,并有为数众多的图形化组件,可以满足我们日常的编程的需要,可以大大提高程序员编程效率。

(2) C++ Builder开发的应用程序可以脱离C++ Builder开发平台和AutoCAD应用平台而独立运行,不象VBA程序要依赖于某个AutoCAD的图形文件,使用时需要加载和卸载。

(3) C++ Builder应用程序是真正的编译程序,执行程序短小,执行效率高,方便在网络间传送,且保密性强。

另一个外在原因,目前学习和使用C/C++语言的人非常普遍,而LISP语言和VBA语言的使用并不十分普及,所以用C++ Builder开发AutoCAD应用程序是非常具有优势的。

4 C++ Builder开发AutoCAD应用程序基本方法

C++ Builder开发AutoCAD应用程序的基本方法是采用Microsoft公司的ActiveX Automation技术。该技术是一种可从一个程序内部编程控制另外一个或几个程序的技术,其中作为控制方的程序叫做Automation客户或控制器,被控制方的程序叫做Automation服务器。这里我们所开发的应用程序就是客户程序,而AutoCAD程序就是服务器程序。

本文所使用的开发平台是基于C++ Builder 5,要求AutoCAD为2000以上版本。另外在AutoCAD软件安装目录(默认为“C:/Program Files/AutoCAD 2002/”)下有一个ACAD.TLB文件,该文件是AutoCAD的类型库文件,本文所介绍的程序设计就是依赖这个文件的。

本文介绍一个简单的程序,它的功能仅仅是在AutoCAD模型空间画一个圆。这个程序虽然功能比较简单,但它几乎包括了AutoCAD ActiveX程序设计的全部思想和方法。可以使读者对AutoCAD的ActiveX Automation程序设计有一个直观和快捷的了解。

 

           图1 Imports Type Libray对话框           

 

   图2 程序设计窗体界面

(1) 启动C++Builder。

(2) 保存项目。将窗体文件保存为CadU1.cpp,将项目保存为CadP。这时C++Builder IDE窗口标题栏中出现被保存的项目名称。

(3) 引入类型库。选择Project菜单中“Import Type Libray”子菜单,出现对话框(图1)。

在对话框中列出所有已注册的类型库,从中选择AutoCAD 2000 Type Libray [Version 1.1] (AutoCAD2000的类型库,2002和2003版的类型库同样适用)。如果列表中没有AutoCAD的类型库,说明它还没有被注册,单击[Add]按钮,在AutoCAD的程序目录中找到acad.tlb文件,选中后单击[OK]按钮,随后就在列表中增加了“AutoCAD 2000的类型库”。

选中AutoCAD2000类型库,单击[Create Unit]按钮,稍候系统将自动生成AutoCAD类型库的外套文件及其头文件,默认文件名为AutoCAD_TLB.cpp和AutoCAD_TLB.h。并且自动将外套文件添加到CadP项目中。

(4) 设计窗体中。按照图2所示设计Form1的窗体。选择工具栏中Button按钮,将其放置在Form1的合适位置,系统自动将其命名为Button1,然后将Caption属性改为“启动CAD”;再往窗体中放置一个Button按钮,系统自动命名为Button2,再将其Caption属性改为“画圆”。最后将Form1的Caption属性改为“AutoCAD ActiveX Automation”。设置完成后的窗体界面如图2所示。

(5) 添加代码。选择菜单[View]→[Toggle Form/Unit],或者直接按F12按钮,跳转到代码编辑窗口,在成员函数体定义之前添加以下代码。

IAcadApplicationDisp ICad;          //声明程序对象

双击Button1,系统自动生成名为Button1Click成员函数,光标定位在函数体的空白处,继续添加如下代码:

ICad.BindDefault();                            //连接到服务器程序中

ICad.Visible=True;                       //设置程序对象为可见

双击Button2,系统自动生成名为Button2Click成员函数,光标定位在函数体的空白处,在此添加如下代码:

IAcadDocumentDisp Doc=ICad.ActiveDocument;    //将活动文档传给文档对象

     IAcadModelSpaceDisp Mds=Doc.ModelSpace;      //将模型空间传给其对象

     ACAD_POINT Cen;                           //定义圆心点

    Variant Pt1;                                 //定义Variant临时变量

     double tmp;                                //定义临时变量

    Pt1=VarArrayCreate(OPENARRAY(int,(0,2)),varDouble);     //创建Variant类型数组,数据类型为double

       tmp=200.0;

       Pt1.PutElement(tmp,0);    //给数组第一个分量赋值

       tmp=200.0;

       Pt1.PutElement(tmp,1);    //给数组第二个分量赋值

       tmp=0.0;

       Pt1.PutElement(tmp,2);    //给数组第三个分量赋值

       Cen=Pt1;                             //将临时变量数组赋给圆心点

       Mds.AddCircle(Cen,100.0);   //调用画圆方法在模型空间画圆

       ICad.ZoomAll();                   //更新程序对象

(6) 包含头文件。在File菜单中选择“Include Unit Hdr...”子菜单,出现“Use Unit”对话框,选择AutoCAD_TLB,单击[OK]按钮,系统自动在TForm1的单元文件中增加一行:

#include "AutoCAD_TLB.h"

(7) 编译和连接。选择Project菜单中Make选项,系统进行一系列的编译、连接工作,如果没有错误,将创建执行文件CadP.EXE。

(8) 运行程序。在C++Builder IDE环境中或脱离IDE环境都可运行CadP。我们选择脱离IDE环境方式。在Windows资源管理器中找到CadP.EXE文件(默认路径为工程文件所在的目录),双击则可运行该程序。单击[启动CAD]按钮,稍候将启动AutoCAD程序,然后点击[画圆]按钮,程序将在其模型空间画一个圆。

5 其它问题

在进行专业程序设计时,可以将以上实例中为ACAD_POINT类型的点分量赋值及读取分量的过程设计为一个函数,使用起来就会非常方便。

作者已将本文介绍的程序设计方法应用于多个专业绘图软件的开发,应用情况良好。

对本文中介绍的程序设计方法有兴趣的读者,可以致信于作者本人或工程地质计算机应用协作网(Geocom.hhcc.net.cn)与作者共同讨论,作者的电子信箱地址:zzyyjj@sina.com

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
AutoCAD_TLB.pas文件下载 unit AutoCAD_TLB; // ************************************************************************ // // WARNING // ------- // The types declared in this file were generated from data read from a // Type Library. If this type library is explicitly or indirectly (via // another type library referring to this type library) re-imported, or the // 'Refresh' command of the Type Library Editor activated while editing the // Type Library, the contents of this file will be regenerated and all // manual modifications will be lost. // ************************************************************************ // // PASTLWTR : $Revision: 1.130 $ // File generated on 2006-8-11 16:08:04 from Type Library described below. // ************************************************************************ // // Type Lib: C:\Program Files\AutoCAD 2002\acad.tlb (1) // LIBID: {C094C1E2-57C6-11D2-85E3-080009A0C626} // LCID: 0 // Helpfile: C:\Program Files\AutoCAD 2002\ACADAUTO.CHM // DepndLst: // (1) v2.0 stdole, (C:\WINDOWS\System32\StdOle2.Tlb) // (2) v4.0 StdVCL, (C:\WINDOWS\System32\stdvcl40.dll) // Errors: // Hint: Parameter 'Type' of IAcadBlock.AddLeader changed to 'Type_' // Hint: Member 'Type' of 'IAcadPolygonMesh' changed to 'Type_' // Hint: Parameter 'Type' of IAcadPolygonMesh.Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadPolygonMesh.Type changed to 'Type_' // Hint: Member 'Type' of 'IAcad3DPolyline' changed to 'Type_' // Hint: Parameter 'Type' of IAcad3DPolyline.Type changed to 'Type_' // Hint: Parameter 'Type' of IAcad3DPolyline.Type changed to 'Type_' // Hint: Parameter 'Object' of IAcad3DSolid.CheckInterference changed to 'Object_' // Hint: Parameter 'Object' of IAcadRegion.Boolean changed to 'Object_' // Hint: Parameter 'Type' of IAcadDimension.VerticalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimension.VerticalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAligned.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimAngular.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.CenterType changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.CenterType changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.CenterMarkSize changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.CenterMarkSize changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimDiametric.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRotated.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimOrdinate.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimOrdinate.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimOrdinate.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimOrdinate.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimOrdinate.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimOrdinate.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.CenterType changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.CenterType changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.CenterMarkSize changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.CenterMarkSize changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.FractionFormat changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.LinearScaleFactor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.ArrowheadType changed to 'Type_' // Hint: Parameter 'Type' of IAcadDimRadial.ArrowheadType changed to 'Type_' // Hint: Member 'Type' of 'IAcadLeader' changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.ArrowheadType changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.ArrowheadType changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.VerticalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadLeader.VerticalTextPosition changed to 'Type_' // Hint: Member 'Type' of 'IAcadPolyline' changed to 'Type_' // Hint: Parameter 'Type' of IAcadPolyline.Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadPolyline.Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadTolerance.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadTolerance.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.DimensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.ExtensionLineColor changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.HorizontalTextPosition changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.Arrowhead1Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Type' of IAcadDim3PointAngular.Arrowhead2Type changed to 'Type_' // Hint: Parameter 'Object' of IAcadDictionary.GetName changed to 'Object_' // Hint: Parameter 'Type' of IAcadApplication.Zoom changed to 'Type_' // Hint: Symbol 'System' renamed to 'System_' // Hint: Member 'Type' of 'IAcadMenuGroup' changed to 'Type_' // Hint: Parameter 'Label' of IAcadPopupMenu.AddMenuItem changed to 'Label_' // Hint: Parameter 'Label' of IAcadPopupMenu.AddSubMenu changed to 'Label_' // Hint: Member 'Label' of 'IAcadPopupMenuItem' changed to 'Label_' // Hint: Member 'Type' of 'IAcadPopupMenuItem' changed to 'Type_' // Hint: Member 'Type' of 'IAcadToolbarItem' changed to 'Type_' // Hint: Parameter 'Unit' of IAcadUtility.AngleToReal changed to 'Unit_' // Hint: Parameter 'Unit' of IAcadUtility.AngleToString changed to 'Unit_' // Hint: Parameter 'Unit' of IAcadUtility.DistanceToReal changed to 'Unit_' // Hint: Parameter 'Unit' of IAcadUtility.RealToString changed to 'Unit_' // Hint: Parameter 'Type' of IAcadUtility.CreateTypedArray changed to 'Type_' // Hint: Parameter 'Object' of IAcadUtility.GetEntity changed to 'Object_' // Hint: Parameter 'Object' of IAcadUtility.GetSubEntity changed to 'Object_' // Hint: Parameter 'Object' of _DAcadDocumentEvents.ObjectAdded changed to 'Object_' // Hint: Parameter 'Object' of _DAcadDocumentEvents.ObjectModified changed to 'Object_' // ************************************************************************ // // *************************************************************************// // NOTE:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值