C#读取Excel数据在CAD上展图

来源:互联网 发布:win10 iis php 配置 编辑:程序博客网 时间:2024/04/30 16:08

根据网友的需求,将<<读取Excel数据在CAD上展图>>的VB.NET版本转为C#版本

 


 

本实例包含以下技术要点:

1.如何用代码创建带属性的块对象,而非导入外部图块文件(尤其是带填充对象的图块).

2.如何更改块属性的属性值.

3.如何创建文本样式.

4.如何读取Excel文件当中的数据.

 


 

Excel文件的数据格式:

JKC1 静力触探孔 3045304.377  543717.354  2.630  32.500 
JKC2 静力触探孔 3045617.146  545348.081  3.200  35.800 
JKC3 静力触探孔 3046038.390  546159.911  3.380  35.500 
SKC1 十字板 3045617.739  545346.739  3.200  30.000 
SKC2 十字板 3046138.556  548510.327  2.520  30.000 
SKC3 十字板 3046605.847  555424.066  2.200  30.000 
ZKC1 取土样钻孔 3045384.183  544032.220  2.680  80.000 
ZKC2 取土样钻孔 3045436.779  544468.844  2.720  76.100 
ZKC3 取土样钻孔 3045477.244  544827.897  2.680  80.500 

 


创建图块的源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

 

namespace ReaderExcelDrawMapII
{
    /// <summary>
    /// 创建图块
    /// </summary>
    public class CreateBlock
    {
        /// <summary>
        /// 创建JK图块
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static ObjectId createBlockJK()
        {
            ObjectId blockId = new ObjectId();
            Database db = HostApplicationServices.WorkingDatabase;
            //得到当前文档图形数据库
            BlockTableRecord record = new BlockTableRecord();

 

            //图块名称
            record.Name = "JK";
            record.Origin = new Point3d(0, 0, 0);

 

            //打开事务
            using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
            {

 

                //创建第一个多段线对象
                Point2dCollection pts1 = new Point2dCollection();
                pts1.Add(new Point2d(-3.8, 0.0));
                pts1.Add(new Point2d(+3.8, 0.0));

 

                Polyline pline1 = new Polyline();
                for (int i = 0; i <= pts1.Count - 1; i++)
                {
                    pline1.AddVertexAt(i, pts1[i], 1, 0.4, 0.4);
                }
                pline1.Closed = true;
                pline1.Layer = "0";
                pline1.ColorIndex = 0;
                record.AppendEntity(pline1);

 

                //创建第一个多段线对象
                Point2dCollection pts2 = new Point2dCollection();
                pts2.Add(new Point2d(0.0, -3.8));
                pts2.Add(new Point2d(3.2909, 1.9));
                pts2.Add(new Point2d(-3.2909, 1.9));

 

                Polyline pline2 = new Polyline();
                for (int i = 0; i <= pts2.Count - 1; i++)
                {
                    pline2.AddVertexAt(i, pts2[i], 0, 0.2, 0.2);
                }
                pline2.Closed = true;
                pline2.Layer = "0";
                pline2.ColorIndex = 0;
                record.AppendEntity(pline2);

 

                //创建第一个多段线对象
                Point2dCollection pts3 = new Point2dCollection();
                pts3.Add(new Point2d(0.0, 4.0));
                pts3.Add(new Point2d(0.0, 14.0));
                pts3.Add(new Point2d(28.0, 14.0));

 

                Polyline pline3 = new Polyline();
                for (int i = 0; i <= pts3.Count - 1; i++)
                {
                    pline3.AddVertexAt(i, pts3[i], 0, 0.2, 0.2);
                }
                pline3.Layer = "0";
                pline3.ColorIndex = 0;
                record.AppendEntity(pline3);

 

                AttributeDefinition att1 = new AttributeDefinition();
                att1.Position = new Point3d(13.6683, 18.8785, 0.0);
                att1.Height = 7.8;
                //设置文字高度
                att1.WidthFactor = 0.7;
                //设置宽度因子
                att1.HorizontalMode = TextHorizontalMode.TextMid;
                //设置水平对齐方式
                att1.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置垂直对齐方式
                att1.AlignmentPoint = att1.Position;
                att1.Prompt = "孔号";
                //设置属性提示
                att1.TextString = "JKS1";
                //设置属性的缺省值
                att1.Tag = "孔号";
                //设置属性标签
                att1.Layer = "0";
                att1.TextStyleId = CreateEntity.CreateStyle();
                //指定文本样式
                att1.ColorIndex = 0;
                record.AppendEntity(att1);

 


                AttributeDefinition att2 = new AttributeDefinition();
                att2.Position = new Point3d(13.6683, 8.3528, 0.0);
                att2.Height = 7.8;
                //设置文字高度
                att2.WidthFactor = 0.7;
                //设置宽度因子
                att2.HorizontalMode = TextHorizontalMode.TextMid;
                //设置水平对齐方式
                att2.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置垂直对齐方式
                att2.AlignmentPoint = att2.Position;
                att2.Prompt = "孔深";
                //设置属性提示
                att2.TextString = "0.00";
                //设置属性的缺省值
                att2.Tag = "孔深";
                //设置属性标签
                att2.Layer = "0";
                att2.TextStyleId = CreateEntity.CreateStyle();
                //指定文本样式
                att2.ColorIndex = 0;
                record.AppendEntity(att2);

 

                //以写的方式打开块表
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

 

                //判断图块是否存在
                if (bt.Has(record.Name) == false)
                {
                    //在块表中加入块
                    blockId = bt.Add(record);
                    //通知事务处理
                    trans.AddNewlyCreatedDBObject(record, true);
                    //提交事务
                    trans.Commit();
                }
            }

 

            return blockId;
        }

 

