圖層的的添加spa
///acDoc 要添加的文檔文檔
///strLayerName 圖層名字string
///color 圖層顏色it
///io
public ObjectId CreateCadLayer(Document acDoc, string strLayerName,System.Drawing.Color color)
{
try
{
if (String.IsNullOrEmpty(strLayerName))
return ObjectId.Null;foreach
ObjectId layerId = new ObjectId();List
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;
}tab
圖層刪除poi
///layer 要刪除的圖層名co
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; } }