Search code examples
c#autocad-plugin

How to add items from a list to an AutoCAD file in C#?


I am am writing a windows form App(plugin) which create layers in AutoCAD from selected list box items using C#. I am pretty new to programming, please forgive if I made any errors.

I have created a method which returns a list of selected layers from the list box. Now, I would like to add these layers from the list to my AutoCAD file. For this, I came up with a Create Layer function where I am experiencing errors while assigning the layer Properties to the new layer object.

Any help would be appreciated. Thank you.

List:

 public List<layer> Buildlayers()//Build a List of Layers
        {
            List<layer> Finallayers = new List<layer>();
            foreach (layer lname in lbGetLayers.SelectedItems)
            {
                Finallayers.Add(BuildLayer(lname));
            }
            return Finallayers;
        }

Create Layers:

public void Createlayer()
        {
            //Create layer with correct name,color,lineweight,line type
            //if the layer already exists then check for correctness/update.
                List<layer> ACADLayers = Buildlayers();
                foreach (layer IL in ACADLayers)
                {
                    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                    Database db = doc.Database;
                    Editor ed = doc.Editor;
                    using (DocumentLock dl = doc.LockDocument())// prevent from modifying the document
                    {
                        using (var tr = db.TransactionManager.StartTransaction())// start a transaction
                        {
                            using (var lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite))
                            {
                                if (!lt.Has(IL.layername))
                                {
                                    lt.UpgradeOpen();
                                       LayerTableRecord newLayer = new LayerTableRecord();
                                       newLayer.Name = IL.layername;
                                       newLayer.Description = IL.Description;
                                       newLayer.LineWeight = IL.Lineweight;//cannot implicity convert string to int error
                                       newLayer.LinetypeObjectId = IL.Linetype;//cannot implicity convert string to int error
                                       lt.Add(newLayer);
                                       tr.AddNewlyCreatedDBObject(newLayer, true);
                                 }   
                            }
                        tr.Commit();
                        }
                    }
                }    
        }

Class:

   public class layer
    {  
       public string layername { get; set; }
       public string Linetype { get; set; }
       public int? Layercolor { get; set; }
       public string Description { get; set; }
       public string Lineweight { get; set; }
       public override string ToString()
        {
            return layername;

        }
    }

Edit:

Utils Class:

 public class utils
    {
        //Get linetype ID
        public ObjectId GetLineTypeID(Transaction tr, string lt)
        {
            ObjectId result = ObjectId.Null;
            //Get linetype id

            return result;
        }
        public LineWeight GetLineWeight(string lw)//lineweight function
        {
            switch (lw.ToUpper())
            {
                case "0.25":
                    return LineWeight.LineWeight025;
                case "0.35":
                    return LineWeight.LineWeight035;
                case "0.18":
                    return LineWeight.LineWeight018;
                case "0.5":
                    return LineWeight.LineWeight005;
            }
        }
    }

Solution

  • For linetypes, you need the LinetypeTable:

    using (var ltype_table = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead))
    {
        if (ltype_table.Has(IL.Linetype))
        {
            layer.LinetypeObjectId = ltype_table[IL.Linetype];
        }
    }
    

    For line weights, the values are an enum that have special values of -3, -2, and -1, then 0 - 211 in various increments. You'll need to figure out what you allow the user to enter and how you map that to an enum.

    layer.LineWeight = LineWeight.LineWeight030; //30 value
    

    If you have an integer value, then this could work if the value matches with an existing enum value:

    layer.LineWeight = (LineWeight)int.Parse(IL.Lineweight);