如何在 C# 中将列表中的项目添加到 AutoCAD 文件?
How to add items from a list to an AutoCAD file in C#?
我正在编写一个 windows 表单应用程序(插件),它使用 C# 在 AutoCAD 中根据选定的列表框项目创建图层。刚接触编程,如有错误请多多包涵
我创建了一个方法,该方法 returns 从列表框中选择图层的列表。现在,我想将列表中的这些图层添加到我的 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();
}
}
}
}
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;
}
}
编辑:
实用程序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;
}
}
}
对于线型,您需要 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);
我正在编写一个 windows 表单应用程序(插件),它使用 C# 在 AutoCAD 中根据选定的列表框项目创建图层。刚接触编程,如有错误请多多包涵
我创建了一个方法,该方法 returns 从列表框中选择图层的列表。现在,我想将列表中的这些图层添加到我的 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();
}
}
}
}
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;
}
}
编辑:
实用程序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;
}
}
}
对于线型,您需要 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);