.NET

Reply
Active Contributor
xpeter88
Posts: 42
Registered: ‎11-14-2013
Message 1 of 4 (561 Views)
Accepted Solution

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

561 Views, 3 Replies
01-05-2014 04:14 AM

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

 

Mentor
SENL1362
Posts: 150
Registered: ‎07-20-2011
Message 2 of 4 (549 Views)

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

01-05-2014 06:41 AM 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

 

Mentor
SENL1362
Posts: 150
Registered: ‎07-20-2011
Message 3 of 4 (543 Views)

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

01-05-2014 07:05 AM 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();
            }
        }

 

Active Contributor
xpeter88
Posts: 42
Registered: ‎11-14-2013
Message 4 of 4 (511 Views)

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

01-05-2014 12:06 PM in reply to: xpeter88
Thanknyou guys, now it works fine,

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community