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

0 Members and 1 Guest are viewing this topic.

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: create Layers, is there a better way
« Reply #75 on: July 30, 2008, 06:26:59 PM »
Yep, that's how I handle most projects :D

(the sad thing is there's always a hint of truth in comedy  :|)
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #76 on: August 05, 2008, 05:31:20 PM »
ok, time for an update.  With help from Glenn and Kerry, I now have code that will move objects from a layer that is named incorrectly to a correctly named layer.  I am still having problems with my linetype, specifically its losing scope

Code: [Select]
        [CommandMethod("LTree")]
        static public void createLayerXML(string Lname)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.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 == null)
                    return;
                if (lt.Has(Lname))
                {
                    return;
                }
                else
                {
                    DataSet dsLayerData = new DataSet();
                    dsLayerData.ReadXml(@"c:\CS_TEPLayers.xml");
                    DataView dsLayerDefinition = new DataView(dsLayerData.Tables["Layer"]);
                    dsLayerDefinition.Sort = "LayerName";

                    int rowIndex = dsLayerDefinition.Find(Lname);
                    if (rowIndex == -1)
                    {
                        ed.WriteMessage("Bogus Layer Name");
                    }
                    else
                    {
                        short lC = Convert.ToInt16(dsLayerDefinition[rowIndex]["Color"]);
                        string lnT = dsLayerDefinition[rowIndex]["LineType"].ToString();
                        LinetypeTable LTT = tr.GetObject(db.LinetypeTableId, OpenMode.ForRead, false) as LinetypeTable;
                        if (LTT == null)
                            return;
                        if (LTT.Has(lnT))
                            return;
                        else
                        {
                            LinetypeTableRecord newLTTR = new LinetypeTableRecord();
                            newLTTR.Name = lnT;
                            LTT.UpgradeOpen();
                            LTT.Add(newLTTR);
                            tr.AddNewlyCreatedDBObject(newLTTR, true);


                        }
                        //string layerColor = dsLayerDefinition[rowIndex]["Color"].ToString();
                        //short  lC = Convert.ToInt16(layerColor);
                        LayerTableRecord newLTR = new LayerTableRecord();
                        newLTR.Name = Lname;
                        newLTR.Color = Color.FromColorIndex(ColorMethod.ByAci, lC);
                        //newLTR.LinetypeObjectId = newLTTR; // Lost scope here from else above
                        lt.UpgradeOpen();
                        lt.Add(newLTR);
                        tr.AddNewlyCreatedDBObject(newLTR, true);
                    }
                }

                // set any other properties
                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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #77 on: August 05, 2008, 05:34:16 PM »
BTW, the above code doesn't work as a command, I'm still working that part out.  This came from another piece of code further up in the thread.
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 #78 on: August 05, 2008, 05:59:02 PM »
This is a classic example of why I don't like nested if,then,else statements.

Duh, remember, that like C/C++, curly braces denote a scope (although not as extensive as C/C++) and that is why your variable for newLTTR is going out of scope - it's DECLARED and INITIALISED in the above 'else' clause, which as luck would have it, has....you guessed it - curly braces.

In this sort of situation, at the begining of the function do this:

LinetypeTableRecord newLTTR = null;

That way, it is accessible for the rest of the 'nested' scopes defined by the curly brace code blocks.

Me

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #79 on: August 05, 2008, 06:09:25 PM »
Thanks Glenn,  I knew I would have to declare it outside of the else, I just wanted to make it the best possible way I could.
« Last Edit: August 05, 2008, 06:15:10 PM by CmdrDuh »
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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #80 on: August 05, 2008, 06:14:46 PM »
I am rewriting all that code to be more user friendly.  Classic example of the drivel I put together, and come back to later and say What was I thinking.  I am going to have to do some reading up on XML and Datasets and maybe LINQ because I have no clue what the rowIndex of my table is doing.
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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #81 on: August 05, 2008, 06:18:43 PM »
Here is what I have so far on the re-write
Code: [Select]
        [CommandMethod("LTree")]
        static public void createXMLLayer()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            DataSet dsLayerData = new DataSet();
            dsLayerData.ReadXml(@"c:\CS_TEPLayers.xml");
            DataView dsLayerDefinition = new DataView(dsLayerData.Tables["Layer"]);
            dsLayerDefinition.Sort = "LayerName";

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, false) as LayerTable;
                string LayerName = null;
                if (lt == null)
                    return;


                int rowIndex = 1;
                LayerName = (string)dsLayerDefinition[rowIndex]["LayerName"];
                ++rowIndex;
                LayerName = (string)dsLayerDefinition[rowIndex]["LayerName"];
I am trying to see how it reads the file, and how to use my rowindex.  I hard coded the index to 1 and then pulled the LayerName for that row.  It did not do what I thought it would do, It pulled from row 90ish.  Then the second time through, it pulled from 85ish.  Bottem line is I have no clue what Im doing, so I am going to have to read up on this tonight
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 #82 on: August 05, 2008, 06:19:10 PM »
That's how you learn mate - an avenue you decide to go down to learn and you emerge a week later going 'WOW'....now what was I doing... :)
Me

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #83 on: August 05, 2008, 06:26:47 PM »
Well, the lightbulb lit up for a second, but quickly went back off.  I thought that because I was doing a sort, that was why the rowindex gave me a result I didn't expect.  Turns out, if it did the sort it claims, I am still way off
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

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: create Layers, is there a better way
« Reply #84 on: August 05, 2008, 06:27:23 PM »
huh, what were we talking about?   :wink:
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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: create Layers, is there a better way
« Reply #85 on: August 05, 2008, 07:53:38 PM »


Quote
<snip> an avenue you decide to go down to learn and you emerge .... <snip>

I wish I could make the time to take a trip down that road .... I'm overdue for an excursion ... :)
kdub in one timeline.
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<-