        /// <summary>
        /// 创建MK图块
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static ObjectId createBlockMK()
        {
            ObjectId blockId = new ObjectId();
            Database db = HostApplicationServices.WorkingDatabase;
            //得到当前文档图形数据库
            BlockTableRecord record = new BlockTableRecord();

 

            //图块名称
            record.Name = "MK";
            record.Origin = new Point3d(0, 0, 0);

 

            //打开事务
            using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
            {

 

                //创建第一个多段线对象
                Point2dCollection pts1 = new Point2dCollection();
                pts1.Add(new Point2d(-3.8, 0.0));
                pts1.Add(new Point2d(+3.8, 0.0));

 

                Polyline pline1 = new Polyline();
                for (int i = 0; i <= pts1.Count - 1; i++)
                {
                    pline1.AddVertexAt(i, pts1[i], 1, 0.4, 0.4);
                }
                pline1.Closed = true;
                pline1.Layer = "0";
                pline1.ColorIndex = 0;
                record.AppendEntity(pline1);

 

                //创建第一个多段线对象
                Point2dCollection pts2 = new Point2dCollection();
                pts2.Add(new Point2d(0.0, 4.0));
                pts2.Add(new Point2d(0.0, 14.0));
                pts2.Add(new Point2d(28.0, 14.0));

 

                Polyline pline2 = new Polyline();
                for (int i = 0; i <= pts2.Count - 1; i++)
                {
                    pline2.AddVertexAt(i, pts2[i], 0, 0.2, 0.2);
                }
                pline2.Layer = "0";
                pline2.ColorIndex = 0;
                record.AppendEntity(pline2);

 

                AttributeDefinition att1 = new AttributeDefinition();
                att1.Position = new Point3d(13.6683, 18.8785, 0.0);
                att1.Height = 7.8;
                //设置文字高度
                att1.WidthFactor = 0.7;
                //设置宽度因子
                att1.HorizontalMode = TextHorizontalMode.TextMid;
                //设置水平对齐方式
                att1.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置垂直对齐方式
                att1.AlignmentPoint = att1.Position;
                att1.Prompt = "孔号";
                //设置属性提示
                att1.TextString = "MK1";
                //设置属性的缺省值
                att1.Tag = "孔号";
                //设置属性标签
                att1.Layer = "0";
                att1.TextStyleId = CreateEntity.CreateStyle();
                //指定文本样式
                att1.ColorIndex = 0;
                record.AppendEntity(att1);

 

                AttributeDefinition att2 = new AttributeDefinition();
                att2.Position = new Point3d(13.6683, 8.3528, 0.0);
                att2.Height = 7.8;
                //设置文字高度
                att2.WidthFactor = 0.7;
                //设置宽度因子
                att2.HorizontalMode = TextHorizontalMode.TextMid;
                //设置水平对齐方式
                att2.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置垂直对齐方式
                att2.AlignmentPoint = att2.Position;
                att2.Prompt = "孔深";
                //设置属性提示
                att2.TextString = "0.00";
                //设置属性的缺省值
                att2.Tag = "孔深";
                //设置属性标签
                att2.Layer = "0";
                att2.TextStyleId = CreateEntity.CreateStyle();
                //指定文本样式
                att2.ColorIndex = 0;
                record.AppendEntity(att2);

 

                //以写的方式打开块表
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

 

                //判断图块是否存在
                if (bt.Has(record.Name) == false)
                {
                    //在块表中加入块
                    blockId = bt.Add(record);
                    //通知事务处理
                    trans.AddNewlyCreatedDBObject(record, true);
                    //提交事务
                    trans.Commit();
                }
            }

 

            return blockId;
        }

 

