gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:36 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com