AutoCAD .net开发之三-图形绘制
1
using System ;
2
using Autodesk.AutoCAD.Runtime ;
3
using Autodesk.AutoCAD.Geometry;
4
using Autodesk.AutoCAD.DatabaseServices;
5
using Autodesk.AutoCAD.Colors;
6![](/Images/OutliningIndicators/None.gif)
7
[assembly: CommandClass(typeof(ClassLibrary.Class))]
8![](/Images/OutliningIndicators/None.gif)
9
namespace ClassLibrary
10
{
11
/// <summary>
12
/// Summary description for Class.
13
/// </summary>
14
public class Class
15
{
16
public Class()
17
{
18
19
}
20![](/Images/OutliningIndicators/InBlock.gif)
21![](/Images/OutliningIndicators/InBlock.gif)
22![](/Images/OutliningIndicators/InBlock.gif)
23
[CommandMethod("test1")]
24
public void createCircle()
25![](/Images/OutliningIndicators/InBlock.gif)
26
{
27
//首先声明我们要使用的对象
28![](/Images/OutliningIndicators/InBlock.gif)
29
Circle circle; //这个是我们要加入到模型空间的圆
30![](/Images/OutliningIndicators/InBlock.gif)
31
BlockTableRecord btr;//要加入圆,我们必须打开模型空间
32
BlockTable bt; //要打开模型空间,我们必须通过块表(BlockTable)来访问它
33
//我们使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
34
Transaction trans;
35![](/Images/OutliningIndicators/InBlock.gif)
36
//使用TransactionManager的StartTransaction()成员来开始事务处理
37
38
trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
39
//现在创建圆……请仔细看这些参数——注意创建Point3d对象的‘New’和Vector3d的静态成员ZAxis
40
41
circle = new Circle(new Point3d(10, 10, 0), Vector3d.ZAxis, 2);
42
bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
43
//使用当前的空间Id来获取块表记录——注意我们是打开它用来写入
44
btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite );
45
//现在使用btr对象来加入圆
46
btr.AppendEntity(circle);
47
trans.AddNewlyCreatedDBObject(circle, true); //并确定事务处理知道要加入圆!
48
//一旦完成以上操作,我们就提交事务处理,这样以上所做的改变就被保存了……
49
trans.Commit();
50
//…然后销毁事务处理,因为我们已经完成了相关的操作(事务处理不是数据库驻留对象,可以销毁)
51![](/Images/OutliningIndicators/InBlock.gif)
52
53![](/Images/OutliningIndicators/InBlock.gif)
54![](/Images/OutliningIndicators/InBlock.gif)
55![](/Images/OutliningIndicators/InBlock.gif)
56
trans.Dispose();
57![](/Images/OutliningIndicators/InBlock.gif)
58
59![](/Images/OutliningIndicators/InBlock.gif)
60
}
61
[CommandMethod("test2")]
62
public void f()
63
{
64
Transaction trans1 = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
65
Ellipse e = new Ellipse(new Point3d(10,10,0),Vector3d.ZAxis,new Vector3d(3,0,0),0.5,0,0);
66
BlockTable bt1 = (BlockTable)trans1.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId,OpenMode.ForRead);
67
BlockTableRecord btr1 = (BlockTableRecord)trans1.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId,OpenMode.ForWrite);
68
btr1.AppendEntity(e);
69
trans1.AddNewlyCreatedDBObject(e,true);
70
trans1.Commit();
71![](/Images/OutliningIndicators/InBlock.gif)
72
73
}
74![](/Images/OutliningIndicators/InBlock.gif)
75![](/Images/OutliningIndicators/InBlock.gif)
76
public ObjectId CreateLayer()
77
{
78
ObjectId layerId; //它返回函数的值
79
80
Database db = HostApplicationServices.WorkingDatabase;
81
Transaction trans = db.TransactionManager.StartTransaction();
82
//首先取得层表……
83
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
84
//检查EmployeeLayer层是否存在……
85
if (lt.Has("EmployeeLayer"))
86
{
87
layerId = lt["EmployeeLayer"];
88
}
89
else
90
{
91
//如果EmployeeLayer层不存在,就创建它
92![](/Images/OutliningIndicators/InBlock.gif)
93
LayerTableRecord ltr = new LayerTableRecord();
94
ltr.Name = "EmployeeLayer"; //设置层的名字
95
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
96
layerId = lt.Add(ltr);
97
trans.AddNewlyCreatedDBObject(ltr, true);
98![](/Images/OutliningIndicators/InBlock.gif)
99
}
100![](/Images/OutliningIndicators/InBlock.gif)
101
trans.Commit();
102![](/Images/OutliningIndicators/InBlock.gif)
103
trans.Dispose();
104![](/Images/OutliningIndicators/InBlock.gif)
105
return layerId;
106![](/Images/OutliningIndicators/InBlock.gif)
107
}
108![](/Images/OutliningIndicators/InBlock.gif)
109![](/Images/OutliningIndicators/InBlock.gif)
110![](/Images/OutliningIndicators/InBlock.gif)
111
}
112
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/InBlock.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/InBlock.gif)
63
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/InBlock.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/InBlock.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/InBlock.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
74
![](/Images/OutliningIndicators/InBlock.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/InBlock.gif)
84
![](/Images/OutliningIndicators/InBlock.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
87
![](/Images/OutliningIndicators/InBlock.gif)
88
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
91
![](/Images/OutliningIndicators/InBlock.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/InBlock.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/InBlock.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/InBlock.gif)
103
![](/Images/OutliningIndicators/InBlock.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
108
![](/Images/OutliningIndicators/InBlock.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
112
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)