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

Get Current/Active Layer?

16 REPLIES 16
Reply
Message 1 of 17
Natalie-C
4085 Views, 16 Replies

Get Current/Active Layer?

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.
16 REPLIES 16
Message 2 of 17
Anonymous
in reply to: Natalie-C

HostApplicationServices.WorkingDatabase.Clayer
Message 3 of 17
Natalie-C
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!
Message 4 of 17
Anonymous
in reply to: Natalie-C

It is normal way! 🙂
Message 5 of 17
Natalie-C
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.
Message 6 of 17
Anonymous
in reply to: Natalie-C

Oops! Why you iterate BlockTableRecord? You need open LayerTableRecord with ObjectID equal db.Clayer and read it's name!
Message 7 of 17
Natalie-C
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!
Message 8 of 17
Anonymous
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]
Message 9 of 17
Natalie-C
in reply to: Natalie-C

See...even better! 17 line to just 3.

Thanks a bunch for your help!
Message 10 of 17
Anonymous
in reply to: Natalie-C

First line is spare! 🙂 It is enough:
[code]
Dim ltr As LayerTableRecord = CType(trans.GetObject(db.Clayer, OpenMode.ForRead), LayerTableRecord)
MsgBox("Current Layer: " + ltr.Name)
[/code]
Message 11 of 17
Anonymous
in reply to: Natalie-C

You can shorten it a bit more by getting rid of the loop.

LayerTableRecord currentLayer =
(LayerTableRecord)trans.GetObject(activeDb.Clayer, OpenMode.ForRead);
string currentLayerName = currentLayer.Name;
--
Bobby C. Jones

wrote in message news:5257809@discussion.autodesk.com...
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!
Message 12 of 17
Anonymous
in reply to: Natalie-C

It is just made! 🙂
Message 13 of 17
Anonymous
in reply to: Natalie-C

Hey Alex,
I just saw that you're faster on the draw! 🙂
--
Bobby C. Jones

wrote in message news:5257860@discussion.autodesk.com...
It is just made! 🙂
Message 14 of 17
Anonymous
in reply to: Natalie-C

🙂 If I also knew how to programme on VB.NET, it will be more quickly...
Message 15 of 17
Natalie-C
in reply to: Natalie-C

OK, now you guys are just showing off 🙂
Message 16 of 17
Anonymous
in reply to: Natalie-C

No Natalie! I really do not program on VB.NET (only C++/C#/VisualLisp)
Message 17 of 17
Anonymous
in reply to: Natalie-C

In a completely new drawing (only layer 0):

Command: -LAYER

Current layer: "0"
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/PStyle/Freeze/Thaw/LOck/Unlock/stAte]: N

Enter name list for new layer(s): LAYER1
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/PStyle/Freeze/Thaw/LOck/Unlock/stAte]:

Command: PURGE

Command: -PURGE

Enter type of unused objects to purge
[Blocks/Dimstyles/LAyers/LTypes/Plotstyles/SHapes/textSTyles/Mlinestyles/Tablest
yles/Regapps/All]: LAYER
Enter name(s) to purge <*>: LAYER1
Verify each name to be purged? [Yes/No] : N
Deleting layer "LAYER1".
1 layer deleted.

Command: -LAYER

Current layer: "0"
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/PStyle/Freeze/Thaw/LOck/Unlock/stAte]: N

Enter name list for new layer(s): LAYER1
Enter an option
[?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/PStyle/Freeze/Thaw/LOck/Unlock/stAte]:

Command: (command "NETLOAD" "CsMgdAcad1.dll")
NIL

Command: ERASED_TABLE_RECORD

Layer name: LAYER1

Layer table indexer returned an erased record!

Command: NON-ERASED_TABLE_RECORD

Layer name: LAYER1

GetSymbolTableRecordId() returned non-erased record

////////////////////////////////////////////////////////////////////////////////////////////////////////

using System ;
using System.Text;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using System.Collections;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;


namespace CsMgdAcad1
{
public class SymbolTableIssue
{
public SymbolTableIssue()
{
}

[CommandMethod("ERASED_TABLE_RECORD")]
public void Test()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

PromptStringOptions ops = new PromptStringOptions("\nLayer name: ");
ops.AllowSpaces = true;

PromptResult res = ed.GetString(ops);

if( res.Status == PromptStatus.OK && res.StringResult != string.Empty )
{

Database db = HostApplicationServices.WorkingDatabase;

using( Transaction tr = db.TransactionManager.StartTransaction() )
{
LayerTable table = (LayerTable) tr.GetObject(db.LayerTableId, OpenMode.ForRead);
ObjectId id = table[res.StringResult];
if( id.IsNull )
ed.WriteMessage("\nLayer not found");
if( id.IsErased )
ed.WriteMessage("\nLayer table indexer returned an erased record!");
else
ed.WriteMessage("\nLayer table indexer returned non-erased record");
}
}
}

[CommandMethod("NON-ERASED_TABLE_RECORD")]
public void Test1()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

PromptStringOptions ops = new PromptStringOptions("\nLayer name: ");
ops.AllowSpaces = true;

PromptResult res = ed.GetString(ops);

if( res.Status == PromptStatus.OK && res.StringResult != string.Empty )
{

Database db = HostApplicationServices.WorkingDatabase;

using( Transaction tr = db.TransactionManager.StartTransaction() )
{
LayerTable table = (LayerTable) tr.GetObject(db.LayerTableId, OpenMode.ForRead);
ObjectId id = GetSymbolTableRecordId(table, res.StringResult);
if( id.IsNull )
ed.WriteMessage("\nSpecified layer not found.");
else if ( id.IsErased )
ed.WriteMessage("\nGetSymbolTableRecordId() returned erased record ???");
else
ed.WriteMessage("\nGetSymbolTableRecordId() returned non-erased record");
}
}
}

// Workaround. Always returns the non-erased record, when there
// are multiple records with the same name (in that case, all but
// one will be erased).

ObjectId GetSymbolTableRecordId(SymbolTable table, string name)
{
ObjectId result = ObjectId.Null;
if( table.Has(name) )
{
result = table[name];
if( result.IsErased )
{
using( Transaction tr = table.Database.TransactionManager.StartTransaction() )
{
foreach( ObjectId id in table )
{
if( ! id.IsErased )
{
SymbolTableRecord rec =
(SymbolTableRecord) tr.GetObject(id, OpenMode.ForRead, false);
if( string.Compare(rec.Name, name, true) == 0 )
{
result = id;
break;
}
}
}
}
}
}
return result;
}
}
}


////////////////////////////////////////////////////////////////////////////////////////////////////////

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5257814@discussion.autodesk.com...
See...even better! 17 line to just 3.

Thanks a bunch for your help!

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