03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-9)

前端之家收集整理的这篇文章主要介绍了03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-9)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.9、Erase Layers删除图层

You can erase a layer at any time during a drawing session. You cannot erase the current layer,layer 0,an xref-dependent layer,or a layer that contains objects. @H_403_3@

可以在绘图过程中随时删除图层。不能删除当前图层、图层0、依赖外部参考的图层,以及包含有对象的图层。@H_403_3@

To erase a layer,use the Erase method. It is recommended to use the Purge function to verify that the layer can be purged,along with verifying that it is not layer 0,Defpoints,or the current layer. @H_403_3@

删除图层使用Erase方法。推荐使用Purge函数检查要删除的图层是否可以被清除,该函数还同时检查要删除的图层不是0图层、Defpoints图层或当前图层。@H_403_3@

Note: Layers referenced by block definitions,along with the special layer named DEFPOINTS,cannot be deleted even if they do not contain visible objects. @H_403_3@

注意:即使不含可见对象,也不能删除块定义所引用的图层、名为Defpoints的特殊图层。@H_403_3@

@H_403_3@

VB.NET @H_403_3@

Imports Autodesk.AutoCAD.Runtime@H_403_3@

Imports Autodesk.AutoCAD.ApplicationServices@H_403_3@

Imports Autodesk.AutoCAD.DatabaseServices@H_403_3@

@H_403_3@

<CommandMethod("EraseLayer")> _@H_403_3@

Public Sub EraseLayer()@H_403_3@

'' Get the current document and database@H_403_3@

Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument@H_403_3@

Dim acCurDb As Database = acDoc.Database@H_403_3@

@H_403_3@

'' Start a transaction@H_403_3@

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()@H_403_3@

@H_403_3@

'' Open the Layer table for read@H_403_3@

Dim acLyrTbl As LayerTable@H_403_3@

acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,_@H_403_3@

OpenMode.ForRead)@H_403_3@

@H_403_3@

Dim sLayerName As String = "ABC"@H_403_3@

@H_403_3@

If acLyrTbl.Has(sLayerName) = True Then@H_403_3@

'' Check to see if it is safe to erase layer@H_403_3@

Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()@H_403_3@

acObjIdColl.Add(acLyrTbl(sLayerName))@H_403_3@

acCurDb.Purge(acObjIdColl)@H_403_3@

@H_403_3@

If acObjIdColl.Count > 0 Then@H_403_3@

Dim acLyrTblRec As LayerTableRecord@H_403_3@

acLyrTblRec = acTrans.GetObject(acObjIdColl(0),OpenMode.ForWrite)@H_403_3@

@H_403_3@

Try@H_403_3@

'' Erase the unreferenced layer@H_403_3@

acLyrTblRec.Erase(True)@H_403_3@

@H_403_3@

'' Save the changes and dispose of the transaction@H_403_3@

acTrans.Commit()@H_403_3@

Catch Ex As Autodesk.AutoCAD.Runtime.Exception@H_403_3@

'' Layer could not be deleted@H_403_3@

Application.ShowAlertDialog("Error:\n" + Ex.Message)@H_403_3@

End Try@H_403_3@

End If@H_403_3@

End If@H_403_3@

End Using@H_403_3@

End Sub@H_403_3@

@H_403_3@

C# @H_403_3@

using Autodesk.AutoCAD.Runtime;@H_403_3@

using Autodesk.AutoCAD.ApplicationServices;@H_403_3@

using Autodesk.AutoCAD.DatabaseServices;@H_403_3@

@H_403_3@

[CommandMethod("EraseLayer")]@H_403_3@

public static void EraseLayer()@H_403_3@

{@H_403_3@

// Get the current document and database获取当前文档和数据库@H_403_3@

Document acDoc = Application.DocumentManager.MdiActiveDocument;@H_403_3@

Database acCurDb = acDoc.Database;@H_403_3@

@H_403_3@

// Start a transaction启动事务@H_403_3@

using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())@H_403_3@

{@H_403_3@

// Open the Layer table for read以读打开图层表@H_403_3@

LayerTable acLyrTbl;@H_403_3@

acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,@H_403_3@

OpenMode.ForRead) as LayerTable;@H_403_3@

@H_403_3@

string sLayerName = "ABC";@H_403_3@

@H_403_3@

if (acLyrTbl.Has(sLayerName) == true)@H_403_3@

{@H_403_3@

// Check to see if it is safe to erase layer检查删除图层是否安全@H_403_3@

ObjectIdCollection acObjIdColl = new ObjectIdCollection();@H_403_3@

acObjIdColl.Add(acLyrTbl[sLayerName]);@H_403_3@

acCurDb.Purge(acObjIdColl);@H_403_3@

@H_403_3@

if (acObjIdColl.Count > 0)@H_403_3@

{@H_403_3@

LayerTableRecord acLyrTblRec;@H_403_3@

acLyrTblRec = acTrans.GetObject(acObjIdColl[0],@H_403_3@

OpenMode.ForWrite) as LayerTableRecord;@H_403_3@

@H_403_3@

try@H_403_3@

{@H_403_3@

// Erase the unreferenced layer删除未引用图层@H_403_3@

acLyrTblRec.Erase(true);@H_403_3@

@H_403_3@

// Save the changes and dispose of the transaction保存修改关闭事务@H_403_3@

acTrans.Commit();@H_403_3@

}@H_403_3@

catch (Autodesk.AutoCAD.Runtime.Exception Ex)@H_403_3@

{@H_403_3@

// Layer could not be deleted不能删除@H_403_3@

Application.ShowAlertDialog("Error:\n" + Ex.Message);@H_403_3@

}@H_403_3@

}@H_403_3@

}@H_403_3@

}@H_403_3@

}@H_403_3@

@H_403_3@

VBA/ActiveX Code Reference @H_403_3@

Sub EraseLayer()@H_403_3@

On Error Resume Next@H_403_3@

@H_403_3@

Dim layerObj As AcadLayer@H_403_3@

Set layerObj = ThisDrawing.Layers("ABC")@H_403_3@

@H_403_3@

layerObj.Delete@H_403_3@

End Sub@H_403_3@

猜你在找的VB相关文章