Author Topic: Linetype is purged  (Read 10207 times)

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Guest
Re: Linetype is purged
« Reply #30 on: May 28, 2008, 04:53:04 AM »
Hmmm...can you confirm that Duh? I was under the impression (from an adesk newsgroup post) that this flaw still remained in 2009..............

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Linetype is purged
« Reply #31 on: May 28, 2008, 09:43:10 AM »
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;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace LayerCode
{
    public class LayerCode
    {

        [CommandMethod("DDD")]
        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");
                createLayer("DSH", 2);
                createLayerXML("Duh3");
            //}
            //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 createLayer(string Lname, short ColorNum)
        {
            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, ColorNum);
                // set any other properties

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

                tr.AddNewlyCreatedDBObject(newLTR, true);

                tr.Commit();

            }
        }
        static public void createLayer(string Lname, short ColorNum, string LType)
        {
            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, ColorNum);
                newLTR.LinetypeObjectId = GetOrLoadLinetypeId(LType, db);

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

                tr.AddNewlyCreatedDBObject(newLTR, true);

                tr.Commit();

            }
        }

     
        public static ObjectId GetOrLoadLinetypeId(string linetypeName, Database db)
        {
            ObjectId ltypeId = GetSymbolTableRecId(typeof(LinetypeTableRecord), linetypeName, db);
            if (ltypeId.IsNull)
            {
                string[] ltypeFiles;
                ltypeFiles = new string[3];
                if (db.Measurement == MeasurementValue.English)
                //if (db.Measurement == MeasurementValue.Metric)
                {
                    PrintToCmdLine(" \n Using English Linetypes");
                    ltypeFiles[0] = "acad.lin";
                    ltypeFiles[1] = "acadiso.lin";
                    ltypeFiles[2] = "ltypeshp.lin";
                }
                else
                {
                    PrintToCmdLine(" \n Using Metric Linetypes");
                    ltypeFiles[0] = "acadiso.lin";
                    ltypeFiles[1] = "acad.lin";
                    ltypeFiles[2] = "ltypeshp.lin";
                }
                //---------------
                int len = ltypeFiles.Length;
                for (int i = 0; i < len; i++)
                {
                    try
                    {
                        // try to load the linetype from the external file
                        db.LoadLineTypeFile(linetypeName, ltypeFiles[i]);

                        ltypeId = GetSymbolTableRecId(typeof(LinetypeTableRecord), linetypeName, db);
                        if (ltypeId.IsErased)
                        {
                            using (Transaction tr = db.TransactionManager.StartTransaction())
                            {
                                LinetypeTableRecord lttr = (LinetypeTableRecord)tr.GetObject(ltypeId, OpenMode.ForWrite, true);
                                lttr.Erase(false);
                                tr.Commit();
                            }
                        }
                        if (ltypeId.IsNull == false)
                            return ltypeId;
                    }
                    catch (Autodesk.AutoCAD.Runtime.Exception acEx)
                    {
                        if (acEx.Message == "eUndefinedLineType")
                        {
                            PrintToCmdLine(string.Format(
                                    "\n ERROR: Could not load linetype \"{0}\", using CONTINUOUS instead.",
                                    linetypeName));
                            ltypeId = db.ContinuousLinetype;
                            return ltypeId;
                        }
                        else
                        {
                            PrintToCmdLine(string.Format(
                                "\n ERROR: in 'LibTools.GetOrLoadLinetypeId' loading linetype \"{0}\".",
                                linetypeName));
                            throw acEx;
                        }
                    }
                }
            }
            return ltypeId;
        }
        public static ObjectId GetSymbolTableRecId(System.Type classType, string symName, Database db)
        {
            ObjectId tblId = GetSymbolTableId(classType, db);
            ObjectId recId = new ObjectId();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                SymbolTable tbl = (SymbolTable)tr.GetObject(tblId, OpenMode.ForRead,true);
                if (tbl.Has(symName))   // TBD: should indexer return ObjectId.null instead of throwing exception
                    recId = tbl[symName];
                tr.Commit();
            }
            return recId;
        }
        public static ObjectId GetSymbolTableId(System.Type classType, Database db)
        {
            Debug.Assert(classType != null);
            Debug.Assert(db != null);

            if (classType == typeof(BlockTableRecord))
                return db.BlockTableId;
            else if (classType == typeof(DimStyleTableRecord))
                return db.DimStyleTableId;
            else if (classType == typeof(LayerTableRecord))
                return db.LayerTableId;
            else if (classType == typeof(LinetypeTableRecord))
                return db.LinetypeTableId;
            else if (classType == typeof(TextStyleTableRecord))
                return db.TextStyleTableId;
            else if (classType == typeof(RegAppTableRecord))
                return db.RegAppTableId;
            else if (classType == typeof(UcsTableRecord))
                return db.UcsTableId;
            else if (classType == typeof(ViewTableRecord))
                return db.ViewTableId;
            else if (classType == typeof(ViewportTableRecord))
                return db.ViewportTableId;
            else
            {
                Debug.Assert(false);
                ObjectId nullObj = new ObjectId();
                return nullObj;
            }
        }
        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();
            }
        }
        public static void PrintToCmdLine(string str)
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage(str);
        }
    }
}
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: Linetype is purged
« Reply #32 on: May 28, 2008, 09:46:23 AM »
Posted is the code I'm using, most of which came from Kerry (thanks again) and I am trying every way i can think of to load the linetype, purge it out, and rerun the program and make it crash.  I am seeing some strange display behavior in the layer props box, but it runs great.

On a side note, I guess procrastinating for 6 weeks on this problem was a good thing.  I still need to try and understand what TT was doing and make use of it in case there are other broken items I find.
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)