2

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;
            }
        }
    }
abhi
  • 1,920
  • 6
  • 24
  • 27
  • At least for linetype, you need to look up the actual linetype id based on the string name (you cannot assign via a string). – crashmstr Nov 28 '17 at 18:20
  • Looks like `LayerTableRecord` defines `LineWeight` and `LineTypeObjectId` as `int` but you define the corresponding properties of your class as `string`, hence you get the type mismatch errors you mention in the remarks. – blins Nov 28 '17 at 18:25
  • @crashmstr. Thanks for your comment. I am still facing the error when I rewrote it as : int.TryParse(IL.Linetype.ToString(), out int Ltype);newLayer.LinetypeObjectId = Ltype; Is there any way to solve this error – abhi Nov 28 '17 at 19:42

1 Answers1

0

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);
crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • Thank you for your reply. FYI, I am reading all the Layer properties(line type, Color,line weight) from a CSV file into the list. In my app the user could only choose from a list of values. – abhi Nov 28 '17 at 20:16
  • If so, and the line type does not exist in the drawing file, you would need to create it (or at least test for it existing with the `.Has` shown above). – crashmstr Nov 28 '17 at 20:41
  • I have updated the code above in the edit section where I have created a new class utils. I have created two functions for getting the line type ID, Line weight objects for a given string. Not able to figure out how to return the values for a given string. Kindly, help me with your suggestions. Thank you. – abhi Nov 30 '17 at 14:59
  • I'm not really sure what you are asking, as the code I've show should work given a linetype *name*, and you seem to have something that works for line weight. Also, please don't change your question too much that answers are no longer valid. If the answer helped, but you now have a more specific question, ask as new question. – crashmstr Nov 30 '17 at 15:04
  • For lineweight, I just hard coded the lineweight values which are defined in my csv file. Could you suggest me how can I return the correct line weight(from the populated values) for a given string in my create Layer function? – abhi Nov 30 '17 at 15:27
  • `newLayer.LineWeight = utilsInstance.GetLineWeight(IL.Lineweight)`? (although making your `utils` class and methods `static` might make more sense if you want them in a separate class). – crashmstr Nov 30 '17 at 15:30
  • @Crahmstr.Thank you and it works for Line weight. In the same way, In my utils class i am trying to get the line type id using GetLineTypeID function. How can I use your logic in my function and return the Object Id for the given string? newLayer.LinetypeObjectId = utils.GetLineTypeID(tr, IL.Linetype); ? – abhi Nov 30 '17 at 16:03
  • @abhinay yes, it would look like that. – crashmstr Nov 30 '17 at 16:05
  • @Crahmstr How would I frame the logic to return the objectID in my GetLineTypeID function of Utilis class ? Thank you. – abhi Nov 30 '17 at 16:08
  • @abhinay the code would be almost the same as what I've included above, just put into a method. You might want to consider a good book or course on C#. – crashmstr Nov 30 '17 at 16:10
  • @Crahmstr. Thank you, I just removed the Create Layer function in the edit section as it was just redundant and to keep your answers valid. Thank you for your help. – abhi Nov 30 '17 at 16:17