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

标签 c# autocad-plugin

我正在编写一个 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;
            }
        }
    }

最佳答案

对于线型,您需要LinetypeTable:

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

关于c# - 如何在 C# 中将列表中的项目添加到 AutoCAD 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47535591/

相关文章:

c# - 进行回滚 - 存储库集成测试

lisp - 一个实体中的对象 react 器 "copied"和 "modified"出错了 - 需要帮助

c# - 有没有办法使用 C# 获取 autocad (.dwg) 中的所有多段线?

c# - 指定的参数超出有效值范围。参数名称: size

c# - ASP.NET Core 2 中多个相同类型实例的依赖注入(inject)

控制 AutoCAD 相机的 C# 代码

c# - 在 C# 中检索 AutoCAD 对象的属性

c# - 中继器内的按钮 Onclick

c# - 我怎样才能找到整数中9的个数