.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.NET Turn off/on all layers in active document

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
xpeter88
2715 Views, 4 Replies

VB.NET Turn off/on all layers in active document

Hi,

I am new to the VB.NET and would like to turn off all the existing layers in active document. I have found in developer's guide how to turn off one layer with specified layer name but can not apply it on all layers with any layername.

 

I tried to get all the Object ID's in layer table and then for each object turn layer off but seems that it does not work.

Is there anythig what I did wrong?

 

Thanks,

 

<CommandMethod("FilterLayers")> _
Public Sub FilterLayers()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Layer table for read
Dim LayerTable As LayerTable = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
Dim LayerTableRecord As LayerTableRecord = acTrans.GetObject(LayerTable.ObjectId, OpenMode.ForWrite)
For Each ObjectId In LayerTable
LayerTableRecord.IsOff = True
Next
'' Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub

 

4 REPLIES 4
Message 2 of 5
SENL1362
in reply to: xpeter88

I am not used to program in VB.NET, but i think the problem is in you're loop, see below.

However this will also turn off the active layer, and that might not be a good idee.

 

 

 

<CommandMethod("FilterLayers")> _
Public Sub FilterLayers()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Layer table for read
Dim LayerTable As LayerTable = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
For Each ObjectId In LayerTable
   Dim LayerTableRecord As LayerTableRecord = acTrans.GetObject(ObjectId, OpenMode.ForWrite)

   LayerTableRecord.IsOff = True
Next
'' Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub

 

Message 3 of 5
SENL1362
in reply to: SENL1362

c# sample to turn layers off except current/active layer

(using Dynamics would be even more simpler).

 

[CommandMethod("LOFF")]
        public void TurnOffLayers()
        {
            string activeLayerName = "Layer3";

            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
                if (lt.Has(activeLayerName))
                {
                    LayerTableRecord cLy = (LayerTableRecord)tr.GetObject(lt[activeLayerName], OpenMode.ForWrite);
                    cLy.IsFrozen = cLy.IsOff=false;
                    db.Clayer = cLy.ObjectId;
                }

                foreach (ObjectId ltrId in lt)
                {
                    if (db.Clayer != ltrId)
                    {
                        LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(ltrId, OpenMode.ForWrite);
                        ltr.IsOff = true;
                    }
                }
                tr.Commit();
            }
        }

 

Message 4 of 5
xpeter88
in reply to: xpeter88

Thanknyou guys, now it works fine,
Message 5 of 5
cyakupcebioglu
in reply to: SENL1362

Hi,

My command is not work. I don't understand

I want to object select and close layer. Command one by one working, but combine command is not working.

 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myPEO As New Autodesk.AutoCAD.EditorInput.PromptEntityOptions(vbLf & "Tabakasını kapatmak istediğiniz objeyi seçiniz:")
Dim mydwg, mydb, myed, myPS, myPer, myent, mytrans, mytransman As Object

mydwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

 

mydb = mydwg.Database
myed = mydwg.Editor

myPEO.SetRejectMessage("Hatalı obje seçimi." & vbCrLf)
myPEO.AddAllowedClass(GetType(Autodesk.AutoCAD.DatabaseServices.DBObject), False)

myPer = myed.GetEntity(myPEO)
myPS = myPer.Status
Select Case myPS
Case Autodesk.AutoCAD.EditorInput.PromptStatus.OK

 

'MsgBox("Good job!")
mytransman = mydwg.TransactionManager
mytrans = mytransman.StartTransaction
myent = myPer.ObjectId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)

 TabakaKapat(myent.Layer)

exit sub

Case Autodesk.AutoCAD.EditorInput.PromptStatus.Cancel
myed.WriteMessage(vbLf & "...işlem iptal edildi.")

Exit Sub
Case Autodesk.AutoCAD.EditorInput.PromptStatus.Error
MsgBox("Error warning.")
Exit Sub
Case Else
Exit Sub
End Select

End Sub

 

 

Module : TabakaKapat

Public Sub TabakaKapat(TabakaAdi As String)
   '' Get the current document and database
   Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
   Dim acCurDb As Database = acDoc.Database

     Using acDocLock As DocumentLock = acDoc.LockDocument
     '' Start a transaction
          Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
          '' Open the Layer table for read
          Dim LayerTable As LayerTable = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
               For Each ObjectId In LayerTable
                    Dim LayerTableRecord As LayerTableRecord = acTrans.GetObject(ObjectId, OpenMode.ForWrite)
                    If LayerTableRecord.Name = TabakaAdi Then
                         LayerTableRecord.IsOff = True
                    Exit For
                    End If
               Next
'' Save the changes and dispose of the transaction
          acTrans.Commit()
      End Using
     End Using
End Sub

 

Can you help me?

 

And, I don't speak english 🙂 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost