- 积分
- 84
- 明经币
- 个
- 注册时间
- 2012-10-24
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
小弟最近在做的一个小项目,想从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;[assembly: CommandClass(typeof(MyFirstProject1.Class1))]namespace MyFirstProject1{ public class Class1 { [CommandMethod("SetLayer")] 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[1]; int i; short [] excelcolor = new short [p]; double[] excelweight = new double[p]; string[] excelname = new string[p]; string[] excellinetype = new string[p]; for (i = 1; i <= p; i++) { Excel.Range range1 = (Excel.Range)xlsSheet.Cells[i, 3]; excelcolor[i - 1] = Convert.ToInt16(range1.Value); } for (i = 1; i <= p; i++) { Excel.Range range2 = (Excel.Range)xlsSheet.Cells[i, 4]; excelweight[i - 1] = Convert.ToDouble(range2.Value); } for (i = 1; i <= p; i++) { Excel.Range range3 = (Excel.Range)xlsSheet.Cells[i, 1]; excelname[i - 1] = Convert.ToString(range3.Value); } for (i = 1; i <= p; i++) { Excel.Range range4 = (Excel.Range)xlsSheet.Cells[i, 5]; excellinetype[i - 1] = 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[p]; for (i = 1; i <= p; i++) { sLayerNames[i-1] = excelname[i-1]; } // Define an array of colors for the layers Color[] acColors = new Color[p]; for (i = 1; i <= p; i++) { acColors[i-1] = Color.FromColorIndex(ColorMethod.ByAci, excelcolor[i - 1]); } LineWeight[] acLineWeights = new LineWeight[p]; acLineWeights[0] = LineWeight.LineWeight005; acLineWeights[1] = LineWeight.LineWeight009; acLineWeights[2] = LineWeight.LineWeight013; acLineWeights[3] = LineWeight.LineWeight005; acLineWeights[4] = LineWeight.LineWeight009; acLineWeights[5] = LineWeight.LineWeight013; acLineWeights[6] = LineWeight.LineWeight070; string[] Linetypes = new string[p]; for (i = 1; i <= p; i++) { Linetypes[i-1] = excellinetype[i-1]; } 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[sLayerName], OpenMode.ForWrite) as LayerTableRecord; } // Set the color of the layer acLyrTblRec.Color = acColors[nCnt]; // 设置图层线宽 acLyrTblRec.LineWeight = acLineWeights[nCnt]; // 以只读方式打开图层表 Open the Layer table for read LinetypeTable acLinTbl; acLinTbl = acTrans.GetObject(acCurDb.LinetypeTableId, OpenMode.ForRead) as LinetypeTable; if (acLinTbl.Has(Linetypes[nCnt]) == true) { // Upgrade the Layer Table Record for write acLyrTblRec.UpgradeOpen(); // Set the linetype for the layer acLyrTblRec.LinetypeObjectId = acLinTbl[Linetypes[nCnt]]; } nCnt = nCnt + 1; } // Save the changes and dispose of the transaction acTrans.Commit(); } } }}
|
|