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

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #60 on: January 17, 2008, 02:32:47 PM »
Done that before also SCMD (hide a dwg from casual scrutiny) and you can do it with VBA DBX as well too :)

SomeCallMeDave

  • Guest
Re: create Layers, is there a better way
« Reply #61 on: January 17, 2008, 02:35:57 PM »
Glenn,

Do you know of any drawbacks to reading, via .NET, a database from a non-.dwg file?

Just for my general edification.  :)

Thanks

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #62 on: January 17, 2008, 02:39:45 PM »
WOW, I have hidden stuff in files by changing the extension, but those were just txt files.  I didn't know you could change the extension of "database" files and still have it work.  I guess it makes sense, just never thought of it that way
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #63 on: January 17, 2008, 04:17:30 PM »
Glenn,

Do you know of any drawbacks to reading, via .NET, a database from a non-.dwg file?

Just for my general edification.  :)

Thanks

Just to clarify, it STILL is a dwg file, it just doesn't have the DWG extension...so in answer to your question, no, I haven't experienced any drawbacks as such.

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #64 on: January 17, 2008, 04:18:14 PM »
Just make sure you destroy it.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #65 on: January 17, 2008, 04:38:02 PM »
Just make sure you destroy it.
???
You mean when Im done "using" it in code right?  The word destroy is what threw me.  I thought you would have said dispose or unload or close.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #66 on: January 17, 2008, 04:49:16 PM »
Sorry - my C++ coming out again. Yes, you're right - 'use it and dispose it'.

The golden rule in C++ - if you 'new' it, delete/destroy it...good rule to live by even in the managed world.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #67 on: January 17, 2008, 05:21:52 PM »
thats OK, just asking for claritys sake.  Now that you mention it, RR taught me a little C++ back in the day, and we had to destroy our objects we created.  I just forgot the term.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #68 on: January 18, 2008, 05:17:04 PM »
Thanks guys for the pointers.  I now have 3 methods for creating layers, ea building on the last.  I am now working on loading linetypes.  Kerry your LibTools are definetly helping point me in the direction.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #69 on: January 18, 2008, 05:18:15 PM »
Not that this is anywhere near done, but for those following along, here is where Im at
Code: [Select]
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;

namespace LayerCode
{
    public class LayerCode
    {

        [CommandMethod("CD3")]
        static public void createduhlayer()
        {
            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("DSH"))
                    return;
                LayerTableRecord newLTR = new LayerTableRecord();
                newLTR.Name = "DSH";
                newLTR.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
                // set any other properties

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

                tr.AddNewlyCreatedDBObject(newLTR, true);

                tr.Commit();
            }
            //createLayer("Duh2");
            createLayerXML("Duh3");
        }

        static public void createLayer(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;
                LayerTableRecord newLTR = new LayerTableRecord();
                newLTR.Name = Lname;
                newLTR.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
                // set any other properties

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

                tr.AddNewlyCreatedDBObject(newLTR, true);

                tr.Commit();

            }
        }

        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:\LayerDefinitions.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;
                        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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #70 on: January 18, 2008, 05:20:08 PM »
The linetype piece doesn't work yet so thats what i will work on next.
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #71 on: July 29, 2008, 12:44:15 PM »
this was all going so well, when I came across a new problem.  What if you need to rename a layer?  Is it better to create a new layer and move all objects from the old layer to the new and delete the old layer or can  you just rename a layer in the LayerTable?
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #72 on: July 29, 2008, 03:18:06 PM »
AFAIK, you can rename, as long as the new name DOES not exist in the dbase already...obviously  :-)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: create Layers, is there a better way
« Reply #73 on: July 30, 2008, 10:19:11 AM »
Thanks Glenn.  I meant to come back and update this.  I was making it harder than it needed to be, and I was banging my head against the wall tring to find the "updateDB" option.  Finally i said, lets just see what blows up, and it worked
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.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Glenn R

  • Guest
Re: create Layers, is there a better way
« Reply #74 on: July 30, 2008, 10:35:41 AM »
...lets just see what blows up,

Words to live by! :)