以管理员身份启动CAD控件 \Bin\vc100\RegistMxDrawX.exe ,以向系统注册控件。
运行 Delphi ,导入CAD控件的类型库,运行菜单命令,将弹出如下的对话框:
类型库导入对话框:
点击 CreateUnit 按钮,对导入的CAD控件接口创建的代码定义:
点击“ Install ”按钮把CAD控件组件安装到Delphi中去。完成安装后,可以在控制面板看到CAD控件的组件:
新建一个Delphi工程。
在对话框中,放入CAD控件:
增加一个按钮并为它添加按钮点击事件,在事件函数中添加如下代码:
procedure TForm1.Button1Click(Sender: TObject);
var
modleSpace : IMxDrawBlockTableRecord ;
newIterator : IMxDrawBlockTableRecordIterator ;
pEnt : IMxDrawEntity;
pBlkRef : IMxDrawBlockReference;
pAttribute : IMxDrawAttribute;
tag : WideString;
text : WideString;
ii : Integer;
begin
// 得到当前图形空间对象
modleSpace := MxDrawApplication1.WorkingDatabase.CurrentSpace ;
// 生成一个图纸空间对象浏览器
newIterator := modleSpace.NewIterator ;
// 遍历浏览器,得到每个实体
if newIterator <> nil then
begin
while newIterator.Done() = False do
begin
// 到实体对象
pEnt := newIterator.GetEntity();
newIterator.Step(True,False);
if pEnt <> nil then
begin
// 把实体对象转成块引用对象
pEnt.QueryInterface(IMxDrawBlockReference, pBlkRef);
if pBlkRef <> nil then
begin
// 得到块对象名称
if pBlkRef.GetBlockName() = 'BLKNAME' then
begin
// 遍历块引用对象的属性
for ii := 0 to pBlkRef.AttributeCount -1 do
begin
// 得到属性对象
pAttribute := pBlkRef.AttributeItem(ii);
// 得到属性对象的文字,和tag.
tag := pAttribute.Get_Tag();
text := pAttribute.Get_TextString();
showmessage(tag + ':' + text);
end;
end;
end;
end;
end;
end;
if text = '' then
begin
showmessage('没有发现块名为' + ' BLKNAME ' + '的块实体');
end;
end;编译运行,效果如下:
更多的实例代码在CAD控件 \Src\MxDraw5.2\samples 下可找到。如Delphi7Smaple,Delphi7Smaple2。