Author Topic: create Layers, is there a better way  (Read 13419 times)

0 Members and 1 Guest are viewing this topic.

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
create Layers, is there a better way
« on: January 03, 2008, 01:30:00 PM »
What Im looking for is suggestions on how to code this better, or am I doing pretty well.  I went back to some really old code from when C# first started working with acad, and tried to improve what I did back then.  Anyway, comments are welcome

Code: [Select]
namespace LayerCode
{
    public class LayerCode
    {
        [CommandMethod("CL1")]
        public void CreateLayer1()
        {
            Database DB = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            using (Transaction tr = DB.TransactionManager.StartTransaction())
            {
                LayerTable LT = (LayerTable)DB.LayerTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
                if (LT.Has("DSH"))
                {
                    tr.Abort();
                }
                else
                {
                    LayerTableRecord LTR = new Autodesk.AutoCAD.DatabaseServices.LayerTableRecord();
                    LTR.Name = "DSH";
                    LT.Add(LTR);
                    tr.AddNewlyCreatedDBObject(LTR, true);
                    tr.Commit();
                }
            }
        }
    }
}
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

T.Willey

  • Needs a day job
  • Posts: 5241
Re: create Layers, is there a better way
« Reply #1 on: January 03, 2008, 01:33:42 PM »
I think it looks good except for one thing.  Tony T. has pointed out that the Has() method will return items that are marked to be erased, meaning that they don't really exist in the drawing, and will be lost when the drawing is closed.  So what he did was use a sub function to search the whole table checking the name and the IsErased (I think) property, and if both were correct, it would return that item.  I will see if I can find the post, as it was posted here.

Edit:  Here you go.
Tim

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

Please think about donating if this post helped you.

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #2 on: January 03, 2008, 01:40:43 PM »
Here's how I'd do it:

Code: [Select]
[CommandMethod("CreateDuhLayer")]
static public void createduhlayer()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

using (Transaction tr = db.TransactionManager.StartTransaction()) {
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, false) as LayerTable;
if (lt.Has("Some LayerNameGoesHere"))
return;

LayerTableRecord newLTR = new LayerTableRecord();
newLTR.Name = "SomeLayerNameGoesHere";
// set any other properties

lt.UpgradeOpen();
lt.Add(newLTR);

tr.AddNewlyCreatedDBObject(newLTR, true);

tr.Commit();
}

}

Cheers,
Glenn.
Me

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: create Layers, is there a better way
« Reply #4 on: January 03, 2008, 01:53:40 PM »
Thanks Kerry.  I saw that thread, and I have to admit, you had so much going on, I was lost  :-(

Glenn, In your IF statement, did you forget the {} or are they not needed?  Also, when you opened the Layertable for read, what is the false for?  I'm going to see if I can find that answer, on my own, just curious if you had a quick answer
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: create Layers, is there a better way
« Reply #5 on: January 03, 2008, 01:55:28 PM »
the LayerTable.UpgradeOpen is b/c you first accessed it ForRead, now you want to ForWrite - correct?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: create Layers, is there a better way
« Reply #6 on: January 03, 2008, 01:58:05 PM »
Also, Glenn, the code you posted is being used as a sub, and the return; sends it back to another Sub/Funcion?  If not, im confused on what the return; is doing.  I didn't see you passing a "Name" to this function to check for existence, so I guess i'm just confused
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #7 on: January 03, 2008, 02:00:53 PM »
Duh,

Regarding the IF - you don't need {} if you're only executing one line - more than one and you need the {} (rather like lisp nad it's if statements and progn's)

The 'false' is "don't open erased records". Most code I've seen doesn't use the third argument whereas I always use it (habits from ARX I suppose).

WRT UpGradeOpen, yes you're right. The reason you do this is to minimise the time you have things open for write - other things will want to open for write as well, so always keep it as short as possible.

Dan mentioned something I was going to and what he said happens to ALL the SymbolTables and it occurs using the default indexer as well eg. lt["SomeLayerNameGoesHere"]

Cheers,
Glenn.
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #8 on: January 03, 2008, 02:02:59 PM »
Also, Glenn, the code you posted is being used as a sub, and the return; sends it back to another Sub/Funcion?  If not, im confused on what the return; is doing.  I didn't see you passing a "Name" to this function to check for existence, so I guess i'm just confused

You'll notice the method has been decorated with the CommandMethod attribute, so this is the command definition. The return will just return out of this method/function and hand control back to AutoCAD.
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #9 on: January 03, 2008, 02:05:21 PM »
One other thing: This is C#, so stop thinking of them as Sub's and forget that VB drivel  :evil:  :-D
Curly braces rule  :-D
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #10 on: January 03, 2008, 02:18:46 PM »
BTW, happy birthday as well...get some code books did we ;)

Cheers,
Glenn.
Me

CmdrDuh

  • Automatic Duh Generator
  • King Gator
  • Posts: 4039
Re: create Layers, is there a better way
« Reply #11 on: January 03, 2008, 02:24:11 PM »
You'll notice the method has been decorated with the CommandMethod attribute, so this is the command definition. The return will just return out of this method/function and hand control back to AutoCAD.
COOL!! I learned something new today!!!  Dont read into this more than what Im saying, but its like an internal ESC key for the function.  Its there so quit the Command(CommandMethod).  VERY nice.  I love .Net
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

HD

  • Guest
Re: create Layers, is there a better way
« Reply #12 on: January 03, 2008, 02:25:05 PM »
Quote
The 'false' is "don't open erased records". Most code I've seen doesn't use the third argument whereas I always use it (habits from ARX I suppose).

Hi,

I am curious about something. By setting the third argument to false, does that negate what Tim stated about the "Has" method will return items that are marked to be erased?

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #13 on: January 03, 2008, 02:27:08 PM »
You're welcome Duh - learning something is always good in my book.

I suppose, thinking about it, that the VB equivalent of return; (a blank return returning nothing) would be Exit Sub...

Cheers,
Glenn.
« Last Edit: January 03, 2008, 02:32:02 PM by Glenn R »
Me

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: create Layers, is there a better way
« Reply #14 on: January 03, 2008, 02:31:00 PM »
Quote
The 'false' is "don't open erased records". Most code I've seen doesn't use the third argument whereas I always use it (habits from ARX I suppose).

Hi,

I am curious about something. By setting the third argument to false, does that negate what Tim stated about the "Has" method will return items that are marked to be erased?


Nick,

No it won't. To tell the truth, it was pretty superfluous in this case because if the LayerTable ITSELF was erased, your drawing would be cactus.

You would mainly use the construct I showed when, for instance, you were looping over the layertablerecords, which is totally different to just calling the LayerTable.Has() function, which internally does a loop, but will return erased records...it's a nasty little trap which they still haven't fixed.

Cheers,
Glenn.
Me