标签:
图层的的添加
///acDoc 要添加的文档
///strLayerName 图层名字
///color 图层颜色
///
public ObjectId CreateCadLayer(Document acDoc, string strLayerName,System.Drawing.Color color)
{
try
{
if (String.IsNullOrEmpty(strLayerName))
return ObjectId.Null;
ObjectId layerId = new ObjectId();
using(DocumentLock doclock = acDoc.LockDocument())
{
using(Transaction tr = acDoc.Database.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(acDoc.Database.LayerTableId, OpenMode.ForWrite) as LayerTable;
if (lt.Has(strLayerName))
{
layerId = lt[strLayerName];
}
else
{
LayerTableRecord layer = new LayerTableRecord();
layer.Name = strLayerName;
layer.Color = Autodesk.AutoCAD.Colors.Color.FromColor(color);
layerId = lt.Add(layer);
tr.AddNewlyCreatedDBObject(layer, true);
}
tr.Commit();
tr.Dispose();
}
}
return layerId;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "提示");
return ObjectId.Null;
}
图层删除
///layer 要删除的图层名
using (Transaction tr = doc.Database.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(doc.Database.LayerTableId, OpenMode.ForWrite) as LayerTable;
LayerTableRecord ltr = tr.GetObject(lt[layer], OpenMode.ForWrite) as LayerTableRecord;
lt.GenerateUsageData();
if ((doc.Database.Clayer == lt[layer]) //"0"和“Defpoints"以及当前图层不可以删除,若要删除当前图层,必须先修改当前图层到其他图层
|| layer == "0" || layer == "Defpoints") //最好是0图层 代码 doc.Database.Clayer = lt["0"];
{
tr.Commit();
//
}
else
{
tr.Erase(true);
tr.Commit();
}
}
获取全部的CAD图层名字 //当然也可以获取LayerTableRecord的其他记录 比如ObjectId ,linewith,color等
public void GetAllCadLayerName(Document acDoc, out List<string> lyList)
{
lyList = new List<string>();
try
{
Database acDb = acDoc.Database;
using (Transaction tr = acDb.TransactionManager.StartTransaction())
{
LayerTable lyTbl = tr.GetObject(acDb.LayerTableId, OpenMode.ForRead) as LayerTable;
foreach (ObjectId layerId in lyTbl)
{
LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);
if (ltr != null /*&& !ltr.Name.Equals("0")*/)
{
lyList.Add(ltr.Name);
}
}
tr.Commit();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "提示");
return;
}
}
标签:
原文地址:http://www.cnblogs.com/wangfan-0812/p/4825432.html