1
using
System ;
2
using
Autodesk.AutoCAD.Runtime ;
3
using
Autodesk.AutoCAD.Geometry;
4
using
Autodesk.AutoCAD.DatabaseServices;
5
using
Autodesk.AutoCAD.Colors;
6
7
[assembly: CommandClass(
typeof
(ClassLibrary.Class))]
8
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
21
22
23
[CommandMethod("test1")]
24
public void createCircle()
25
26
{
27
//首先声明我们要使用的对象
28
29
Circle circle; //这个是我们要加入到模型空间的圆
30
31
BlockTableRecord btr;//要加入圆,我们必须打开模型空间
32
BlockTable bt; //要打开模型空间,我们必须通过块表(BlockTable)来访问它
33
//我们使用一个名为‘Transaction’的对象,把函数中有关数据库的操作封装起来
34
Transaction trans;
35
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
52
53
54
55
56
trans.Dispose();
57
58
59
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
72
73
}
74
75
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
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
99
}
100
101
trans.Commit();
102
103
trans.Dispose();
104
105
return layerId;
106
107
}
108
109
110
111
}
112
}

2

3

4

5

6

7

8

9

10



11


12

13

14

15



16

17



18

19

20

21

22

23

24

25

26



27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63



64

65

66

67

68

69

70

71

72

73

74

75

76

77



78

79

80

81

82

83

84

85

86



87

88

89

90



91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112
