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

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;
            }
        }
    }

}

C#读取Excel数据在CAD上展图相关推荐

  1. VB.NET读取Excel数据在CAD上展图

    根据网友的需求,编写此程序:读取Excel文件当中的数据,在CAD图上展绘孔位及其桩号与孔深的属性. 本实例包含以下技术要点: 1.如何用代码创建带属性的块对象,而非导入外部图块文件(尤其是带填充对象 ...

  2. 读取excel数据 画k线 成交量图

    # -*- coding: utf-8 -*- """ Created on Tue Mar 23 18:32:15 2021@author: ""& ...

  3. ARCGIS前端--读取excel数据并在地图上显示

    已知一批有经纬度点信息的数据,临时想要在前端地图上展示具体的位置和效果,有三种方法可以实现: 方法一: 传统方法是把数据发布成要素服务,然后在前端调用URL展示,但是当数据变化时,需要重新发布要素服务 ...

  4. python读取excelsheet-一文看懂用Python读取Excel数据

    原标题:一文看懂用Python读取Excel数据 导读:现有的Excel分为两种格式:xls(Excel 97-2003)和xlsx(Excel 2007及以上). Python处理Excel文件主要 ...

  5. python读取表格数据_Python读取Excel数据并根据列名取值

    一直想将自己接触到的东西梳理一遍,可就是迈不出第一步,希望从这篇总结开始不要再做行动的矮人了. 最近测试过程中需要用到python读取excel用例数据,于是去了解和学习了下xlrd库,这里只记录使用 ...

  6. vue-element-xlsx在线读取Excel数据预览

    vue-element-xlsx在线读取Excel数据预览 1.安装XLSX npm install xlsx -s 2.复制过去就可以用 <template><div>< ...

  7. 一文看懂用Python读取Excel数据

    导读:现有的Excel分为两种格式:xls(Excel 97-2003)和xlsx(Excel 2007及以上). Python处理Excel文件主要是第三方模块库xlrd.xlwt.pyexcel- ...

  8. 利用openpyxl来读取Excel数据

    利用openpyxl来读取Excel数据 第二篇博客就这样开始啦. 今天我们来讲如何利用openpyxl来读取Excel当中的数据. 首先,来说一下变更的运行环境 添加 openpyxl 插件 关于插 ...

  9. python读取excel数据使用pyecharts展示

    目录 一.场景 二.思路 三.代码 接上一博文读取[excel数据] 一.场景 场景:数据已经从excel读取完毕了,怎么展示呢 二.思路 思路:1.读取excel数据出来之后,那就是怎么去操作数据 ...

最新文章

  1. 转发和重定向的区别?
  2. python删除文件夹无法访问_零基础小白必看:python基本操作-文件、目录及路径
  3. tcptracerte参数_TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute【转】
  4. 手机pdf格式怎么弄_pdf怎么转html?pdf转html技能分享给你
  5. emui内核支持kvm吗_KVM虚拟化详解
  6. Openwrt 软件安装源
  7. mysql数据库字段字符转数字批量语句_MySQL数据库批量替换指定字段字符串SQL语句命令...
  8. PowerShell 学习笔记 - 2 PS Module
  9. location.reload() 和 location.replace()的区别和应用
  10. pku1088----滑雪(记忆性搜索)
  11. Vue中使用pdf.js通过流的方式传参给pdfviewer.html
  12. 史上最全微信运营工具
  13. html5制作多彩照片墙,照片墙不是乱贴的,教你1分钟打造高逼格照片墙!
  14. 安卓如何调出软键盘_Android软键盘显示模式及打开和关闭方式(推荐)
  15. 构建股票交易平台专业术语
  16. python 语料_用python从语料库中提取最常用的词
  17. Ubuntu 和 CentOS 根据命令查找软件包
  18. 计算机左右手控制的按键,采用改进按键布局方法的计算机键盘技术
  19. Candance Allegro 16.6操作流程补充
  20. 服务器项目混淆,压缩和混淆node.js服务端代码

热门文章

  1. 使用trashcan恢复linux下误删的文件
  2. 怎样把word转换成excel表格格式
  3. 利用python读取excel中的公司名称获取公司的经营范围并回填进excel中
  4. fabric-ca-client 详解动态添加组织
  5. 不用运动快速有效减肥——红光光浴#大健康#红光光浴#红光#种光光学
  6. flutter-拨打电话功能
  7. 【漏洞学习——SQL】华图教育某分站SQL注入漏洞
  8. matlab怎么存盘,matlab怎么保存成word
  9. python文本txt词频统计_python实例:三国演义TXT文本词频分析
  10. 软件测试:什么样的公司需要专职测试?