        /// <summary>
        /// 创建SZB图块
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static ObjectId createBlockSZB()
        {
            ObjectId blockId = new ObjectId();
            Database db = HostApplicationServices.WorkingDatabase;
            //得到当前文档图形数据库
            BlockTableRecord record = new BlockTableRecord();

 

            //图块名称
            record.Name = "SZB";
            record.Origin = new Point3d(0, 0, 0);

 

            //打开事务
            using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
            {

 

                //创建第一个多段线对象
                Point2dCollection pts1 = new Point2dCollection();
                pts1.Add(new Point2d(-3.8, 0.0));
                pts1.Add(new Point2d(+3.8, 0.0));

 

                Polyline pline1 = new Polyline();
                for (int i = 0; i <= pts1.Count - 1; i++)
                {
                    pline1.AddVertexAt(i, pts1[i], 1, 0.4, 0.4);
                }
                pline1.Closed = true;
                pline1.Layer = "0";
                pline1.ColorIndex = 0;
                record.AppendEntity(pline1);

 

                //创建第一个多段线对象
                Polyline pline2 = new Polyline();
                pline2.AddVertexAt(0, new Point2d(-3.8, 0.0), 0, 0.2, 0.2);
                pline2.AddVertexAt(1, new Point2d(+3.8, 0.0), 0, 0.2, 0.2);
                pline2.Layer = "0";
                pline2.ColorIndex = 0;
                record.AppendEntity(pline2);

 

                //创建第一个多段线对象
                Polyline pline3 = new Polyline();
                pline3.AddVertexAt(0, new Point2d(0.0, +3.8), 0, 0.2, 0.2);
                pline3.AddVertexAt(1, new Point2d(0.0, -3.8), 0, 0.2, 0.2);
                pline3.Layer = "0";
                pline3.ColorIndex = 0;
                record.AppendEntity(pline3);

 

                //创建第一个多段线对象
                Point2dCollection pts4 = new Point2dCollection();
                pts4.Add(new Point2d(0.0, 4.0));
                pts4.Add(new Point2d(0.0, 14.0));
                pts4.Add(new Point2d(28.0, 14.0));

 

                Polyline pline4 = new Polyline();
                for (int i = 0; i <= pts4.Count - 1; i++)
                {
                    pline4.AddVertexAt(i, pts4[i], 0, 0.2, 0.2);
                }
                pline4.Layer = "0";
                pline4.ColorIndex = 0;
                record.AppendEntity(pline4);

 

                AttributeDefinition att1 = new AttributeDefinition();
                att1.Position = new Point3d(13.6683, 18.8785, 0.0);
                att1.Height = 7.8;
                //设置文字高度
                att1.WidthFactor = 0.7;
                //设置宽度因子
                att1.HorizontalMode = TextHorizontalMode.TextMid;
                //设置水平对齐方式
                att1.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置垂直对齐方式
                att1.AlignmentPoint = att1.Position;
                att1.Prompt = "孔号";
                //设置属性提示
                att1.TextString = "SKS1";
                //设置属性的缺省值
                att1.Tag = "孔号";
                //设置属性标签
                att1.Layer = "0";
                att1.TextStyleId = CreateEntity.CreateStyle();
                //指定文本样式
                att1.ColorIndex = 0;
                record.AppendEntity(att1);

 

                AttributeDefinition att2 = new AttributeDefinition();
                att2.Position = new Point3d(13.6683, 8.3528, 0.0);
                att2.Height = 7.8;
                //设置文字高度
                att2.WidthFactor = 0.7;
                //设置宽度因子
                att2.HorizontalMode = TextHorizontalMode.TextMid;
                //设置水平对齐方式
                att2.VerticalMode = TextVerticalMode.TextVerticalMid;
                //设置垂直对齐方式
                att2.AlignmentPoint = att2.Position;
                att2.Prompt = "孔深";
                //设置属性提示
                att2.TextString = "0.00";
                //设置属性的缺省值
                att2.Tag = "孔深";
                //设置属性标签
                att2.Layer = "0";
                att2.TextStyleId = CreateEntity.CreateStyle();
                //指定文本样式
                att2.ColorIndex = 0;
                record.AppendEntity(att2);

 

                //以写的方式打开块表
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

 

                //判断图块是否存在
                if (bt.Has(record.Name) == false)
                {
                    //在块表中加入块
                    blockId = bt.Add(record);
                    //通知事务处理
                    trans.AddNewlyCreatedDBObject(record, true);
                    //提交事务
                    trans.Commit();
                }
            }

 

            return blockId;
        }

 

