乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 61|回复: 3

【求助】使用c#在CAD中批量创建图层

[复制链接]

2

主题

2

帖子

1

银币

初来乍到

Rank: 1

铜币
10
发表于 2012-11-12 15:45:00 | 显示全部楼层 |阅读模式
小弟最近在做的一个小项目,想从excel文档中批量读取数据到CAD创建图层。
采用将excel中的数据存入不同的数组,再在创建CAD图层的时候应用这些数组的方法。
现在对于图层名字,颜色和线型的设置都没有问题。
对于线宽:acLineWeights = LineWeight.LineWeight005;acLyrTblRec.LineWeight = acLineWeights ;可以创建单一的图层线宽。
但是想要将批量创建图层线宽就发生了问题。我的想法是,
string u = LineWeight005;
acLyrTblRec.LineWeight = LineWeight.(u)
只要这种参数化的设置线宽可以实现,那么就可以实现批量导入。
望有人赐教,不甚感激!(附代码)
如有表达不清,请追加~谢谢~~

  1. 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();            }        }    }}

回复

使用道具 举报

5

主题

13

帖子

4

银币

初来乍到

Rank: 1

铜币
33
发表于 2012-11-17 13:48:00 | 显示全部楼层
创建图层线宽
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[layerName], 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[sLineTypeName];
                        acLyrTblRec.DowngradeOpen();
                        acTrans.Commit();
                    }
                    acLinTbl.Dispose();
                    acLyrTblRec.Dispose();
                    acLyrTbl.Dispose();
                }
            }
        }
回复

使用道具 举报

32

主题

651

帖子

8

银币

中流砥柱

Rank: 25

铜币
779
发表于 2012-11-17 15:55:00 | 显示全部楼层
代码没发看,能不能整理一下?
回复

使用道具 举报

84

主题

543

帖子

12

银币

中流砥柱

Rank: 25

铜币
886
发表于 2012-11-17 22:56:00 | 显示全部楼层
代码确实没发看...
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-14 21:10 , Processed in 0.388824 second(s), 60 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表