Author Topic: How to get an objects layer  (Read 4298 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
How to get an objects layer
« on: January 18, 2007, 12:06:58 PM »
When using code like whats posted below (which does not work), how can you get the layer?  I have tried serval methods, and none seem to work.  I'm so lost.  Any/all help appreciated.
Code: [Select]
public void MyPrintMspace()
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId ObjId in btr)
{
DBObject DbObj = trans.GetObject(ObjId, OpenMode.ForRead);
ed.WriteMessage("\n " + DbObj.ToString());
Entity Ent = trans.GetObject(ObjId, OpenMode.ForRead);
ed.WriteMessage("\n  Layer: " + Ent.Layer);
}
trans.Commit();
trans.Dispose();
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: How to get an objects layer
« Reply #1 on: January 18, 2007, 12:49:40 PM »
Code: [Select]
                //Entity Ent = trans.GetObject(ObjId, OpenMode.ForRead);
                Entity Ent = DbObj as Entity;

I tried your code, and did not run here... I change it to what it is above... now, let me find where is the info to simple return the layer name...


T.Willey

  • Needs a day job
  • Posts: 5251
Re: How to get an objects layer
« Reply #2 on: January 18, 2007, 12:55:49 PM »
Code: [Select]
                //Entity Ent = trans.GetObject(ObjId, OpenMode.ForRead);
                Entity Ent = DbObj as Entity;

I tried your code, and did not run here... I change it to what it is above... now, let me find where is the info to simple return the layer name...


Thanks Luis!!  You don't know how long I have spent looking for this simple answer.  How did you know to do it this way.  Here is the code I use to print the layer name.
Code: [Select]
foreach (ObjectId ObjId in btr)
{
DBObject DbObj = trans.GetObject(ObjId, OpenMode.ForRead);
ed.WriteMessage("\n " + DbObj.ToString());
Entity Ent = DbObj as Entity;
ed.WriteMessage("\n  Layer: " + Ent.Layer);
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to get an objects layer
« Reply #3 on: January 18, 2007, 03:47:49 PM »
If you don't need to use the bdObj later, you could cast in-line rather than create another variable ...

Code: [Select]
        public static void MyPrintMspace_1()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;           
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
                foreach (ObjectId ObjId in
                    (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead))
                {
                    Entity Ent = (Entity)trans.GetObject(ObjId, OpenMode.ForRead, true);
                    ed.WriteMessage("\n  Layer: " + Ent.Layer);
                }               
            }
        }
« Last Edit: January 18, 2007, 03:51:50 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: How to get an objects layer
« Reply #4 on: January 18, 2007, 03:55:42 PM »
Thanks Kerry.  I could have sworn I tried tha way, and it didn't work for me, but yours did.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to get an objects layer
« Reply #5 on: January 18, 2007, 04:02:56 PM »
.. or if you wanted to keep the dbObj, you could still cast in-line inside the writemessage
Code: [Select]
                foreach (ObjectId ObjId in btr)
                {
                    DBObject DbObj = trans.GetObject(ObjId, OpenMode.ForRead);
                    ed.WriteMessage("\n " + DbObj.ToString());                   
                    ed.WriteMessage("\n  Layer: " + (DbObj as Entity).Layer);
                }
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to get an objects layer
« Reply #6 on: January 18, 2007, 04:12:04 PM »
... and if you want to be frugal :-

this :,
Code: [Select]
ed.WriteMessage("\n " + DbObj.ToString());could become this :
Code: [Select]
ed.WriteMessage("\n " + DbObj);
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: How to get an objects layer
« Reply #7 on: January 18, 2007, 04:54:52 PM »
 :lmao:

This was one of my first C# routines, I just changed it so I could test for getting the layer of and entity.  I use the same method in my larger program to step through all the objects in the drawing.  I just couldn't, for the life of me, find a way to turn it into an entity.

Thanks for all the options Kerry.  I'm still learning, and this is some good information.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How to get an objects layer
« Reply #8 on: January 18, 2007, 10:08:56 PM »
You're welcome Tim

 ... but keep in mind I'm learning too so the stuff I'm posting may not be optimal.

 :wink:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: How to get an objects layer
« Reply #9 on: January 19, 2007, 11:18:21 AM »
You're welcome Tim

 ... but keep in mind I'm learning too so the stuff I'm posting may not be optimal.

 :wink:
Okay Kerry.  It was just funny to see how a simple program, written when one first learns something, can be changed so much to run faster, better with less lines of code.  Got to love experience! :-D
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

LE

  • Guest
Re: How to get an objects layer
« Reply #10 on: January 19, 2007, 11:33:31 AM »
To me when starting learning a new software language it is better not trying to come up with the greatest solutions or the best compact code, that will come up as is mentioned with experience.