        /// <summary>
        /// 创建TC图块
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static ObjectId createBlockTC()
        {
            ObjectId blockId = new ObjectId();
            Database db = HostApplicationServices.WorkingDatabase;
            //得到当前文档图形数据库

 

            //打开事务
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {

 

                //以写的方式打开块表
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

 

                //判断图块是否存在
                if (bt.Has("TC") == false)
                {

 

                    Hatch MyHatch = new Hatch();
                    MyHatch.SetDatabaseDefaults();
                    MyHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");

 

                    BlockTableRecord record = new BlockTableRecord();

 

                    //图块名称
                    record.Name = "TC";
                    record.Origin = new Point3d(0, 0, 0);

 

                    //在块表中加入块
                    blockId = bt.Add(record);
                    trans.AddNewlyCreatedDBObject(record, true);

 

                    //创建第一个多段线对象
                    Point2dCollection pts1 = new Point2dCollection();
                    pts1.Add(new Point2d(-6.346, +2.866));
                    pts1.Add(new Point2d(+6.346, +2.866));
                    pts1.Add(new Point2d(+6.346, -2.866));
                    pts1.Add(new Point2d(-6.346, -2.866));

 

                    Polyline pline1 = new Polyline();
                    for (int i = 0; i <= pts1.Count - 1; i++)
                    {
                        pline1.AddVertexAt(i, pts1[i], 0, 0.4, 0.4);
                    }
                    pline1.Closed = true;
                    pline1.Layer = "0";
                    pline1.ColorIndex = 0;
                    record.AppendEntity(pline1);

 

                    //创建第一个多段线对象
                    Point2dCollection pts2 = new Point2dCollection();
                    pts2.Add(new Point2d(0.0, 2.866));
                    pts2.Add(new Point2d(0.0, 12.866));
                    pts2.Add(new Point2d(28.0, 12.866));

 

                    Polyline pline2 = new Polyline();
                    for (int i = 0; i <= pts2.Count - 1; i++)
                    {
                        pline2.AddVertexAt(i, pts2[i], 0, 0.2, 0.2);
                    }
                    pline2.Closed = true;
                    pline2.Layer = "0";
                    pline2.ColorIndex = 0;
                    record.AppendEntity(pline2);

 

                    //创建第一个多段线对象
                    Point2dCollection pts3 = new Point2dCollection();
                    pts3.Add(new Point2d(+6.346, +2.866));
                    pts3.Add(new Point2d(+6.346, -2.866));
                    pts3.Add(new Point2d(-6.346, -2.866));

 

                    Polyline pline3 = new Polyline();
                    for (int i = 0; i <= pts3.Count - 1; i++)
                    {
                        pline3.AddVertexAt(i, pts3[i], 0, 0, 0);
                    }
                    pline3.Closed = true;
                    pline3.Layer = "0";
                    pline3.ColorIndex = 0;
                    pline3.Closed = true;

 

                    ObjectId objId = record.AppendEntity(pline3);
                    record.AppendEntity(MyHatch);

 

                    AttributeDefinition att1 = new AttributeDefinition();
                    att1.Position = new Point3d(13.6683, 18.8785, 0.0);
                    att1.Height = 7.8;
                    //设置文字高度
                    att1.WidthFactor = 0.7;
                    //设置宽度因子
                    att1.HorizontalMode = TextHorizontalMode.TextMid;
                    //设置水平对齐方式
                    att1.VerticalMode = TextVerticalMode.TextVerticalMid;
                    //设置垂直对齐方式
                    att1.AlignmentPoint = att1.Position;
                    att1.Prompt = "孔号";
                    //设置属性提示
                    att1.TextString = "TS1";
                    //设置属性的缺省值
                    att1.Tag = "孔号";
                    //设置属性标签
                    att1.Layer = "0";
                    att1.TextStyleId = CreateEntity.CreateStyle();
                    //指定文本样式
                    att1.ColorIndex = 0;
                    record.AppendEntity(att1);

 

                    AttributeDefinition att2 = new AttributeDefinition();
                    att2.Position = new Point3d(13.6683, 8.3528, 0.0);
                    att2.Height = 7.8;
                    //设置文字高度
                    att2.WidthFactor = 0.7;
                    //设置宽度因子
                    att2.HorizontalMode = TextHorizontalMode.TextMid;
                    //设置水平对齐方式
                    att2.VerticalMode = TextVerticalMode.TextVerticalMid;
                    //设置垂直对齐方式
                    att2.AlignmentPoint = att2.Position;
                    att2.Prompt = "孔深";
                    //设置属性提示
                    att2.TextString = "0.00";
                    //设置属性的缺省值
                    att2.Tag = "孔深";
                    //设置属性标签
                    att2.Layer = "0";
                    att2.TextStyleId = CreateEntity.CreateStyle();
                    //指定文本样式
                    att2.ColorIndex = 0;
                    record.AppendEntity(att2);

 

                    //通知事务处理
                    trans.AddNewlyCreatedDBObject(pline1, true);
                    trans.AddNewlyCreatedDBObject(pline2, true);
                    trans.AddNewlyCreatedDBObject(pline3, true);
                    trans.AddNewlyCreatedDBObject(att1, true);
                    trans.AddNewlyCreatedDBObject(att2, true);
                    trans.AddNewlyCreatedDBObject(MyHatch, true);

 

                    ObjectIdCollection Ids = new ObjectIdCollection();
                    Ids.Add(objId);
                    MyHatch.Associative = true;
                    MyHatch.AppendLoop(HatchLoopTypes.Default, Ids);
                    MyHatch.EvaluateHatch(true);
                    MyHatch.Layer = "0";
                    MyHatch.ColorIndex = 0;

 

                    //提交事务
                    trans.Commit();
                }
            }
            return blockId;
        }

 

