AutoCAD二次开发&自定义线型

作者: admin 分类: CAD 发布时间: 2020-07-31 23:42

转自:https://www.pianshen.com/article/255218545/

今天是国庆的最后一天了,有点时间,来学习一下AutoCAD关于线型方面的内容。很多关于AutoCAD开发插件都会或多或少的涉及到线型方面的内容。线型存放在AutoCAD线型表中,线型表是AutoCAD几个组成表之一。首先这里去线型表里面找是否有我们对应名字的表,如果没有,来创建我们需要的线型样式。具体的线型样式,可以参考代码里面的注释,样式内容对应着线型表记录属性,设置相应的值即可。AutoCAD中对图形见面的实体线型类型可以通过LineTypeId来设置。经过测试,遇到了一个问题。在创建新的线型时,线型名称(Name)不能设置为空的字符串,否则AutoCAD运行后会报空错误。

这里创建好新的线型后,我们创建了多段线、圆两种实体分别用于展现我们创建新的创建线型。最终的效果如下图所示。

cd2515d1aeaea5fbcede707345af693c.png

f7ac55220a68c269d7af676354748f8c.png

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;
 
        }
    }
}

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

标签云