anderson 发表于 2014-4-2 22:22:00

CAD二次开发基础之图层管理

本人初学CAD二次开发,已有半载,感觉中途还是走了很多弯路,在此共享些基础东西。和前辈们探讨。
本文主要是图层管理,如常见的新建、关闭、删除等。基本上都是基础东西,只列出了关键代码,没写注释。
新建图层

///
      /// create new layer
      ///
      ///
      public void CreateLayer(String layerName)
      {
            Document doc = Autodesk..ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Transaction trs = db.TransactionManager.StartTransaction();
            using (trs)
            {
                LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
                if (!lt.Has(layerName))
                {
                  LayerTableRecord ltr = new LayerTableRecord();
                  ltr.Name = layerName;
                  lt.Add(ltr);
                  trs.AddNewlyCreatedDBObject(ltr, true);
                }
                trs.Commit();
            }
      }
判断图层是否存在
///
      /// judge if exist layer
      ///
      ///
      ///
      public bool IsExistLayer(String layerName)
      {
            bool retval = false;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Transaction trs = db.TransactionManager.StartTransaction();
            using (trs)
            {
                LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (lt.Has(layerName))
                {
                  retval = true;
                }
                trs.Commit();
            }
            return retval;
      }
锁定图层
///
      /// lock layer
      ///
      ///
      public void LockLayer(String layerName)
      {
            if (!IsExistLayer(layerName))
            {
                return;
            }            
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Transaction trs = db.TransactionManager.StartTransaction();
            using (trs)
            {
                LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                LayerTableRecord ltr = trs.GetObject(lt, OpenMode.ForWrite) as LayerTableRecord;
                if (!ltr.IsLocked)
                {
                  ltr.IsLocked = true;
                }
                trs.Commit();
            }
      }
删除图层
///
      /// erase layer
      ///
      ///
      ///
      public bool EraseLayer(String layerName)
      {
            bool retval = false;
            if (!IsExistLayer(layerName))
            {
                return retval;
            }
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Transaction trs = db.TransactionManager.StartTransaction();
            using (trs)
            {
                LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
                LayerTableRecord ltr = trs.GetObject(lt, OpenMode.ForWrite) as LayerTableRecord;
                lt.GenerateUsageData();
                if ((db.Clayer == lt) || ltr.IsUsed
                  || layerName == "0" || layerName == "Defpoints")
                {
                  trs.Commit();
                  return retval;
                }
                ltr.Erase(true);
                retval = true;
                trs.Commit();
            }
            return retval;
      }
设置当前图层
      ///
      /// set layer to current layer
      ///
      ///
      public void SetLayerCurrent(String layerName)
      {
            if (!IsExistLayer(layerName))
            {
                return;
            }
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Transaction trs = db.TransactionManager.StartTransaction();
            using (trs)
            {
                LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                db.Clayer = lt;
                trs.Commit();
            }
      }
获取所有图层名称
///
      /// get all layers names
      ///
      ///
      public List GetAllLayersName()
      {
            List layers = new List();
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Transaction trs = db.TransactionManager.StartTransaction();
            using (trs)
            {
                LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                foreach (ObjectId id in lt)
                {
                  LayerTableRecord ltr = trs.GetObject(id, OpenMode.ForRead) as LayerTableRecord;
                  layers.Add(ltr.Name);
                }
                trs.Commit();
            }
            return layers;
      }
获取图层上的实体id
///
      /// get layer entity id
      ///
      ///
      public void GetLayerEntity(String layerName, ref List entitys)
      {
            if (!IsExistLayer(layerName))
            {
                return;
            }
            
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            TypedValue []values = { new TypedValue((int)DxfCode.LayerName, layerName)};
            SelectionFilter filters = new SelectionFilter(values);            
            PromptSelectionResult result = ed.SelectAll();
            if (result.Status == PromptStatus.OK)
            {
                ObjectId []ids = result.Value.GetObjectIds();
                foreach (ObjectId id in ids)
                {
                  entitys.Add(id);
                }
            }
            else
            {
                return;
            }
            
      }

cdinten 发表于 2014-4-3 22:00:00


嗯,做的不错。不过还是有两个地方先交流一下。
获取指定图层上的实体id的方法,可以考虑使用如下函数签名:
ObjectIdCollection GetObjectsOnLayer(string layerName);
这样和ObjectARX的使用风格保持统一,还是一个问题就是:
PromptSelectionResult result = ed.SelectAll();
应该需要加上过滤吧?如下:
PromptSelectionResult result = ed.SelectAll(filters);
不然感觉没有过滤?不知道我说的对不对,你可以检验一下。

anderson 发表于 2014-4-6 09:58:00


嗯嗯,好,是的,那里的确是有一处错误,谢谢指正

hjki_i 发表于 2016-10-8 16:07:00


楼主,求分享
1246462177@qq.com

wfch2000 发表于 2018-1-25 13:10:00

好东西,刚好要用到

凨仴黯夜 发表于 2018-2-7 11:16:00

好东西,学习了

BaoWSE 发表于 2018-2-23 22:48:00

好东西!先留名以后会用的着

heiwaheiwa 发表于 2020-2-15 23:23:00

好东西,新手学习了。求教一下怎么将根据选择的图元移动到指定图层?
页: [1]
查看完整版本: CAD二次开发基础之图层管理