        /// <summary>
        /// 创建ZK图块
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static ObjectId createBlockZK()
        {
            ObjectId blockId = new ObjectId();
            Database db = HostApplicationServices.WorkingDatabase;
            //得到当前文档图形数据库

 

            //打开事务
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //以写的方式打开块表
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

 

                //判断图块是否存在
                if (bt.Has("ZK") == false)
                {

 

                    Hatch MyHatch = new Hatch();
                    MyHatch.SetDatabaseDefaults();
                    MyHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");

 

                    BlockTableRecord record = new BlockTableRecord();

 

                    //图块名称
                    record.Name = "ZK";
                    record.Origin = new Point3d(0, 0, 0);

 

                    //在块表中加入块
                    blockId = bt.Add(record);
                    trans.AddNewlyCreatedDBObject(record, true);

 

                    //创建第一个多段线对象
                    Point2dCollection pts1 = new Point2dCollection();
                    pts1.Add(new Point2d(-3.8, 0.0));
                    pts1.Add(new Point2d(+3.8, 0.0));

 

                    Polyline pline1 = new Polyline();
                    for (int i = 0; i <= pts1.Count - 1; i++)
                    {
                        pline1.AddVertexAt(i, pts1[i], 1, 0.4, 0.4);
                    }
                    pline1.Closed = true;
                    pline1.Layer = "0";
                    pline1.ColorIndex = 0;
                    record.AppendEntity(pline1);

 

                    //创建第一个多段线对象
                    Point2dCollection pts2 = new Point2dCollection();
                    pts2.Add(new Point2d(0.0, 3.8));
                    pts2.Add(new Point2d(0.0, 14.0));
                    pts2.Add(new Point2d(28.0, 14.0));

 

                    Polyline pline2 = new Polyline();
                    for (int i = 0; i <= pts2.Count - 1; i++)
                    {
                        pline2.AddVertexAt(i, pts2[i], 0, 0.2, 0.2);
                    }
                    pline2.Layer = "0";
                    pline2.ColorIndex = 0;
                    record.AppendEntity(pline2);

 

                    //创建第一个多段线对象
                    Polyline pline3 = new Polyline();
                    pline3.AddVertexAt(0, new Point2d(0.0, -3.8), 1, 0, 0);
                    pline3.AddVertexAt(1, new Point2d(0.0, +3.8), 0, 0, 0);
                    pline3.Layer = "0";
                    pline3.ColorIndex = 0;
                    pline3.Closed = true;

 

                    ObjectId objId = record.AppendEntity(pline3);
                    record.AppendEntity(MyHatch);

 

                    AttributeDefinition att1 = new AttributeDefinition();
                    att1.Position = new Point3d(13.668, 18.878, 0.0);
                    att1.Height = 7.8;
                    //设置文字高度
                    att1.WidthFactor = 0.7;
                    //设置宽度因子
                    att1.HorizontalMode = TextHorizontalMode.TextMid;
                    //设置水平对齐方式
                    att1.VerticalMode = TextVerticalMode.TextVerticalMid;
                    //设置垂直对齐方式
                    att1.AlignmentPoint = att1.Position;
                    att1.Prompt = "孔号";
                    //设置属性提示
                    att1.TextString = "ZKS1";
                    //设置属性的缺省值
                    att1.Tag = "孔号";
                    //设置属性标签
                    att1.Layer = "0";
                    att1.TextStyleId = CreateEntity.CreateStyle();
                    //指定文本样式
                    att1.ColorIndex = 0;
                    record.AppendEntity(att1);

 

                    AttributeDefinition att2 = new AttributeDefinition();
                    att2.Position = new Point3d(13.668, 8.649, 0.0);
                    att2.Height = 7.8;
                    //设置文字高度
                    att2.WidthFactor = 0.7;
                    //设置宽度因子
                    att2.HorizontalMode = TextHorizontalMode.TextMid;
                    //设置水平对齐方式
                    att2.VerticalMode = TextVerticalMode.TextVerticalMid;
                    //设置垂直对齐方式
                    att2.AlignmentPoint = att2.Position;
                    att2.Prompt = "孔深";
                    //设置属性提示
                    att2.TextString = "0.00";
                    //设置属性的缺省值
                    att2.Tag = "孔深";
                    //设置属性标签
                    att2.Layer = "0";
                    att2.TextStyleId = CreateEntity.CreateStyle();
                    //指定文本样式
                    att2.ColorIndex = 0;
                    record.AppendEntity(att2);

 

                    //通知事务处理
                    trans.AddNewlyCreatedDBObject(pline1, true);
                    trans.AddNewlyCreatedDBObject(pline2, true);
                    trans.AddNewlyCreatedDBObject(pline3, true);
                    trans.AddNewlyCreatedDBObject(att1, true);
                    trans.AddNewlyCreatedDBObject(att2, true);
                    trans.AddNewlyCreatedDBObject(MyHatch, true);

 

                    ObjectIdCollection Ids = new ObjectIdCollection();
                    Ids.Add(objId);
                    MyHatch.Associative = true;
                    MyHatch.AppendLoop(HatchLoopTypes.Default, Ids);
                    MyHatch.EvaluateHatch(true);
                    MyHatch.Layer = "0";
                    MyHatch.ColorIndex = 0;

 

                    //提交事务
                    trans.Commit();
                }
            }

 

