如何将列表中的项目添加到C#中的AutoCAD文件?

如何将列表中的项目添加到C#中的AutoCAD文件?,c#,autocad-plugin,C#,Autocad Plugin,我正在编写一个windows窗体应用程序(插件),它使用C#从选定的列表框项在AutoCAD中创建图层。我对编程很陌生,如果我犯了任何错误,请原谅 我创建了一个从列表框返回选定图层列表的方法。现在,我想将这些图层从列表添加到AutoCAD文件中。为此,我提出了一个创建层函数,在将层特性指定给新层对象时遇到错误 任何帮助都将不胜感激。多谢各位 列表: public List<layer> Buildlayers()//Build a List of Layers {

我正在编写一个windows窗体应用程序(插件),它使用C#从选定的列表框项在AutoCAD中创建图层。我对编程很陌生,如果我犯了任何错误,请原谅

我创建了一个从列表框返回选定图层列表的方法。现在,我想将这些图层从列表添加到AutoCAD文件中。为此,我提出了一个创建层函数,在将层特性指定给新层对象时遇到错误

任何帮助都将不胜感激。多谢各位

列表:

 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;
        }
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();
                        }
                    }
                }    
        }
   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;

        }
    }
 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;
            }
        }
    }
编辑:

 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;
        }
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();
                        }
                    }
                }    
        }
   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;

        }
    }
 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;
            }
        }
    }
Utils类:

 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;
        }
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();
                        }
                    }
                }    
        }
   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;

        }
    }
 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;
            }
        }
    }

对于线型,您需要
线型表

using (var ltype_table = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForRead))
{
    if (ltype_table.Has(IL.Linetype))
    {
        layer.LinetypeObjectId = ltype_table[IL.Linetype];
    }
}
对于线宽,这些值是一个枚举,其特殊值为
-3
-2
-1
,然后以各种增量为
0
-
211
。您需要弄清楚您允许用户输入什么以及如何将其映射到枚举

layer.LineWeight = LineWeight.LineWeight030; //30 value
如果您有一个整数值,那么如果该值与现有的枚举值匹配,则可以执行此操作:

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

至少对于线型,您需要根据字符串名称查找实际的线型id(您不能通过字符串进行分配)。类似于
LayerTableRecord
LineWeight
LineTypeObjectId
定义为
int
,但您将类的相应属性定义为
string
,因此,您会得到您在备注中提到的类型不匹配错误。@crashmstr。谢谢你的评论。当我将其重写为:int.TryParse(IL.Linetype.ToString(),out int-Ltype)时,我仍然面临错误;newLayer.LinetypeObjectId=Ltype;有没有办法解决这个问题谢谢你的回复。仅供参考,我正在将CSV文件中的所有图层特性(线型、颜色、线宽)读取到列表中。在我的应用程序中,用户只能从值列表中进行选择。如果是这样,并且图形文件中不存在线型,则需要创建它(或者至少使用上面显示的
.Has
测试它是否存在)。我已在创建新类utils的编辑部分更新了上面的代码。我创建了两个函数,用于获取给定字符串的线型ID和线宽对象。无法确定如何返回给定字符串的值。请帮我提建议。谢谢。我不太确定你在问什么,因为我展示的代码应该在给定线型名称的情况下工作,而且你似乎有一些适合线宽的东西。此外,请不要改变你的问题太多,答案不再有效。如果答案有帮助,但您现在有一个更具体的问题,请作为新问题提问。对于线宽,我只是硬编码了csv文件中定义的线宽值。您能建议我如何在create Layer函数中返回给定字符串的正确线宽(根据填充值)?