本人初学CAD二次开发,已有半载,感觉中途还是走了很多弯路,在此共享些基础东西。和前辈们探讨。
本文主要是图层管理,如常见的新建、关闭、删除等。基本上都是基础东西,只列出了关键代码,没写注释。
- /// <summary>
- /// create new layer
- /// </summary>
- /// <param name="layerName"></param>
- public void CreateLayer(String layerName)
- {
- 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;
- if (!lt.Has(layerName))
- {
- LayerTableRecord ltr = new LayerTableRecord();
- ltr.Name = layerName;
- lt.Add(ltr);
- trs.AddNewlyCreatedDBObject(ltr, true);
- }
- trs.Commit();
- }
- }
- /// <summary>
- /// judge if exist layer
- /// </summary>
- /// <param name="layerName"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// lock layer
- /// </summary>
- /// <param name="layerName"></param>
- 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[layerName], OpenMode.ForWrite) as LayerTableRecord;
- if (!ltr.IsLocked)
- {
- ltr.IsLocked = true;
- }
- trs.Commit();
- }
- }
- /// <summary>
- /// erase layer
- /// </summary>
- /// <param name="layerName"></param>
- /// <returns></returns>
- 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[layerName], OpenMode.ForWrite) as LayerTableRecord;
- lt.GenerateUsageData();
- if ((db.Clayer == lt[layerName]) || ltr.IsUsed
- || layerName == "0" || layerName == "Defpoints")
- {
- trs.Commit();
- return retval;
- }
- ltr.Erase(true);
- retval = true;
- trs.Commit();
- }
- return retval;
- }
- /// <summary>
- /// set layer to current layer
- /// </summary>
- /// <param name="layerName"></param>
- 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[layerName];
- trs.Commit();
- }
- }
- /// <summary>
- /// get all layers names
- /// </summary>
- /// <returns></returns>
- public List<String> GetAllLayersName()
- {
- List<String> layers = new List<String>();
- 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;
- }
- /// <summary>
- /// get layer entity id
- /// </summary>
- /// <param name="entitys"></param>
- public void GetLayerEntity(String layerName, ref List<ObjectId> 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;
- }
-
- }
|