C#CAD⼆次开发之字体替换⽂字样式处理DBText
1 public static ObjectId AddTextStyle(string styleName, string fontName, bool bold, bool italic, int charset, int pitchAndFamily)
2 {
3 Database db = HostApplicationServices.WorkingDatabase;//获取当前库
4 TextStyleTable st;
5 using (Transaction trans = db.TransactionManager.StartTransaction())//开启事务
6 {
7 //打开⽂字样式表
8 st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
9 if (!st.Has(styleName))//如果不存在名为styleName的⽂字样式,则新建⼀个⽂字样式
10 {
11 //定义⼀个新的的⽂字样式表记录
12 TextStyleTableRecord str = new TextStyleTableRecord();
13 str.Name = styleName;//设置的⽂字样式名
14 //设置⽂字样式的字体
15 try
16 {
17 str.Font = new FontDescriptor(fontName, bold, italic, charset, pitchAndFamily);
18 }
19 catch (System.Exception)
20 {
21 str.Font = new FontDescriptor("微软雅⿊", bold, italic, charset, pitchAndFamily);
22 }
23
24 str.XScale = 0.48;
25 st.UpgradeOpen();//切换的⽂字样式表的状态为写以添加新的的⽂字样式
26 st.Add(str);//将新建的⽂字样式表记录的信息添加到⽂字样式表中
27 //把的⽂字样式表记录添加到事务处理中
28 db.TransactionManager.AddNewlyCreatedDBObject(str, true);
29 st.DowngradeOpen();//为了安全,将⽂字样式表的状态切换为读
30 }
31 trans.Commit();//提交事务
32 }
33 return st[styleName];//返回新添加的的⽂字样式表记录的ObjectId
34 }
35textstyle
36 public static ObjectId AddTextStyle(string styleName, string fontFilename, string bigFontFilename)
37 {
38 Database db = HostApplicationServices.WorkingDatabase;//获取当前库
39 TextStyleTable st;
40 using (Transaction trans = db.TransactionManager.StartTransaction())//开启事务
41 {
42 //打开⽂字样式表
43 st = (TextStyleTable)db.TextStyleTableId.GetObject(OpenMode.ForRead);
44 if (!st.Has(styleName))//如果不存在名为styleName的⽂字样式,则新建⼀个⽂字样式
45 {
46 //定义⼀个新的⽂字样式表记录
47 TextStyleTableRecord str = new TextStyleTableRecord();
48 str.Name = styleName;//设置⽂字样式名
49 str.FileName = fontFilename;//设置字体⽂件名
50 str.BigFontFileName = bigFontFilename;//设置⼤字体⽂件名
51 str.XScale = 0.5;
52 st.UpgradeOpen();//切换⽂字样式表的状态为写以添加新的⽂字样式
53 st.Add(str);//将⽂字样式表记录的信息添加到⽂字样式表中
54 //把⽂字样式表记录添加到事务处理中
55 db.TransactionManager.AddNewlyCreatedDBObject(str, true);
56 st.DowngradeOpen();//为了安全,将⽂字样式表的状态切换为读
57 }
58 trans.Commit();//提交事务
59 }
60 return st[styleName];//返回新添加的⽂字样式表记录的ObjectId
61 }
62
63 //遍历所有的DBtext实体对象
64 //条件是指定的⽂字样式
65 //替换成新的⽂字样式
66 /// <summary>
67 /// ⽂字样式替换
68 /// </summary>
69 /// <param name="BeforeName">原来的样式名</param>
70 /// <param name="AfterName">改后的样式名</param>
71 /// <returns>成功与否</returns>
72 public static bool TextStyleTransForm(string BeforeName, string AfterName)
73 {
74
75 ObjectId afterId = GetTextStyleId(AfterName);//获取指定名称的⽂字样式id
76 Database db = HostApplicationServices.WorkingDatabase;//获取当前库
77 using (Transaction trans = db.TransactionManager.StartTransaction())//开启事务
78 {
79 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;//获取块表
80 BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;//获取模型空间的块表记录
81 foreach (ObjectId item in btr)//遍历记录
82 {
83 Entity ent = trans.GetObject(item, OpenMode.ForRead) as Entity;//根据id获取实体对象
84 if (ent.GetType() == typeof(DBText))//是否是单⾏⽂本
85 {
86 DBText dbtext = (DBText)ent;//转化
87 if (dbtext != null && dbtext.TextStyleName == BeforeName)//不为空,且是指定的⽂字样式名
88 {
89 dbtext.UpgradeOpen();//是需要改的升级权限
90 dbtext.TextStyleId = afterId;//改成新的样式
91 TextStyleTableRecord str = afterId.GetObject(OpenMode.ForRead) as TextStyleTableRecord;
92 dbtext.WidthFactor = str.XScale;//宽度因⼦
93 dbtext.DowngradeOpen(); //修改之后限权
94 }
95 }
96 }
97
98 trans.Commit();//提交事务
99 }
100 return true;
101 }
102
103 /// <summary>
104 /// 获取⽂本样式的id
105 /// </summary>
106 /// <param name="styleName"></param>
107 /// <returns>id</returns>
108 public static ObjectId GetTextStyleId(string styleName)
109 {
110 ObjectId id = new ObjectId();
111 Database db = HostApplicationServices.WorkingDatabase;
112 using (Transaction trans = db.TransactionManager.StartTransaction())
113 {
114 TextStyleTable st = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead);
115 id = st[styleName];
116 trans.Commit();
117 }
118 return id;
119 }
120 /// <summary>
121 /// 获取数据库中所有的⽂字样式的名称
122 /// </summary>
123 /// <returns>ArrayList的数组</returns>
124 public static ArrayList GetTextStyleName()
125 {
126
127 ArrayList textstylename = new ArrayList();
128 using (Database db = HostApplicationServices.WorkingDatabase)
129 {
130
131 using (Transaction trans = db.TransactionManager.StartTransaction())
132 {
133
134 using (TextStyleTable tt = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForRead)) 135 {
136
137 foreach (ObjectId id in tt)
138 {
139 TextStyleTableRecord ttr = (TextStyleTableRecord)trans.GetObject(id, OpenMode.ForRead); 140 textstylename.Add(ttr.Name);
141 }
142 }
143 trans.Commit();
144 }
145 }
146 return textstylename;
147 }
1using System;
2using System.Collections;
3using System.Windows.Forms;
4using DotNetARX;
5namespace CadBasic
6{
7 public partial class TextStyleTransForm : Form
8 {
9 public TextStyleTransForm()
10 {
11 InitializeComponent();
12 }
13
14 private void button1_Click(object sender, EventArgs e)
15 {
16 if (lstBefore.Text == "")
17 {
18 MessageBox.Show("请选择更新前的⽂字样式名称");
19 return;
20 }
21
22 if (lstAfter.Text == "")
23 {
24 MessageBox.Show("请选择更新后的⽂字样式名称");
25 return;
26 }
27
28 //下⾯为可以替换的内容
29 if (TextStyleTools.TextStyleTransForm(lstBefore.Text, lstAfter.Text))
30 MessageBox.Show("操作已经完成~!");
31 this.Hide();
32 }
33
34 private void TextStyleTransForm_Activated(object sender, EventArgs e)
35 {
36
37 TextStyleTools.AddTextStyle("⾃定义", "Romans.shx", "HZFS.SHX");
38 TextStyleTools.AddTextStyle("华⽂新魏", "华⽂新魏", true, false, 0, 0); 39
40 lstBefore.Items.Clear();
41 lstAfter.Items.Clear();
42
43 ArrayList alist = TextStyleTools.GetTextStyleName();
44 lstBefore.Items.AddRange(alist.ToArray());//加⼊列表框内
45 lstAfter.Items.AddRange(alist.ToArray());//加⼊列表框内
46 }
47
48 private void lstBefore_SelectedIndexChanged(object sender, EventArgs e)
49 {
50 lstAfter.Items.Clear();
51 for (int i = 0; i < lstBefore.Items.Count; i++)
52 if (lstBefore.Items[i].ToString() != lstBefore.Text)
53 lstAfter.Items.Add(lstBefore.Items[i].ToString());
54 }
55 }
56}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论