            return blockId;
        }

 

    }
}



创建实体源码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;

 

namespace ReaderExcelDrawMapII
{
    /// <summary>
    /// 创建实体对象
    /// </summary>
    public class CreateEntity
    {
        /// <summary>
        /// 插入一个带属性的块
        /// </summary>
        /// <param name="blockName">图块名称</param>
        /// <param name="point">插入点</param>
        /// <param name="scale">图块比例</param>
        /// <param name="rotateAngle">图块旋转角度</param>
        /// <param name="KHstring">属性值:孔号</param>
        /// <param name="KSdouble">属性值:孔深</param>
        /// <remarks></remarks>
        public void InsertBlockRefWithAtt(string blockName, Point3d point, Scale3d scale, double rotateAngle, string KHstring, double KSdouble)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                if (!bt.Has(blockName))
                {
                    return;
                }
                BlockTableRecord blockwithatt = (BlockTableRecord)trans.GetObject(bt[blockName], OpenMode.ForRead);
                BlockReference blockRef = new BlockReference(point, bt[blockName]);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                blockRef.ScaleFactors = scale;
                blockRef.Rotation = rotateAngle;
                btr.AppendEntity(blockRef);
                trans.AddNewlyCreatedDBObject(blockRef, true);
                //获取blockName块的遍历器,以实现对块中对象的访问
                BlockTableRecordEnumerator iterator = blockwithatt.GetEnumerator();
                //如果blockName块包含属性
                if (blockwithatt.HasAttributeDefinitions)
                {
                    //利用块遍历器对块中的对象进行遍历
                    while (iterator.MoveNext())
                    {
                        //获取块遍历器当前指向的块中的对象
                        AttributeDefinition attdef = trans.GetObject(iterator.Current, OpenMode.ForRead) as AttributeDefinition;
                        //定义一个新的属性参照对象
                        AttributeReference att = new AttributeReference();
                        //判断块遍历器当前指向的块中的对象是否为属性定义
                        if (attdef != null)
                        {
                            //从属性定义对象中继承相关的属性到属性参照对象中
                            att.SetAttributeFromBlock(attdef, blockRef.BlockTransform);
                            //设置属性参照对象的位置为属性定义的位置+块参照的位置
                            att.Position = attdef.Position + blockRef.Position.GetAsVector();
                            //判断属性定义的名称
                            switch (attdef.Tag)
                            {
                                //设置块参照的属性值
                                case "孔号":
                                    att.TextString = KHstring;
                                    break;
                                case "孔深":
                                    att.TextString = KSdouble.ToString();
                                    break;
                            }

 

                            //判断块参照是否可写,如不可写,则切换为可写状态
                            if (!blockRef.IsWriteEnabled)
                            {
                                blockRef.UpgradeOpen();
                            }
                            //添加新创建的属性参照
                            blockRef.AttributeCollection.AppendAttribute(att);
                            //通知事务处理添加新创建的属性参照
                            trans.AddNewlyCreatedDBObject(att, true);
                        }
                    }
                }
                trans.Commit();//提交事务处理
            }
        }

 

        /// <summary>
        /// 创建文本样式
        /// </summary>
        public static ObjectId CreateStyle()
        {
            ObjectId TextstyleId = new ObjectId();
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
                String StyleName = "hzhz";
                if (st.Has(StyleName) == false)
                {
                    TextStyleTableRecord str = new TextStyleTableRecord();
                    str.Name = StyleName;

 

                    // 设置TrueType字体(黑体)
                    str.FileName = "SIMHEI.TTF";
                    //---------------------------------------------
                    // 设置SHX字体
                    // str.FileName = "gbenor"
                    //设置大字体.
                    // str.BigFontFileName = "gbcbig"
                    // --------------------------------------------
                    str.ObliquingAngle = 15 * Math.PI / 180;
                    str.XScale = 0.67;
                    TextstyleId = st.Add(str);
                    trans.AddNewlyCreatedDBObject(str, true);
                    db.Textstyle = TextstyleId;
                    trans.Commit();
                }
            }
            return TextstyleId;
        }
    }
}



