最近有朋友询问切换图层的代码
com切换图层
12 Public Sub MySubLayerChange() 3 Dim Thisdrawing As Autodesk.AutoCAD.Interop.AcadDocument = Application.DocumentManager.MdiActiveDocument.AcadDocument 4 Dim curLayer As Autodesk.AutoCAD.Interop.Common.AcadLayer = Thisdrawing.ActiveLayer 5 Dim newLayer As Autodesk.AutoCAD.Interop.Common.AcadLayer 6 For Each la As Autodesk.AutoCAD.Interop.Common.AcadLayer In Thisdrawing.Layers 7 If la.Name = "layer1" Then 8 newLayer = la 9 Thisdrawing.ActiveLayer = newLayer10 End If11 Next12 13 End Sub
示效的话用下面的代码,切换系统变量
1 Public Sub ChangeLayer(ByRef LayerName As String) 2 3 Acadapp.ActiveDocument.SetVariable("Clayer", LayerName) 4 Acadapp.ActiveDocument.SetVariable("CELTYPE", "Bylayer") 5 6 'For Each entry As Object In Acadapp.ActiveDocument.layers 7 ' If entry.name = LayerName Then 8 ' Acadapp.ActiveDocument.Activelayer = entry 9 ' Exit For10 ' End If11 'Next entry12 13 ''改变线型14 'For Each entry As Object In Acadapp.ActiveDocument.Linetypes15 ' If entry.name = "Bylayer" Then16 ' Acadapp.ActiveDocument.ActiveLinetype = entry17 ' Exit For18 ' End If19 'Next20 21 End Sub22 '改变标注样式 //20190606 nan sheng modify 23 Public Sub ChangeDimStyles(ByRef Name As String)24 Acadapp.ActiveDocument.SendCommand("-dimstyle" & vbCr & "r" & vbCr & Name & vbCr) REM "_zoom" & vbCr & "a" & vbCr25 'ThisDrawing.SendCommand ("-dimstyle" & vbCr & "r" & vbCr & "111" & vbCr)26 'For Each entry As Object In Acadapp.ActiveDocument.DimStyles27 ' If entry.name = Name Then28 ' Acadapp.ActiveDocument.ActiveDimStyle = entry29 ' Exit For30 ' End If31 'Next entry32 End Sub33 34 '改变文字样式//20190606 nan sheng modify 35 Public Sub ChangeTextStyles(ByRef Name As String)36 Acadapp.ActiveDocument.SetVariable("TEXTSTYLE", Name)37 'For Each entry As Object In Acadapp.ActiveDocument.TextStyles38 ' If entry.name = Name Then39 ' Acadapp.ActiveDocument.ActiveTextStyle = entry40 ' Exit For41 ' End If42 'Next entry43 End Sub
NetApi切换图层
12 Public Sub MySubLayerChangeNetApi() 3 Dim doc As Document = Application.DocumentManager.MdiActiveDocument 4 Dim db As Database = doc.Database 5 Using trans As Transaction = db.TransactionManager.StartTransaction() 6 Dim lt As LayerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead) 7 If lt.Has("layer1") Then 8 db.Clayer = lt("layer1") 9 End If10 trans.Commit()11 End Using12 13 End Sub