AutoCAD二次開發&自定義線型
阿新 • • 發佈:2018-12-13
於AutoCAD開發外掛都會或多或少的涉及到線型方面的內容。線型存放在AutoCAD線型表中,線型表是AutoCAD幾個組成表之一。首先這裡去線型表裡面找是否有我們對應名字的表,如果沒有,來建立我們需要的線型樣式。具體的線型樣式,可以參考程式碼裡面的註釋,樣式內容對應著線型表記錄屬性,設定相應的值即可。AutoCAD中對圖形見面的實體線型型別可以通過LineTypeId來設定。經過測試,遇到了一個問題。在建立新的線型時,線型名稱(Name)不能設定為空的字串,否則AutoCAD執行後會報空錯誤。
這裡建立好新的線型後,我們建立了多段線、圓兩種實體分別用於展現我們建立新的建立線型。最終的效果如下圖所示。
具體實現程式碼如下所示。
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ComplexLinetype { public class Class1 { [CommandMethod("CCL")] public void demo() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; Transaction trans = db.TransactionManager.StartTransaction(); using (trans) { BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); using (Circle circle = new Circle()) { circle.Center = new Point3d(600, 600, 0); circle.Normal = Vector3d.ZAxis; circle.Radius = 50; circle.LinetypeId = GetLineTypeId("yfqhGIS"); btr.AppendEntity(circle); trans.AddNewlyCreatedDBObject(circle, true); } using (Polyline acPoly1 = new Polyline()) { acPoly1.SetDatabaseDefaults(db); acPoly1.AddVertexAt(0, new Point2d(400, 400), 0, 0, 0); acPoly1.AddVertexAt(1, new Point2d(400, 600), 0, 0, 0); acPoly1.AddVertexAt(2, new Point2d(600, 600), 0, 0, 0); acPoly1.AddVertexAt(3, new Point2d(600, 400), 0, 0, 0); acPoly1.Closed = true; btr.AppendEntity(acPoly1); trans.AddNewlyCreatedDBObject(acPoly1, false); Polyline2d poly21 = acPoly1.ConvertTo(true); poly21.LinetypeGenerationOn = false; poly21.LinetypeId = GetLineTypeId("yfqhGIS"); trans.AddNewlyCreatedDBObject(poly21, true); } trans.Commit(); } } /// <summary> /// 獲取線型的ID /// </summary> /// <param name="lineName"></param> /// <returns></returns> public ObjectId GetLineTypeId(string lineName) { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Transaction trans = db.TransactionManager.StartTransaction(); ObjectId ObjID = ObjectId.Null; using (trans) { TextStyleTable tt = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead); LinetypeTable lt = (LinetypeTable)trans.GetObject(db.LinetypeTableId, OpenMode.ForWrite); if (lt.Has(lineName)) { ObjID = lt[lineName]; } else { LinetypeTableRecord ltr = new LinetypeTableRecord(); ltr.Name = lineName;//線型名 ltr.AsciiDescription = " ---- 測繪 ---- 測繪 ---- 測繪 ----";//線型描述 ltr.PatternLength = 0.9;////線型總長度 ltr.NumDashes = 3;//組成線型的筆畫數目 ltr.SetDashLengthAt(0, 0.5);//0.5個單位的畫線 ltr.SetDashLengthAt(1, -0.2);//0.2個單位的空格 ltr.SetShapeStyleAt(1, tt["Standard"]);//設定文字的樣式 ltr.SetShapeNumberAt(1, 0);//設定空格處包含的圖案圖形 ltr.SetShapeOffsetAt(1, new Vector2d(-0.1, -0.05));//圖形在X軸方向上左移0.1個單位,在Y軸方向上下移0.05個單位 ltr.SetShapeScaleAt(1, 0.1);//圖形的縮放比例 ltr.SetShapeIsUcsOrientedAt(1, false);// ltr.SetShapeRotationAt(1, 0); ltr.SetTextAt(1, "測繪");//文字內容 ltr.SetDashLengthAt(2, -0.2);//0.2個單位的空格 ObjID = lt.Add(ltr); trans.AddNewlyCreatedDBObject(ltr, true); } trans.Commit(); } return ObjID; } } }
更多內容,請關注公眾號