如何用AUTOCAD的AUTOMATION技术建立选择集并取出元素进行判断?(100分)

  • 主题发起人 主题发起人 emoth
  • 开始时间 开始时间
E

emoth

Unregistered / Unconfirmed
GUEST, unregistred user!
兄弟看了CAD的帮助半天,用DELPHI编制了一个程序来建立选择集(设想用两条来建立一条篱笆线,让与
篱笆线相交的所有CAD物体全部被选择),却总是通不过,提示是未指定的错误?代码如下:

unit UnitMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,comobj;

type
TForm1 = class(TForm)
butOK: TButton;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Edit3: TEdit;
Label3: TLabel;
butLinkCAD: TButton;
procedure butLinkCADClick(Sender: TObject);
procedure butOKClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses AutoCAD_TLB;

var
CADApp:AcadApplication;
ssetObj:AcadSelectionSet;

{$R *.dfm}

procedure TForm1.butLinkCADClick(Sender: TObject);
//与CAD建立链接
begin
try
cadapp:=GetActiveOleObject('AutoCAD.Application.15') as AcadApplication;
ShowMessage(cadapp.Version);
except
ShowMessage('Error!');
end;
end;

procedure TForm1.butOKClick(Sender: TObject);
//建立CAD选择集
var
Pnt,S:oleVariant;
returnObj:OleVariant;
ssFilterType,ssFilterData:OleVariant;
I:Integer;
begin
try
Pnt:=VarArrayCreate([0, 5], varDouble);
pnt[0]:=151;
pnt[1]:=336;
pnt[2]:=0;
pnt[3]:=425;
pnt[4]:=-32;
pnt[5]:=0;
ssFilterType:=0;
ssFilterData:='ABC';
try
① ssetObj:=cadapp.ActiveDocument.SelectionSets.Add('MySelect');
② ssetObj.SelectByPolygon(acSelectionSetFence,pnt,ssFilterType,ssFilterData);
I:=ssetObj.Count;
ShowMessage(IntToStr(I));
finally
③ ssetObj.Erase;
end;
except
on e:Exception do
ShowMessage(E.Message);
end;
end;

end.

第一次程序执行到①时正常,建立一个选择集。但执行到②时就提示出错!然后执行③句正常。照理说③句处
程序已将建立的名为MySelect的选择集删除了,可是以后程序执行到①时就再也执行不下去,提示也是未指定
的错误?!

我用的是CAD2000英文版和DELPHI6.0,请各位富翁指正?
 
Erases all the objects in a selection set

这是 AutoCAD帮助文档里对, Erases 功能的描述。它只是删除所有 指定选择集中的
Object ,并没有把 选择集删除,当你第二次运行时,因为指定的"MySelect"已经存在,
这时当然就不能创建了,出现错误也是再所难免。

如果是要删除 Object 的话,可以用 Object 的 Delete 方法。 Object 包含了,选择集
ACadSelectionSet ..

今天都要放假了,等国庆后,跟你这位自贡的兄弟切磋一下,关于AutoCAD 的问题。:)
 
Darnis您好 ^_^
我试着用VB编写了一个程序,代码如下:

Private Sub Command1_Click()
Dim CAD As AcadApplication
Dim ssetObj As AcadSelectionSet

Set CAD = GetObject(, "Autocad.application.15")
Set ssetObj = CAD.ActiveDocument.SelectionSets.Add("TEST_SSET")

Dim mode As Integer
Dim pointsArray(0 To 5) As Double
mode = acSelectionSetFence

pointsArray(0) = 151: pointsArray(1) = 336: pointsArray(2) = 0
pointsArray(3) = 425: pointsArray(4) = -32: pointsArray(5) = 0

ssetObj.SelectByPolygon mode, pointsArray

MsgBox ssetObj.Count

ReDim gpCode(0) As Integer
gpCode(0) = 8
ReDim dataValue(0) As Variant
dataValue(0) = "DGX"

Dim groupCode As Variant, dataCode As Variant
groupCode = gpCode
dataCode = dataValue

ssetObj.SelectByPolygon mode, pointsArray, groupCode, dataCode

MsgBox ssetObj.Count

ssetObj.Clear

End Sub

可以调试通过,不过我发现用不用过滤条件都是一样的,选择出的物体个数都是11个!

那么为什么DELPHI不可以呢?

请指教!
 
:)
ReDim gpCode(0) As Integer
gpCode(0) = 8
ReDim dataValue(0) As Variant
dataValue(0) = "DGX"

你这里定义的过滤条件是选择只在DGX图层上的对象,那你如果指定范围内的所有对象
都是在DGX图层上的话,那你这个过滤条件可能就跟没有一样了。。:)

FilterType FilterData
0 Object Type (String)
Such as "Line," "Circle," "Arc," and so forth.
2 Object Name (String)
The table (given) name of a named object.
8 Layer Name (String)
Such as "Layer 0."
60 Object Visibility (Integer)
Use 0 = visible, 1 = invisible.
62 Color Number (Integer)
Numeric index values ranging from 0 to 256.
Zero indicates the BYBLOCK. 256 indicates BYLAYER. A negative value indicates that the layer is turned off.
67 Model/paper space indicator (Integer)
Use 0 or omitted = model space, 1 = paper space

The filter arguments are declared as arrays. The filter type is declared
as an integer and the filter value as a variant.
Each filter type must be paired with a filter value. For example:

