【求助】使用c#在CAD中批量创建图层
小弟最近在做的一个小项目,想从excel文档中批量读取数据到CAD创建图层。采用将excel中的数据存入不同的数组,再在创建CAD图层的时候应用这些数组的方法。
现在对于图层名字,颜色和线型的设置都没有问题。
对于线宽:acLineWeights = LineWeight.LineWeight005;acLyrTblRec.LineWeight = acLineWeights ;可以创建单一的图层线宽。
但是想要将批量创建图层线宽就发生了问题。
我的想法是,
string u = LineWeight005;
acLyrTblRec.LineWeight = LineWeight.(u);
只要这种参数化的设置线宽可以实现,那么就可以实现批量导入。
望有人赐教,不甚感激!(附代码)
如有表达不清,请追加~谢谢~~
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.Geometry;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Colors;using System.Reflection;using System.IO;using Microsoft.Office.Core;using Excel = Microsoft.Office.Interop.Excel;namespace MyFirstProject1{ public class Class1 { public static void SetLayer() { int p = 7; Excel.Application xlsApp = new Excel.Application(); if (xlsApp == null) { return; } xlsApp.Visible = true; Excel.Workbook xlsBook = xlsApp.Workbooks.Open(@"D:\test.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Excel.Worksheet xlsSheet = (Excel.Worksheet)xlsBook.Sheets; int i; short [] excelcolor = new short ; double[] excelweight = new double; string[] excelname = new string; string[] excellinetype = new string; for (i = 1; i <= p; i++) { Excel.Range range1 = (Excel.Range)xlsSheet.Cells; excelcolor = Convert.ToInt16(range1.Value); } for (i = 1; i <= p; i++) { Excel.Range range2 = (Excel.Range)xlsSheet.Cells; excelweight = Convert.ToDouble(range2.Value); } for (i = 1; i <= p; i++) { Excel.Range range3 = (Excel.Range)xlsSheet.Cells; excelname = Convert.ToString(range3.Value); } for (i = 1; i <= p; i++) { Excel.Range range4 = (Excel.Range)xlsSheet.Cells; excellinetype = Convert.ToString(range4.Value); } // 获得当前文档和数据库 Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // 启动一个事务Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // 以只读方式打开图层表 Open the Layer table for read LayerTable acLyrTbl; acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; // Define an array of layer names string[] sLayerNames = new string; for (i = 1; i <= p; i++) { sLayerNames = excelname; } // Define an array of colors for the layers Color[] acColors = new Color; for (i = 1; i <= p; i++) { acColors = Color.FromColorIndex(ColorMethod.ByAci, excelcolor); } LineWeight[] acLineWeights = new LineWeight; acLineWeights = LineWeight.LineWeight005; acLineWeights = LineWeight.LineWeight009; acLineWeights = LineWeight.LineWeight013; acLineWeights = LineWeight.LineWeight005; acLineWeights = LineWeight.LineWeight009; acLineWeights = LineWeight.LineWeight013; acLineWeights = LineWeight.LineWeight070; string[] Linetypes = new string; for (i = 1; i <= p; i++) { Linetypes = excellinetype; } int nCnt = 0; // Add or change each layer in the drawing foreach (string sLayerName in sLayerNames) { LayerTableRecord acLyrTblRec; if (acLyrTbl.Has(sLayerName) == false) { acLyrTblRec = new LayerTableRecord(); // Assign the layer a name acLyrTblRec.Name = sLayerName; // Upgrade the Layer table for write if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen(); // Append the new layer to the Layer table and the transaction acLyrTbl.Add(acLyrTblRec); acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true); } else { // Open the layer if it already exists for write acLyrTblRec = acTrans.GetObject(acLyrTbl, OpenMode.ForWrite) as LayerTableRecord; } // Set the color of the layer acLyrTblRec.Color = acColors; // 设置图层线宽 acLyrTblRec.LineWeight = acLineWeights; // 以只读方式打开图层表 Open the Layer table for read LinetypeTable acLinTbl; acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId, OpenMode.ForRead) as LinetypeTable; if (acLinTbl.Has(Linetypes) == true) { // Upgrade the Layer Table Record for write acLyrTblRec.UpgradeOpen(); // Set the linetype for the layer acLyrTblRec.LinetypeObjectId = acLinTbl]; } nCnt = nCnt + 1; } // Save the changes and dispose of the transaction acTrans.Commit(); } } }}
创建图层线宽
public static void SetLayerLineType(string layerName, LineType lineTypeName)
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Transaction acTrans = acCurDb.TransactionManager.StartTransaction();
using (acTrans)
{
LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable;
if (acLyrTbl.Has(layerName) == true)
{
LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl, OpenMode.ForRead) as LayerTableRecord;
LinetypeTable acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;
string sLineTypeName = lineTypeName.ToString();
if (acLinTbl.Has(sLineTypeName) == true)
{
acLyrTblRec.UpgradeOpen();
acLyrTblRec.LinetypeObjectId = acLinTbl;
acLyrTblRec.DowngradeOpen();
acTrans.Commit();
}
acLinTbl.Dispose();
acLyrTblRec.Dispose();
acLyrTbl.Dispose();
}
}
} 代码没发看,能不能整理一下? 代码确实没发看...
页:
[1]