我在工程项目添加了一个窗体及一个按钮,按钮的单击事件代码如下:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
using System.Data.OleDb;
using Autodesk.AutoCAD.Geometry;
using System.Text.RegularExpressions;

namespace ReaderExcelDrawMapII
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Title = "打开展点文件";
            this.openFileDialog1.Filter = "Excle 文件(*.xls)|*xls";
            this.openFileDialog1.FileName = "";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string PathString = this.openFileDialog1.FileName;
                string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + PathString + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
                string strExcel = "select * from [sheet1$]";

                OleDbConnection conn = new OleDbConnection();
                try
                {
                    conn = new OleDbConnection(strConn);
                    conn.Open();
                    OleDbCommand cmd = new OleDbCommand(strExcel, conn);
                    OleDbDataReader read = cmd.ExecuteReader();

                    //得到当前文档图形数据库.
                    Database db = HostApplicationServices.WorkingDatabase;
                    //图形文档加锁
                    using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
                    {

                        //创建图块
                        CreateBlock.createBlockJK();
                        CreateBlock.createBlockMK();
                        CreateBlock.createBlockTC();
                        CreateBlock.createBlockZK();
                        CreateBlock.createBlockSZB();

                        using (Transaction trans = db.TransactionManager.StartTransaction())
                        {

                            //以读方式打开块表.
                            BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                            //以写方式打开模型空间块表记录.
                            BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                            while (read.Read())
                            {

                                //获取坐标值
                                string strX = read.GetValue(2).ToString();
                                string strY = read.GetValue(3).ToString();
                                string strZ = read.GetValue(4).ToString();

                                if (lzxIsNumber(strX) & lzxIsNumber(strY) & lzxIsNumber(strZ))
                                {
                                    Point3d pt = new Point3d(Convert.ToDouble(strX), Convert.ToDouble(strY), Convert.ToDouble(strZ));
                                    string strBlockName = null;
                                    switch (read.GetString(1))
                                    {
                                        case "静力触探孔":
                                        case "静力触探":
                                        case "静探":
                                            strBlockName = "JK";
                                            break;
                                        case "十字板孔":
                                        case "十字板试验孔":
                                        case "十字板":
                                            strBlockName = "SZB";
                                            break;
                                        case "槽探":
                                        case "探槽":
                                            strBlockName = "TC";
                                            break;
                                        case "麻花钻":
                                        case "麻花钻孔":
                                        case "螺纹钻":
                                        case "螺纹钻孔":
                                            strBlockName = "MK";
                                            break;
                                        default:
                                            strBlockName = "ZK";
                                            break;
                                    }

                                    //获取深度值
                                    double douSD = 0;
                                    string strSD = read.GetValue(5).ToString();
                                    if (lzxIsNumber(strSD))
                                    {
                                        douSD = Convert.ToDouble(strSD);
                                    }
                                    else
                                    {
                                        douSD = 0.0;
                                    }
                                    CreateEntity createEntity = new CreateEntity();
                                    createEntity.InsertBlockRefWithAtt(strBlockName, pt, new Scale3d(1), 0, read.GetString(0), douSD);
                                }
                            }
                            trans.Commit();
                        }
                    }
                }
                catch
                {

                }
                conn.Close();
                this.Close();
            }
        }

        /// <summary>
        /// 判断一个string是否可以为数字
        /// </summary>
        /// <param name="strNumber">数字的字符串</param>
        /// <returns>是否为数字</returns>
        public bool lzxIsNumber(String strNumber)
        {
            if (!string.IsNullOrEmpty(strNumber))
            {
                if (strNumber.Trim().Length == 0)
                {
                    return false;
                }
                Regex objNotNumberPattern = new Regex("[^0-9.-]");
                Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
                Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
                String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
                String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
                Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

                return !objNotNumberPattern.IsMatch(strNumber) &&
                !objTwoDotPattern.IsMatch(strNumber) &&
                !objTwoMinusPattern.IsMatch(strNumber) &&
                objNumberPattern.IsMatch(strNumber);
            }
            else
            {
                return false;
            }
        }
    }

}

原创粉丝点击