FilterType(0) = 0 'Indicates filter refers to an object type
FilterData(0) = "Circle" 'Indicates the object type is "Circle"


不知你在Delphi有没有能够正确做选择?我试了N多把了,都报告我说那个
FilterType 参数没有正确。这是怎么回事?再多找找资料…………
 
可是我在DXG图层里只有7个对象啊,其它4个分属不同的图层!
在网上找到个“日文”(??)网站,提供的DELPHI代码如下:可是我照样无法通过编译,譬如:

丂pts:=Icad.Library.CreatePoints;
是什么意思?CAD没有这个CreatePoints的方法啊,请各位富翁还多多帮忙,指点指点如何
用DELPHI建立CAD选择集。

真是气愤,VB可以做的事,DELPHI不能做!!!

下面为“日文”网站提供的代码:

儐乕僓乕偵傛傜側偄恾宍偺慖戰
ICAD.ActiveDocument.SelectionSets.Add(Name As String) As SelectionSet
SelectionSet.SelectByPolygon(Type As SelectionSetType, PolygonPoints As Points, [FilterType], [FilterData]);
SelectionSet.Select(type As SelectionSetType, [Point1 As Point], [Point2 As Point], [FilterType], [FilterData]);
SelectionSet.SelectAtPoint(Point As Point, [FilterType], [FilterData])
uses ComObj;

var
丂Icad,pts,pt1,pt2.OLEVarinat;
丂SS:OLEVarinat;
丂i:Integer;
begin
丂Icad:=GetActiveOleObject('ICAD.Application');

丂//嬻偺僙儗僋僔儑儞僙僢僩傪梡堄偡傞
丂SS:=ICAD.ActiveDocument.SelectionSets.Add('TEST')丟

丂//億儕僑儞偱慖戰
丂pts:=Icad.Library.CreatePoints;
丂pts.Add(0,0,0,1);
丂pts.Add(0,297,0,1);
丂pts.Add(420,297,0,1);
丂pts.Add(420,0,0,1);
丂//0=CrossingPolygon,1=OutsidePolygon,2=InsidePolygon
丂SS.SelectByPolygon(0,pts);

丂//憢偱慖傇
丂pt1:=Icad.Library.CreatePoint(0,0,0);
丂pt2:=Icad.Library.CreatePoint(420,297,0);
丂//6=CrossingWindow,7=OutsideWindow,8=InsideWindow
丂SS.Select(6,pt1,pt2);

丂//捈慜偺慖戰僙僢僩傪慖戰
丂pt1:=Icad.Library.CreatePoint(0,0,0);
丂pt2:=Icad.Library.CreatePoint(420,297,0);
丂//pt1,pt2偼柍帇偝傟傞偑丄徣棯偱偒側偄
丂SS.Select(9,pt1,pt2);

丂//嵟屻偺恾宍傪慖戰
丂pt1:=Icad.Library.CreatePoint(0,0,0);
丂pt2:=Icad.Library.CreatePoint(420,297,0);
丂//pt1,pt2偼柍帇偝傟傞偑丄徣棯偱偒側偄
丂SS.Select(10,pt1,pt2);

丂//偡傋偰傪慖戰
丂pt1:=Icad.Library.CreatePoint(0,0,0);
丂pt2:=Icad.Library.CreatePoint(420,297,0);
丂//pt1,pt2偼柍帇偝傟傞偑丄徣棯偱偒側偄
丂SS.Select(11,pt1,pt2);

丂//慖戰偝傟偨恾宍傪僴僀儔僀僩
丂SS.Hightlight(True);

丂//慖戰偝傟偨恾宍偺悢
丂Edit1.Text:=IntToStr(SS.Count);

丂//慖戰偟偨恾宍傪庢傝弌偡
丂Memo1.Lines.Clear;
丂for i:=1 to SS.Count do begin
丂丂Memo1.Lines.Add(SS.Items(i).EntityName);
丂end;
end;

仸SelectionSetType12乮僼僃儞僗乯偼惉岟偟偰偄側偄
IntelliCAD_TLB傪巊偆帪偼丄僆僾僔儑儞偱偁傞僼傿儖僞乕偼昁梫丅
僼傿儖僞乕偺拞恎偼柍帇偝傟傞偺偱Variant宆愰尵偩偗偱拞恎偼側偔偰傕椙偄丅

var
丂filType,filData:Variant;
丂ipt1,ipt2:IICADPoint;
偲偟偰
丂iSS.Select(11,ipt1,ipt2,filType,filData);
偲偡傞偲丄偡傋偰偺恾宍偑慖戰偝傟傞丅

僼傿儖僞乕偼柍帇偝傟傞傛偆偩偑丄
僙僢僩偡傞応崌偼丄壓婰偺傛偆VarArrayCreate傪巊偆偺偱偼側偄偐偲巚偆丅
丂filType:=VarArrayCreate([0,2],varInteger);
丂filType[0]:=1;
丂...
 
为什么VB可以做的事DELPHI不能做呢?
对于AUTOCAD而言,其AUTOMATION接口是一样的啊!
 
很久没见到你了,我又去发了一篇名为“Active Automation:- DELPHI不敌VB????”
的帖子,有空去看看吧。
这个帖子我就把它关闭了吧。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
629
import
I
后退
顶部