[求助] VB.net创建文字样式的几个问题
几位老大编写的《 VBA & VB.NET开发》一书中讲了文字样式的创建等方法。我今天下午在使用的时候,遇到三个不明白的问题:(1)“宋体”的文件名称是什么?
我的测试背景是Win7+VS2010+AutoCAD2008,根据文中提供的方法,到控制面板的字体中找到“宋体”的文件名是simsun.ttc,但是按照下面的代码却没有成功地生成字体样式:
Dim str As New TextStyleTableRecord()
str.Name = "TextStyle-宋体-横向"
str.FileName = "simsun.ttc"
我猜是不是应该为“simsun.ttf”,结果运行后字体名显示为“SimSun-ExtB”,也失败了。那到底是什么啊?
(2)字体样式里面垂直字体如何定义?
找了半天,在系统里都没看到带有“@”符号或有“竖向”、“垂直”关键字的字体文件,网上也找了半天,没有结果。
自己最后终于找到了TextStyle Members的一个属性IsVertical,设置为True后发现也没有成功。怎么办?
以上两个问题总结起来,就是如附件图中所示,需要高手填空:
(3)手工定义的宋体字体样式查不到文件名
《AutoCAD VBA & VB.NET开发》一书“文字样式”章节中有GetTextStyle命令可以显示所用字体的文件名。
我手工设置字体的字体样式为宋体,使用书中的GetTextStyle查询,得到空结果,如下图所示,为什么啊?是不是我的系统有问题?还是其它?
'创建文字样式
Public Shared Function CreateStyle(ByVal textstyleName As String, ByVal font1 As String, ByVal bigfont As String, ByVal anger As Double, ByVal textwidth As Double) As ObjectId
Dim db As Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
' 得到文字样式表
Dim st As TextStyleTable = trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite)
Dim StyleName As String = textstyleName
' 如果名为"工程图"的文字样式不存在,则新建一个文字样式.
If st.Has(StyleName) = True Then
'Dim TextstyleId As ObjectId = st(StyleName)
db.Textstyle = st(StyleName)
trans.Commit()
End If
If st.Has(StyleName) = False Then
' 新建一个文字样式表记录.
Dim str As New TextStyleTableRecord()
' 设置文字样式名.
str.Name = StyleName
' 设置TrueType字体(仿宋体)
If Right(font1, 3) = "ttf" Or Right(font1, 3) = "TTC" Or font1 = "宋体" Then
str.FileName = font1
Else
' 设置SHX字体
str.FileName = font1
'设置大字体.
str.BigFontFileName = bigfont
End If
' 设置倾斜角(弧度).
str.ObliquingAngle = anger * Math.PI / 180
' 设置宽度比例.
str.XScale = textwidth
' 把文字样式表记录添加到文字样式表中.
Dim TextstyleId As ObjectId = st.Add(str)
' 把文字样式表记录添加到事务处理中.
trans.AddNewlyCreatedDBObject(str, True)
' 将文字样式"工程图"设置为当前文字样式
db.Textstyle = TextstyleId
trans.Commit()
End If
End Using
End Function Dim st As TextStyleTable = trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite)
我的怎么只能用读的方式打开呢,用写报错啊!
"Autodesk.AutoCAD.Runtime.Exception"的异常
cad2005 & vs2008 谢谢楼主和各位大神的解答,本人小白只能慢慢领悟 手动创建样式,然后用代码读属性。。。 版主啊,我也是想这么做的呀,可是出现了问题,问题就是我发的帖子中的第三个问题。
public void test24()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTableRecord tstr =
new TextStyleTableRecord
{
Name = "Font1",
Font =
new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("宋体",false,false,0,34),
XScale = 1
};
TextStyleTable tst = tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
tst.Add(tstr);
tr.AddNewlyCreatedDBObject(tstr, true);
tr.Commit();
}
}
非常感谢lzh741206的热心。
使用Font属性之后,问题就解决了。
new Autodesk.AutoCAD.GraphicsInterface.FontDescriptor("宋体",false,false,0,34)里面的参数“34”为“the pitch and family attributes”,设置字体间距与Family(字符集?),34指的是DefaultPitch吧?
根据lzh741206和ObjectARX编程站sbydo的提示,问题解决了。下面总结一下,给后面的同志们作参考。
【方法1】创建文字样式时,使用FileName属性来实现。
Dim str As New TextStyleTableRecord()
str.FileName= "宋体.ttf"
或者
str.FileName= "@宋体.ttf"
【方法2】创建文字样式时,使用Font属性来实现。
str.Font = New Autodesk.AutoCAD.GraphicsInterface.FontDescriptor(FontName, False, False, 0, 34)
其中,参数FontName的不同取值可以创建不同的文字样式:
FontName="宋体",则为横向的宋体样式;
FontName="@宋体",则为竖向的宋体样式。
其它的字体样式同样处理。
Dim tktextstyId As ObjectId = ModelSpace.CreateStyle("图框", "宋体", "", 0, 0.85)
页:
[1]
2