①: 隱藏實體
在CAD的開發中,有時候我們需要根據用戶的需求隱藏和顯示一些實體,對于實體本身的屬性Visible當然可以實現隱藏、顯示實體,不過,除此之外,我們還可以將一個層上的實體同時隱藏起來,這就要使用LayerTableRecord中的IsOff屬性,并且隱藏的實體ObjectId可以出現在SelectAll()的集合中.當然,他們之間還是有區別的,當你用Ctrl+A,選中當前活動文檔的全部實體是,你就會發現原來他們之間區別。
簡單的測試程序:


1 #region 測試圖層的IsOff和實體的Visible是否一致
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
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,就可以改變他們的線寬了。
簡單的測試程序:


1 #region 測試直線、圓的LineWeight和PolyLine的ConstantWidth之間的區別
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
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的原有節點的序號會因為有新插入點而自增)。
簡單的測試程序:


1 [CommandMethod("TestPolyLine")]
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 }
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 }