CAD.net二次开发之图层,文字样式,标注样式,标注封装,引线的封装

1.图层,线形状,的创立

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.Windows;
using application = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.Colors;
using DotNetARX;
namespace steelBridge.verticalView.Label
{
    class LaryerColor
    {

        //layer library图层库
        public void layerLibrary(string layerName)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //结构红色、线形continues,线宽0.35 structure
                if (layerName == "结构")
                {
                    db.AddLayer(layerName);
                    db.SetLayerColor(layerName, 1);
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //辅助线白色auxiliary
                if (layerName == "辅助线")
                {
                    db.AddLayer(layerName);

                    db.SetLayerColor(layerName, 7);
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //标题下划线蓝色underline
                if (layerName == "标题下划线")
                {
                    db.AddLayer(layerName);

                    db.SetLayerColor(layerName, 5);
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //标注绿色label
                if (layerName == "标注")
                {
                    db.AddLayer(layerName);

                    db.SetLayerColor(layerName, 3);
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //附注白色annotation
                if (layerName == "附注")
                {
                    db.AddLayer(layerName);

                    db.SetLayerColor(layerName, 7);
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //文本白色text
                if (layerName == "文本")
                {
                    db.AddLayer(layerName);

                    db.SetLayerColor(layerName, 7);
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //表格外框线红色TableOutline
                if (layerName == "表格外框线")
                {
                    db.AddLayer(layerName);

                    db.SetLayerColor(layerName, 1);
                    //线形
                    db.SetLayerLineType(layerName, "Center");
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
                //中心线黄色CentreLine
                if (layerName == "中心线")
                {
                    db.AddLayer(layerName);
                    db.SetLayerColor(layerName, 2);
                    //db.LoadLineType("Center");
                    //设立线性

                    //ObjectId centerId3 = db.LoadLineType("Center");
                    db.SetLayerLineType(layerName, "CENTER");
                    db.SetCurrentLayer(layerName);


                    trans.Commit();
                }
                //虚线黄色Imaginaryline
                if (layerName == "虚线")
                {
                    db.AddLayer(layerName);
                    db.SetLayerColor(layerName, 2);
                    db.SetLayerLineType(layerName, "DASHED");
                    db.SetCurrentLayer(layerName);
                    trans.Commit();
                }
            }
        }

        public void setlayColor()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = db.GetEditor();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                string name1 = "结构";
                string name2 = "辅助线";
                string name3 = "标题下划线";
                string name4 = "标注";
                string name5 = "附注";
                string name6 = "文本";
                string name7 = "表格外框线";
                string name8 = "中心线";
                string name9 = "虚线";
                DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
                ObjectId centerId = db.LoadLineType("CENTER");
                ObjectId centerId2 = db.LoadLineType("continuous");
                ObjectId centerId3 = db.LoadLineType("DASHED");

                verticalView.Label.LaryerColor color = new verticalView.Label.LaryerColor();
                color.layerLibrary(name1);
                color.layerLibrary(name2);
                color.layerLibrary(name3);
                color.layerLibrary(name4);
                color.layerLibrary(name5);
                color.layerLibrary(name6);
                color.layerLibrary(name7);
                color.layerLibrary(name8);
                color.layerLibrary(name9);
                m_DocumentLock.Dispose();
                trans.Commit();
            }
        }
    }
}

如图所示
(上面图层线形的创立)
2文本样式
生成文本名字为新文字的文字样式,并置于当前

  public void addTextStye()
        {
             Database db=HostApplicationServices.WorkingDatabase;
            Editor ed=Application.DocumentManager.MdiActiveDocument.Editor;
            using (Transaction trans=db.TransactionManager.StartTransaction())
            {
                //设置TrueType字体(仿宋体)
                ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx","fsdb.shx");
                DBText txt1 = new DBText();
                txt1.TextString = "新字体";
                txt1.TextStyle = styleId;//文字样式
                TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                str.TextSize = 0;//高度
                str.XScale = 0.75;//宽度因子
                str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                str.DowngradeOpen();//为了安全切换为读的状态    
                db.SetCurrentTextStyle("新字体");
                trans.Commit();
            }

        }

在这里插入图片描述
3,标注样式
标注样式的生成及封装
在这里插入图片描述
在这里插入图片描述
具体封装代码如下

 //这个是对标注样式的封装。这里封装的样式有2,2.5,25,40---200等
        public void NewDimStyle()
        {
            //Database db =Application.DocumentManager.MdiActiveDocument.Database;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            { 
                DimStyleTable DimTabb = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                ObjectId dimId = ObjectId.Null;
                if (!DimTabb.Has("2"))
                {
                    DocumentLock m_DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "2";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 2;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                    m_DocumentLock.Dispose();
                }
                if (!DimTabb.Has("2.5"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "2.5";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 2.5;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    //设置为当前的标注样式
                    trans.AddNewlyCreatedDBObject(newRecord, true);

                }
                if (!DimTabb.Has("5"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "5";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 5;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                }
                if (!DimTabb.Has("10"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "10";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 10;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                }
                if (!DimTabb.Has("25"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "25";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 25;
                  
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                }
                if (!DimTabb.Has("40"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "40";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 40;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                }
                if (!DimTabb.Has("50"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "50";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 50;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                }
                if (!DimTabb.Has("100"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "100";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //设置TrueType字体(仿宋体)
                    //ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    //DBText txt1 = new DBText();
                    //txt1.TextString = "新字体";
                    //txt1.TextStyle = styleId;//文字样式
                    //TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    //str.TextSize = 0;//高度
                    //str.XScale = 0.75;//宽度因子
                    //str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    //str.DowngradeOpen();//为了安全切换为读的状态    
                    //db.SetCurrentTextStyle("新字体");
                    //newRecord.Dimtxsty = styleId;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    //比例因子
                    newRecord.Dimlfac = 100;
                    //文字样式
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    //并确定事务处理知道要加入圆!
                    //一旦完成以上操作,我们就提交事务处理,这样以上所做的改变就被保存了……
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                   
                }
                if (!DimTabb.Has("200"))
                {
                    DimTabb.UpgradeOpen();
                    DimStyleTableRecord newRecord = new DimStyleTableRecord();
                    //建立标准名字
                    newRecord.Name = "200";
                    //指定标注文字的高度
                    newRecord.Dimtxt = 2.5;
                    //Dimtxsty 指定标注的文字样式。
                    //设置尺寸末端箭头块
                    newRecord.Dimblk = ObjectId.Null;
                    //设置箭头的大小
                    newRecord.Dimasz = 2.5;
                    //控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
                    newRecord.Dimtofl = false;
                    //当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
                    newRecord.Dimgap = 0.625;
                    //小数位数
                    newRecord.Dimdec = 0;
                    文字位置
                    //newRecord.Dimjust = 1;
                    //控制文字相对尺寸线的垂直位置//这个是上方
                    newRecord.Dimtad = 1;
                    //文字对齐
                    newRecord.Dimtoh = false;
                    ObjectId styleId = db.AddTextStyle("新字体", "fsdb_e.shx", "fsdb.shx");
                    DBText txt1 = new DBText();
                    txt1.TextString = "新字体";
                    txt1.TextStyle = styleId;//文字样式
                    TextStyleTableRecord str = styleId.GetObject(OpenMode.ForWrite) as TextStyleTableRecord;
                    str.TextSize = 0;//高度
                    str.XScale = 0.75;//宽度因子
                    str.SetPaperOrientation(true);//文字方向与布局是否匹配           
                    str.DowngradeOpen();//为了安全切换为读的状态    
                    db.SetCurrentTextStyle("新字体");
                    newRecord.Dimtxsty = styleId;
                   
                    //比例因子
                    newRecord.Dimlfac = 200;
                    //newRecord.Dimlim = false;
                    dimId = DimTabb.Add(newRecord);
                    trans.AddNewlyCreatedDBObject(newRecord, true);
                }
                else
                {
                    dimId = DimTabb["100"];
                }
               
                trans.Commit();
               
            }

        }

标注样式的API我网上找的资料如下

  //Dimadec 1.使用 Dimdec 设置的小数位数绘制角度标注;0-8 使用 Dimadec 设置的小数位数绘制角度标注。
  //Dimalt 控制标注中换算单位的显示:关.禁用换算单位 开.启用换算单位
  //Dimaltd 控制换算单位中小数位的位数。
  //Dimaltf 控制换算单位乘数。
  //Dimaltrnd 舍入换算标注单位。
  //Dimalttd 设置标注换算单位公差值小数位的位数。
  //Dimalttz 控制是否对公差值作消零处理。
  //Dimaltu 为所有标注样式族(角度标注除外)换算单位设置单位格式。
  //Dimaltz 控制是否对换算单位标注值作消零处理。Dimaltz 值为 0 - 3 时只影响英尺-英寸标注。
  //Dimapost 为所有标注类型(角度标注除外)的换算标注测量值指定文字前缀或后缀(或两者都指定)。
  //Dimaso 控制标注对象的关联性。
  //Dimassoc 控制标注对象的关联性。
  //Dimasz 控制尺寸线、引线箭头的大小。并控制钩线的大小。
  //Dimatfit 当尺寸界线的空间不足以同时放下标注文字和箭头时,本系统变量将确定这两者的排列方式。
  //Dimaunit 设置角度标注的单位格式:0.十进制度数 1.度/分/秒 2.百分度 3.弧度
  //Dimazin 对角度标注作消零处理。
  //Dimblk 设置尺寸线或引线末端显示的箭头块。
  //Dimblk1 当 Dimsah 系统变量打开时,设置尺寸线第一个端点的箭头。
  //Dimblk2 当 Dimsah 系统变量打开时,设置尺寸线第二个端点的箭头。
  //Dimcen 控制由 Dimcenter、 Dimdiameter 和 Dimradius 命令绘制的圆或圆弧的圆心标记和中心线图形。
  //Dimclrd 为尺寸线、箭头和标注引线指定颜色。同时控制由 leader 命令创建的引线颜色。
  //Dimclre 为尺寸界线指定颜色。
  //Dimclrt 为标注文字指定颜色。
  //Dimdec 设置标注主单位显示的小数位位数。精度基于选定的单位或角度格式。
  //Dimdle 当使用小斜线代替箭头进行标注时,设置尺寸线超出尺寸界线的距离。
  //Dimdli 控制基线标注中尺寸线的间距。
  //Dimdsep 指定一个单字符作为创建十进制标注时使用的小数分隔符。
  //Dimexe 指定尺寸界线超出尺寸线的距离。
  //Dimexo 指定尺寸界线偏移原点的距离。
  //Dimfit 旧式,除用于保留脚本的完整性外没有任何影响。Dimfit 被 Dimatfit 系统变量和 Dimtmove 系统变量代替。
  //Dimfrac 在 Dimlunit 系统变量设置为:4(建筑)或 5(分数)时设置分数格式,0.水平 1.斜 2.不堆叠
  //Dimgap 当尺寸线分成段以在两段之间放置标注文字时,设置标注文字周围的距离。
  //Dimjust 控制标注文字的水平位置。
  //Dimldrblk 指定引线箭头的类型。要返回默认值(实心闭合箭头显示),请输入单个句点 (.)。
  //Dimlfac 设置线性标注测量值的比例因子。
  //Dimlim 将极限尺寸生成为默认文字。
  //Dimlunit 为所有标注类型(除角度标注外)设置单位制。
  //Dimlwd 指定尺寸线的线宽。其值是标准线宽。-3.bylayer -2.byblock 整数代表百分之一毫米的倍数
  //Dimlwe 指定尺寸界线的线宽。其值是标准线宽。-3 bylayer -2 byblock 整数代表百分之一毫米的倍数
  //Dimpost 指定标注测量值的文字前缀或后缀(或者两者都指定)。
  //Dimrnd 将所有标注距离舍入到指定值。
  //Dimsah 控制尺寸线箭头块的显示。
  //Dimscale 为标注变量(指定尺寸、距离或偏移量)设置全局比例因子。同时还影响 leader 命令创建的引线对象的比例。
  //Dimsd1 控制是否禁止显示第一条尺寸线。
  //Dimsd2 控制是否禁止显示第二条尺寸线。
  //Dimse1 控制是否禁止显示第一条尺寸界线:关.不禁止显示尺寸界线 开.禁止显示尺寸界线
  //Dimse2 控制是否禁止显示第二条尺寸界线:关.不禁止显示尺寸界线 开.禁止显示尺寸界线
  //Dimsho 旧式,除用于保留脚本的完整性外没有任何影响。
  //Dimsoxd 控制是否允许尺寸线绘制到尺寸界线之外:关.不消除尺寸线 开.消除尺寸线
  //Dimstyle Dimstyle 既是命令又是系统变量。作为系统变量,Dimstyle 将显示当前标注样式。
  //Dimtad 控制文字相对尺寸线的垂直位置。
  //Dimtdec 为标注主单位的公差值设置显示的小数位位数。
  //Dimtfac 按照 Dimtxt 系统变量的设置,相对于标注文字高度给分数值和公差值的文字高度指定比例因子。
  //Dimtih 控制所有标注类型(坐标标注除外)的标注文字在尺寸界线内的位置。
  //Dimtix 在尺寸界线之间绘制文字。
  //Dimtm 在 Dimtol 系统变量或 Dimlim 系统变量为开的情况下,为标注文字设置最小(下)偏差。
  //Dimtmove 设置标注文字的移动规则。
  //Dimtofl 控制是否将尺寸线绘制在尺寸界线之间(即使文字放置在尺寸界线之外)。
  //Dimtoh 控制标注文字在尺寸界线外的位置:0或关.将文字与尺寸线对齐 1或开.水平绘制文字
  //Dimtol 将公差附在标注文字之后。将 Dimtol 设置为“开”,将关闭 Dimlim 系统变量。
  //Dimtolj 设置公差值相对名词性标注文字的垂直对正方式:0.下 1.中间 2.上
  //Dimtp 在Dimtol 或 Dimlim 系统变量设置为开的情况下,为标注文字设置最大(上)偏差。Dimtp 接受带符号的值。
  //Dimtsz 指定线性标注、半径标注以及直径标注中替代箭头的小斜线尺寸。
  //Dimtvp 控制尺寸线上方或下方标注文字的垂直位置。当 Dimtad 设置为关时,autocad 将使用 Dimtvp 的值。
  //Dimtxsty 指定标注的文字样式。
  //Dimtxt 指定标注文字的高度,除非当前文字样式具有固定的高度。
  //Dimtzin 控制是否对公差值作消零处理。
  //Dimunit 旧式,除用于保留脚本的完整性外没有任何影响。Dimunit 被 Dimlunit 和 Dimfrac 系统变量代替。
  //Dimupt 控制用户定位文字的选项。0光标仅控制尺寸线的位置 1或开 光标控制文字以及尺寸线的位置
  //Dimzin 控制是否对主单位值作消零处理。

4标注的封装
(1)水平标注封装

        //整体放大比例采用前面输入
        //水平标注的封装;水平标注的封装;水平标注的封装
        //分别输入标注起始点,末尾点,标注文字,垂直距离(),整体标注的移动的开始点,结束点,以及放大比例
        public void addHorizonRotatedDimension(Point3d pt1, Point3d pt2, string text, double length, Point3d startpoint, Point3d endpoint, int fontNumber)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {   //水平移动的开始点,结束点
                Point3d startPoint = startpoint;
                Point3d endPoint = endpoint;

                RotatedDimension dimRotated = new RotatedDimension();
                dimRotated.XLine1Point = pt1;
                dimRotated.XLine2Point = pt2;
                dimRotated.DimLinePoint = GeTools.MidPoint(pt1, pt2).PolarPoint(-Math.PI / 2, length);
                dimRotated.DimensionText = text;//<>代表标注的主尺寸,此处在标注线上插入文字
                dimRotated.DimensionStyle = db.Dimstyle;//设置标注样式为当前样式
                //将标注整体放大平移
                dimRotated.Scale(startPoint, fontNumber);
                dimRotated.Move(startPoint, endPoint);
                //进行标注
                db.AddToModelSpace(dimRotated);

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

(2)垂直标注封装

//整体放大比例采用前面输入
        //垂直标注的封装;垂直标注的封装;垂直标注的封装;
        //分别输入标注起始点,末尾点,标注文字,垂直距离(),整体标注的移动的开始点,结束点,以及放大比例
        public void addVerticalRotatedDimension(Point3d pt1, Point3d pt2, string text, double length, Point3d startpoint, Point3d endpoint, int fontNumber)
        {

            Point3d startPoint = startpoint;
            Point3d endPoint = endpoint;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //垂直标注
                RotatedDimension dimRotated = new RotatedDimension();
                dimRotated.Rotation = Math.PI / 2;
                //开始点,结束点
                dimRotated.XLine1Point = pt1;
                dimRotated.XLine2Point = pt2;

                dimRotated.DimLinePoint = GeTools.MidPoint(pt1, pt2).PolarPoint(0, length);

                dimRotated.DimensionText = text;//<>代表标注的主尺寸,此处在标注线上插入文字
                dimRotated.DimensionStyle = db.Dimstyle;//设置标注样式为当前样式
                //将标注整体放大平移
                dimRotated.Scale(startPoint, fontNumber);
                dimRotated.Move(startPoint, endPoint);

                db.AddToModelSpace(dimRotated);

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

(3)对齐标注封装

  //整体放大比例采用前面输入
        //对齐标注的封装;对齐标注的封装;对齐标注的封装,,也就是倾斜标注的封装
        //分别输入标注起始点,末尾点,标注文字,垂直距离(),整体标注的移动的开始点,结束点,以及放大比例
            public void alignedDimension(Point3d pt1, Point3d pt2, string text, double length, Point3d startpoint, Point3d endpoint, int fontNumber)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {   //水平移动的开始点,结束点
                Point3d startPoint = startpoint;
                Point3d endPoint = endpoint;
                //倾斜标注的封装
                
                AlignedDimension dimRotated = new AlignedDimension();

                dimRotated.XLine1Point = pt1;
                dimRotated.XLine2Point = pt2;
                dimRotated.DimLinePoint = GeTools.MidPoint(pt1, pt2).PolarPoint(-Math.PI / 2, length);
                dimRotated.DimensionText = text;//<>代表标注的主尺寸,此处在标注线上插入文字
                dimRotated.DimensionStyle = db.Dimstyle;//设置标注样式为当前样式
                //将标注整体放大平移
                dimRotated.Scale(startPoint, fontNumber);
                dimRotated.Move(startPoint, endPoint);
                //进行标注
                db.AddToModelSpace(dimRotated);

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

(4)半径标注

        //半径标注的封装;半径标注的封装;半径标注的封装;
        //半径标注的封装;半径标注的封装;半径标注的封装;
        //半径标注的封装;半径标注的封装;半径标注的封装;
        //分别输入标注圆心,半径(也就是距离圆心的距离),线的长度,标注度数,开始点,结束点,放大倍数
        public void addRadialDimension(Point3d cen, double R, double lo, double de, Point3d startpoint, Point3d endpoint, double fontNumber)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                Point3d startPoint = startpoint;
                Point3d endPoint = endpoint;

                Circle cir1 = new Circle(cen, Vector3d.ZAxis, R);
                //List<Dimension> dims = new List<Dimension>();
                // 创建半径标注
                RadialDimension dimRadial = new RadialDimension();
                dimRadial.Center = cir1.Center;//圆或圆弧的圆心
                dimRadial.ChordPoint = cir1.Center.PolarPoint(GeTools.DegreeToRadian(de), R);
                dimRadial.LeaderLength = lo;//引线长度

                dimRadial.Scale(startPoint, fontNumber);
                dimRadial.Move(startPoint, endPoint);

                dimRadial.DimensionStyle = db.Dimstyle;//设置标注样式为当前样式

                db.AddToModelSpace(dimRadial);

                trans.Commit();//提交更改

            }
        }

5 引线的封装


            //引线
            //分别输入标注起始点,末尾点,标注文字,垂直距离(),整体标注的移动的开始点,结束点,以及放大比例
            public void leaderLine(Point3d pt1, Point3d pt2, string text, Point3d startpoint, Point3d endpoint, int fontNumber)
            {
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {

              //水平移动的开始点,结束点
                Point3d startPoint = startpoint;
                Point3d endPoint = endpoint;
                //创建一个多行文本并设置其内容为4Xφd±0.005(其中d为圆的直径)
                MText txt=new MText();

                txt.Contents = text;
                txt.Location = pt2;//文本位置
                txt.TextHeight = 0.2;//文本高度 

                txt.Scale(startPoint, fontNumber);
                txt.Move(startPoint, endPoint);
                db.AddToModelSpace( txt);//将圆和文本添加到模型空间中 
                Leader leader=new Leader();//创建一个引线对象
                //将圆上一点及文本位置作为引线的顶点\
                //(Math.PI / 3这个是标注的角度
                leader.AppendVertex(pt1.PolarPoint(Math.PI / 3, 0));
                leader.AppendVertex(txt.Location);



               

                leader.Scale(startPoint, fontNumber);
                leader.Move(startPoint, endPoint);

                db.AddToModelSpace(leader);//将引线添加到模型空间中

                leader.Dimgap = 0.625;//设置引线的文字偏移为0.1
                leader.Dimasz = 2.5;//设置引线的箭头大小为2.5
               
                leader.Annotation = txt.ObjectId;//设置引线的注释对象为文本
                leader.EvaluateLeader();//计算引线及其关联注释之间的关系



              
                trans.Commit();//提交更改
            }        
                
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值