①: 隐藏实体
在CAD的开发中,有时候我们需要根据用户的需求隐藏和显示一些实体,对于实体本身的属性Visible当然可以实现隐藏、显示实体,不过,除此之外,我们还可以将一个层上的实体同时隐藏起来,这就要使用LayerTableRecord中的IsOff属性,并且隐藏的实体ObjectId可以出现在SelectAll()的集合中.当然,他们之间还是有区别的,当你用Ctrl+A,选中当前活动文档的全部实体是,你就会发现原来他们之间区别。
简单的测试程序:
2 [CommandMethod( " TestLayer " )]
3 public void TestLayerVis()
4 {
5 // 选择一点创建实体
6 PromptPointOptions opt = new PromptPointOptions( " 选择起始点: " );
7 PromptPointResult res = ed.GetPoint(opt);
8 if (PromptStatus.OK == res.Status)
9 {
10 Point3d sPt = res.Value;
11 using (Transaction trans = db.TransactionManager.StartTransaction())
12 {
13 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
14 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
15 // 直线,其所在层为默认的层
16 Line line = new Line();
17 line.StartPoint = sPt;
18 line.EndPoint = new Point3d(sPt.X + 100 , sPt.Y + 50 , 0 );
19 // 圆,指定层
20 Circle c = new Circle(sPt, new Vector3d( 0 , 0 , 1 ), 30 );
21 c.LayerId = CreateLayer( " TestLayer " );
22 line.Visible = false ; // 不显示实体
23 btr.AppendEntity(line);
24 btr.AppendEntity(c);
25 trans.AddNewlyCreatedDBObject(line, true );
26 trans.AddNewlyCreatedDBObject(c, true );
27
28 LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
29 LayerTableRecord ltr = (LayerTableRecord)trans.GetObject(CreateLayer( " TestLayer " ), OpenMode.ForWrite);
30 ltr.IsOff = true ; // 关闭图层,隐藏图层上的实体
31
32 trans.Commit();
33 }
34 }
35 }
36
37 // 创建一个图层
38 private ObjectId CreateLayer( string layerName)
39 {
40 ObjectId objId = ObjectId.Null;
41 using (Transaction trans = db.TransactionManager.StartTransaction())
42 {
43 LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
44 if (lt.Has(layerName))
45 {
46 objId = lt[layerName];
47 }
48 else
49 {
50 LayerTableRecord ltr = new LayerTableRecord();
51 ltr.Name = layerName;
52 objId = lt.Add(ltr);
53 trans.AddNewlyCreatedDBObject(ltr, true );
54 trans.Commit();
55 }
56 }
57
58 return objId;
59 }
60 #endregion
②:修改线宽
有的时候我们为了打印出来的图片更加的清楚,可能会设计到修改实体的线宽,对于多段线,我们很容易想到使用ConstantWidth,这样就可以修改其线宽了,其实,对于圆或是直线也一样,不过我们在修改他们的线宽之前要保证系统变量的lwdisplay的值为1(开),这时我们再设置LineWeight,就可以改变他们的线宽了。
简单的测试程序:
2 [CommandMethod( " TestLW " )]
3 public void TestLW()
4 {
5 PromptPointOptions opt = new PromptPointOptions( " 选择起点: " );
6 PromptPointResult res = ed.GetPoint(opt);
7 if (PromptStatus.OK == res.Status)
8 {
9 Point3d sPt = res.Value;
10 ObjectId plId = ObjectId.Null;
11 ObjectId circleId = ObjectId.Null;
12 using (Transaction trans = db.TransactionManager.StartTransaction())
13 {
14 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
15 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
16
17 // 创建一个多段线
18 Polyline pl = new Polyline();
19 pl.AddVertexAt( 0 , sPt.Convert2d( new Plane()), 0 , 0 , 0 );
20 pl.AddVertexAt( 1 , new Point3d(sPt.X + 100 , sPt.Y + 50 , 0 ).Convert2d( new Plane()), 0 , 0 , 0 );
21 pl.AddVertexAt( 2 , new Point3d(sPt.X + 150 , sPt.Y - 150 , 0 ).Convert2d( new Plane()), 0 , 0 , 0 );
22
23 // 创建一个圆
24 Circle c = new Circle(sPt, new Vector3d( 0 , 0 , 1 ), 40 );
25
26 plId = btr.AppendEntity(pl);
27 circleId = btr.AppendEntity(c);
28 trans.AddNewlyCreatedDBObject(pl, true );
29 trans.AddNewlyCreatedDBObject(c, true );
30 // 修改线宽
31 pl.ConstantWidth = 0.15 ;
32 c.LineWeight = LineWeight.LineWeight035; // 绝对线宽
33
34 trans.Commit();
35 }
36 }
37 }
38 #endregion
③:多段线凸起
这个功能一般用在两条多段线相交时,区分多段线的走向而特意使相交的多段线在交点出凸起一个"小弧",在这个应用中,我们会发现PolyLine中的AddVertexAt的奇妙之处(插入点的序号可以小于PLine的节点数目,PLine的原有节点的序号会因为有新插入点而自增)。
简单的测试程序:
2 public void TestPolyLine()
3 {
4 PromptPointOptions opt = new PromptPointOptions( " 选择起点: " );
5 PromptPointResult res = ed.GetPoint(opt);
6 if (PromptStatus.OK == res.Status)
7 {
8 Point3d sPt = res.Value;
9 ObjectId plId = ObjectId.Null;
10 ObjectId circleId = ObjectId.Null;
11 Polyline pl = new Polyline();
12 using (Transaction trans = db.TransactionManager.StartTransaction())
13 {
14 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
15 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
16
17 // 创建一个多段线
18 pl.AddVertexAt( 0 , sPt.Convert2d( new Plane()), 0 , 0 , 0 );
19 pl.AddVertexAt( 1 , new Point3d(sPt.X + 200 , sPt.Y + 150 , 0 ).Convert2d( new Plane()), 0 , 0 , 0 );
20 pl.AddVertexAt( 2 , new Point3d(sPt.X + 400 , sPt.Y + 80 , 0 ).Convert2d( new Plane()), 0 , 0 , 0 );
21
22 btr.AppendEntity(pl);
23 trans.AddNewlyCreatedDBObject(pl, true );
24 /// /加一个小弧
25 Point3d pt1 = pl.GetPointAtDist( 80 );
26 Point3d pt2 = pl.GetPointAtDist( 100 );
27 pl.AddVertexAt( 1 , pt1.Convert2d( new Plane()), 0 , 0 , 0 );
28 pl.AddVertexAt( 2 , pt2.Convert2d( new Plane()), 0 , 0 , 0 );
29 pl.SetBulgeAt( 1 , 0.78 );
30
31 trans.Commit();
32 }
33
34 }
35 }