using System;using System.Collections.Generic;using System.Linq;using System.Text;using Autodesk..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(); } } }}