CAD二次开发 --关于图层方面的应用

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Sabrina_TSM/article/details/78933967

LayerTable(层表)是存放LayerTableRecord(层表记录)的容器,层表记录就是图层。
1.根据图层名过滤对象

public ObjectIdCollection GetObjectIdsAtLayer(string name)
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var ed = doc.Editor;
    ObjectIdCollection ids = new ObjectIdCollection();
    var layerList = new List<TypedValue>
    {               
        new TypedValue(Convert.ToInt32(DxfCode.LayerName), name),
    };
    SelectionFilter sf = new SelectionFilter(layerList.ToArray());
    PromptSelectionResult psr = ed.GetSelection(sf);
    if (psr.Status == PromptStatus.OK)
    {
        ObjectId[] id = psr.Value.GetObjectIds();
        for (int i = 0; i < id.Length; i++)
        {
            ids.Add(id[i]);
        }
    }
    return ids;
}

2.建立指定名字、颜色的图层

 public ObjectId Addin(string layerName, short colorIndex, Database db)
 {
     short colorIndex1 = (short)(colorIndex % 256);
     using (Transaction tr = db.TransactionManager.StartTransaction())
     {
       LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
       ObjectId layerId = ObjectId.Null;
       if (lt.Has(layerName) == false)
       {
         LayerTableRecord ltr = new LayerTableRecord();
         ltr.Name = layerName;
         ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, colorIndex1);
         layerId = lt.Add(ltr);
         tr.AddNewlyCreatedDBObject(ltr, true);
       }
       tr.Commit();
       return layerId;
     }
 }

3.点击对象,获取该对象的图层名

private string acquireLayer(string keywords)
{
    var doc = Application.DocumentManager.MdiActiveDocument;
    var ed = doc.Editor;
    var db = doc.Database;
    PromptEntityOptions peo = new PromptEntityOptions(keywords);
    var per = ed.GetEntity(peo);
    if (per.Status == PromptStatus.OK)
    {
       using (var tr = db.TransactionManager.StartTransaction())
       {
           var polyLine = (Polyline)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
           if (polyLine != null)
           {
               return polyLine.Layer;
           }
           tr.Commit();
       }
    }
    return string.Empty;
}

4. 新建图层,并判断该图层是否已经存在。

 private void AddLayer(string layerName)
{
    doc = Application.DocumentManager.MdiActiveDocument;
    db = doc.Database;
    ed = doc.Editor;
    string LayerName = "";
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
        if (lt.Has(layerName))
            ed.WriteMessage("此图层已存在");
        else
        {
             LayerName = layerName;
        }
        LayerTableRecord ltr = new LayerTableRecord();
        ltr.Name = LayerName;
        PromptIntegerOptions pio = new PromptIntegerOptions("\n 请输入图层的颜色");
        PromptIntegerResult prs = ed.GetInteger(pio);
        if (prs.Status == PromptStatus.OK)
        {
            colorIndex = (short)prs.Value;
            short colorIndex1 = (short)(colorIndex % 256);
            ltr.Color = Color.FromColorIndex(ColorMethod.ByColor, colorIndex1);
        }
        ObjectId layerId = lt.Add(ltr);
        tr.AddNewlyCreatedDBObject(ltr, true);
        db.Clayer = layerId;
        tr.Commit();
    }
}
展开阅读全文

没有更多推荐了,返回首页