本文将向你介绍一些DOTNETARX的例子。要运行文中的例子,你必须创建一个新的工程并加入DOTNETARX2.0组件,然后在代码文件中加入"using DOTNETARX;"。关于DOTNETARX的详细说明,请大家参考DOTNETARX参考文档。
- 多重选择集
在ObjectARX托管封装类中有一个bug,你不能使用多重选择集。例如,你不能使用选择集来同时选择圆和直线。但有了DOTNETARX2.0,你就可以使用多重选择集了。
下面的例子在选择集中加入半径为1.0的圆和在层ABC上的直线。
void test()
{
TypedValue[] values=new TypedValue[]{
new TypedValue(-4,"
1<or"), new="" typedvalue((int)dxfcode.real,1.0),="" typedvalue(-4,"<and"),="" typedvalue(-4,"and="" typedvalue(0,"circle"),="">"),
2
3new TypedValue(-4,"<and"), new="" typedvalue((int)dxfcode.layername,"abc"),="" typedvalue(-4,"and="" typedvalue(0,"line"),="">"),
4
5new TypedValue(-4,"or>")
6
7};
8
9int n=Tools.GetSelection(values).Count;//在选择集中加入对象
10
11Tools.Editor.WriteMessage(n.ToString());
12
13}
14
15 * 加入多个实体
16
17
18
19在DOTNETARX1.0(也就是原来的zhfarx),如果你多次使用Tools.AddEntity来加入多个实体,那么效率将非常的低。所以在2.0中,我加入了AddEntities()函数来解决这个问题。
20
21下面的例子使用AddEntities()在AutoCAD图形中加入直线和圆。
22
23void AddLineCircle()
24
25{
26
27Point3d pt1=new Point3d(0,0,0);
28
29Point3d pt2=new Point3d(50,50,0);
30
31Lines line=new Lines(pt1,pt2);
32
33Point3d center=new Point3d(50,0,0);
34
35Circles circle=new Circles(center,50);
36
37Tools.AddEntities(new Entity[]{line,center});
38
39}
40
41 * 组
42
43
44
45下面的例子在AutoCAD中加入一个组。
46
47void MakeGroup()
48
49{
50
51PromptSelectionResult res=Tools.Editor.GetSelection();//选择对象
52
53SelectionSet ss=res.Value; //获取选择集
54
55ObjectId[] ids=ss.GetObjectIds(); //获取表示所选对象的objectId数组
56
57Group gp=new Group("grouptest",true);//创建名为grouptest的组对象
58
59gp.Append(new ObjectIdCollection(ids));// 加入上面的所选对象到组中
60
61Tools.AddDictionaryObject("ASDK_GROUPTEST",gp,Tools.Database.GroupDictionaryId);
62
63//使用AddDictionaryObject()函数把组加入到AutoCAD中。
64
65}
66
67 * 扩展记录
68
69
70
71下面的例子在AutoCAD中加入一个扩展记录。
72
73void MakeXRecord()
74
75{
76
77Xrecord rec=new Xrecord();//创建一个扩展记录对象
78
79rec.Data=new ResultBuffer(
80
81new TypedValue((int)DxfCode.Text,"This is a test"),
82
83new TypedValue((int)DxfCode.Int8,0),
84
85new TypedValue((int)DxfCode.UcsOrg,new Point3d(0,0,0))
86
87);//使用Xreord的Data属性来设置Xreord的内容
88
89Tools.AddDictionaryObject("test",rec,Tools.Database.NamedObjectsDictionaryId);
90
91//使用AddDictionaryObject()函数把扩展记录加入到AutoCAD中。
92
93foreach (TypedValue rb in rec.Data) //输出刚才加入的条目
94
95{
96
97Tools.Editor.WriteMessage(string.Format("TypedCode={0},Value={1}\n",rb.TypeCode,rb.Value));
98
99}
100
101}
102
103 * 扩展数据
104
105
106
107下面的例子在AutoCAD中加入扩展数据。
108
109void MakeXData()
110
111{
112
113Lines line=new Lines(new Point3d(0,0,0),new Point3d(100,100,0));
114
115//创建直线
116
117RegAppTableRecord app=new RegAppTableRecord();
118
119//加入扩展数据到直线,首先注册程序
120
121app.Name="MyApp";
122
123Tools.AddSymbolTableRecord(app,Tools.Database.RegAppTableId);
124
125//使用AddSymbolTableRecord 函数把app加入到RegAppTable。
126
127line.XData=new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName,"MyApp"),
128
129new TypedValue((int)DxfCode.ExtendedDataAsciiString,"This is some xdata string"));
130
131//设置直线的XData属性
132
133Tools.AddEntity(line);//加入直线到数据库中
134
135foreach (TypedValue rb in line.XData)// 输出刚才加入的条目
136
137{
138
139Tools.Editor.WriteMessage(string.Format("TypedCode={0},Value={1}\n",rb.TypeCode,rb.Value));
140
141}
142
143}
144
145 * 复制和移动
146
147
148
149下面的例子先使用Copy()获取所选实体的拷贝,然后使用Move()来移动这个拷贝。
150
151void test()
152
153{
154
155PromptEntityResult res = Tools.Editor.GetEntity("please select an entity to copy:\n");
156
157ObjectId id= Tools.Copy(res.ObjectId);
158
159Tools.Move(id,res.PickedPoint,res.PickedPoint.Add(new Vector3d(20,20,0)));
160
161}
162
163 * 镜像拷贝
164
165
166
167下面的例子镜像拷贝所选的实体。
168
169void test()
170
171{
172
173Editor ed=Tools.Editor;
174
175PromptEntityResult res =ed.GetEntity("please select an entity:\n");
176
177Point3d pt1=ed.GetPoint("Select the first point of the mirror axis:\n").Value;
178
179Point3d pt2=ed.GetPoint("Select the second point of the mirror axis:\n").Value;
180
181Tools.Mirror(res.ObjectId,pt1,pt2,false);
182
183}
184
185 * 偏移
186
187
188
189下面的例子偏移所选的实体。
190
191void test()
192
193{
194
195Editor ed=Tools.Editor;
196
197PromptEntityResult res =ed.GetEntity("please select an entity:\n");
198
199Tools.Offset(res.ObjectId,20);
200
201}
202
203 * 旋转
204
205
206
207下面的例子旋转所选的实体45度角。
208
209void test()
210
211{
212
213Editor ed=Tools.Editor;
214
215PromptEntityResult res =ed.GetEntity("please select an entity:\n");
216
217Point3d pt1=ed.GetPoint("Select the base point of rotation:\n").Value;
218
219Tools.Rotate(res.ObjectId,pt1,45);//注意:是角度而不是弧度
220
221}
222
223 * 缩放
224
225
226
227下面的例子放大所选的实体2倍。
228
229void test()
230
231{
232
233Editor ed=Tools.Editor;
234
235PromptEntityResult res =ed.GetEntity("please select an entity:\n");
236
237Point3d pt1=ed.GetPoint("Select the base point of scaling:\n").Value;
238
239Tools.Scale(res.ObjectId,pt1,2);
240
241}
242
243 * 加入符号表记录
244
245
246
247下面的例子加入一个名为Test的层
248
249void CreateLayer()
250
251{
252
253LayerTableRecord ltr=new LayerTableRecord();
254
255ltr.Name="Test";
256
257Tools.AddSymbolTableRecord(ltr,Tools.Database.LayerTableId);
258
259}
260
261 * 获取符号表遍历器
262
263
264
265下面的例子使用GetIteratorForSymbolTable()来遍历层表,并输出层的名字。
266
267void test()
268
269{
270
271DBObjectCollection objs= Tools.GetIteratorForSymbolTable(Tools.Database.LayerTableId);
272
273foreach (DBObject obj in objs)
274
275{
276
277LayerTableRecord ltr=(LayerTableRecord)obj;
278
279Tools.Editor.WriteMessage(ltr.Name+"\n");
280
281}
282
283}
284
285 * 选择过一个点的所有实体
286
287
288
289void test()
290
291{
292
293PromptPointResult res=Tools.Editor.GetPoint("Please select a point:\n");
294
295Point3d pt=res.Value;
296
297int n=Tools.SelectAtPoint(pt).Count;
298
299Tools.Editor.WriteMessage(n.ToString());
300
301}</and"),></or"),>