.NET

Reply
Contributor
Natalie-C
Posts: 19
Registered: ‎07-11-2006
Message 1 of 17 (244 Views)

Get Current/Active Layer?

244 Views, 16 Replies
08-03-2006 06:51 AM
Is there are way to get the current layer using vb.net 2005 and autocad 2007?

Any help or a point in the right direction would be appreciated.
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 2 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 07:49 AM in reply to: Natalie-C
HostApplicationServices.WorkingDatabase.Clayer
Contributor
Natalie-C
Posts: 19
Registered: ‎07-11-2006
Message 3 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 07:53 AM in reply to: Natalie-C
Thanks....I actually just figured that out.

Now, I'm trying to get the actual layer name. My code is working but I don't feel it's the most efficient way of doing it.

Here it is:

Dim id As ObjectId
Dim bt As BlockTable = CType(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
For Each id In bt
Dim btr As BlockTableRecord = CType(trans.GetObject(id, OpenMode.ForRead), BlockTableRecord)
Dim btrID As ObjectId
For Each btrID In btr
Dim ent As Entity = CType(trans.GetObject(btrID, OpenMode.ForRead, False), Entity)
If TypeOf ent Is BlockReference Then
Dim blkRef As BlockReference = CType(ent, BlockReference)
blkRef = ent
If blkRef.LayerId = db.Clayer Then
MsgBox("Current Layer: " + blkRef.Layer)
Exit Sub
End If
End If
Next
Next

If you could give me a better way, that would be awesome!!

Thanks again!
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 4 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 07:55 AM in reply to: Natalie-C
It is normal way! :smileyhappy:
Contributor
Natalie-C
Posts: 19
Registered: ‎07-11-2006
Message 5 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 07:56 AM in reply to: Natalie-C
Wow!!

I'm really new to autocad development....as if you couldn't tell. It just looks like i took the long way. lol.
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 6 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 07:59 AM in reply to: Natalie-C
Oops! Why you iterate BlockTableRecord? You need open LayerTableRecord with ObjectID equal db.Clayer and read it's name!
Contributor
Natalie-C
Posts: 19
Registered: ‎07-11-2006
Message 7 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 08:05 AM in reply to: Natalie-C
Ahh...yes, that does make more since.

Dim id As ObjectId
Dim lt As LayerTable = CType(trans.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
For Each id In lt
Dim ltr As LayerTableRecord = CType(trans.GetObject(id, OpenMode.ForRead), LayerTableRecord)
If ltr.Id = db.Clayer Then
MsgBox("Current Layer: " + ltr.Name)
Exit Sub
End If
Next

Much less code too!!

Thanks!
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 8 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 08:08 AM in reply to: Natalie-C
[code]
Dim lt As LayerTable = CType(trans.GetObject(db.LayerTableId, OpenMode.ForRead), LayerTable)
Dim ltr As LayerTableRecord = CType(trans.GetObject(db.Clayer, OpenMode.ForRead), LayerTableRecord)
MsgBox("Current Layer: " + ltr.Name)
[/code]
Contributor
Natalie-C
Posts: 19
Registered: ‎07-11-2006
Message 9 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 08:09 AM in reply to: Natalie-C
See...even better! 17 line to just 3.

Thanks a bunch for your help!
Distinguished Contributor
Alexander Rivilis
Posts: 303
Registered: ‎09-26-2004
Message 10 of 17 (244 Views)

Re: Get Current/Active Layer?

08-03-2006 08:17 AM in reply to: Natalie-C
First line is spare! :smileyhappy: It is enough:
[code]
Dim ltr As LayerTableRecord = CType(trans.GetObject(db.Clayer, OpenMode.ForRead), LayerTableRecord)
MsgBox("Current Layer: " + ltr.Name)
[/code]
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.