[求助]创建指定样式名的单行文字
// 由插入点、文字内容、文字高度和倾斜角度创建单行文字的函数.public static ObjectId AddText(Point3d position, string textString, ObjectId style, double height, double oblique)
{
try
{
DBText ent = new DBText();
ent.Position = position;
ent.TextString = textString;
ent.Height = height;
ent.Oblique = oblique;
ObjectId entId = AppendEntity(ent);
return entId;
}
catch
{
ObjectId nullId = ObjectId.Null;
return nullId;
}
} public void CreateStyle()
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
TextStyleTable st = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
String StyleName = "工程图";
if (st.Has(StyleName) == false)
{
TextStyleTableRecord str = new TextStyleTableRecord();
str.Name = StyleName;
str.FileName = "simfang.ttf";
//---------------------------------------------
// 设置SHX字体
// str.FileName = "gbenor"
//设置大字体.
// str.BigFontFileName = "gbcbig"
// --------------------------------------------
str.ObliquingAngle = 15 * Math.PI / 180;
str.XScale = 0.67;
ObjectId TextstyleId = st.Add(str);
trans.AddNewlyCreatedDBObject(str, true);
db.Textstyle = TextstyleId;
trans.Commit();
}
}
}
String StyleName = "工程图";
string textStr = "%%u" + "单行文字ABC123" + "%%u";
// 我现在不知道怎么将类型为string的StyleName转成我需要的style(其类型为ObjectId)。。。。。
AddText(new Point3d(0, 0, 0), textStr, style, 0, 0);
<p>上面的两个函数原文取自:</p><p>《AutoCAD VBA&VB.NET开发基础与实例教程》</p><p>作者:曾洪飞 张帆 卢择临编著 书自带光盘</p><p></p><p>如果没有看错,第一个函数可能少了一句。ent.TextStyle = style;</p><p>oblique属性应该是文字倾斜度(一般用不上这个属性)</p><p>应该改为角度Rotation比较实用吧</p> 给个Lisp的建立单行文字的。
;;;(JW-MK_TEXT PT STRING STYLE HEIGHT ANG)
(defun JW-MK_TEXT (PT STRING STYLE HEIGHT ANG)
(if (entmake (list (cons 0 "TEXT")
(cons 100 "AcDbEntity")
(cons 100 "AcDbText")
(cons 1 STRING)
(cons 7 STYLE)
(cons 10 PT)
(cons 40 HEIGHT)
(cons 50 ANG)
)
)
(entlast)
)
)
文字式样记录存在于文字式样表里
首先打开文字式样表,然后
txt.TextStyle = tst
另,创建文字式样记录的函数最好返回Id:
下面是我的自定义类里的函数
public ObjectId AddTextStyle(string name, string smallfont, string bigfont, double height, double xscale)
{
ObjectId id = GetRecordId(m_TextStyleTable, name);
if (id == ObjectId.Null)
{
TextStyleTableRecord tstr = new TextStyleTableRecord();
tstr.Name = name;
tstr.FileName = smallfont;
tstr.BigFontFileName = bigfont;
tstr.TextSize = height;
tstr.XScale = xscale;
m_TextStyleTable.UpgradeOpen();
id = m_TextStyleTable.Add(tstr);
m_Transaction.AddNewlyCreatedDBObject(tstr, true);
}
return id;
} Public Sub addtext()
Dim mytext As New DBText
mytext.TextString = "测试"
mytext.Position = New Point3d(0, 0, 0)
Dim Db As Database = HostApplicationServices.WorkingDatabase
Using Trans As Transaction = Db.TransactionManager.StartTransaction
Dim TSB As TextStyleTable = Trans.GetObject(Db.TextStyleTableId, OpenMode.ForRead)
mytext.TextStyle = TSB.Item("工程图")
Dim spc As BlockTableRecord = Trans.GetObject(Db.CurrentSpaceId, OpenMode.ForWrite)
spc.AppendEntity(mytext)
Trans.AddNewlyCreatedDBObject(mytext, True)
Trans.Commit()
End Using
End Sub
<p>TextStyleTable 有默认索引器</p><p>VBNet直接写</p><p>mytext.<font color="#0000ff">TextStyle</font> <font color="#0000ff">=</font> TSB<font color="#ff0000">(</font><font color="#880000">"工程图"</font><font color="#ff0000">)</font></p><p><font color="#ff0000">另外,Cad2008以下符号表索引器和Has函数有Bug,2009没试过</font><br/>ObjectId id = GetRecordId(m_TextStyleTable, name);<br/>参见我的DBTransaction 类的相关函数</p> 那是item的省略模式,在VB时代就有了,不过我还是喜欢用item比较清楚明了 本帖最后由 作者 于 2009-6-25 9:19:16 编辑 <br /><br /> <p>感谢,感动啊。。。</p><p>谢谢版主。(问遍超级群,没有高手回答。还是这里好)</p><p>要是能把 <strong><font face="Verdana" color="#61b713">sailorcwx 翻译成C#就更好了。</font></strong></p><p><strong><font face="Verdana" color="#61b713">虽然是很简单的事情,但是对于初学者来说,那层窗户纸就是不知道怎么捅。。</font></strong></p><p><strong><font face="Verdana" color="#61b713"></font></strong></p><p></p> 本帖最后由 作者 于 2009-6-25 10:27:33 编辑
using System;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace CsMgd25
{
public class Class1
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
public void Test()
{
ObjectId styleid = AddTextStyle("工程图", "txt.shx", "gbcbig.shx", 0, 0.7);
ObjectId id = AddText(new Point3d(0, 0, 0), "明经通道", styleid, 2.5, 0);
}
// 将图形对象加入到模型空间的函数.
public static ObjectId AppendEntity(Entity ent)
{
Database db = HostApplicationServices.WorkingDatabase;
ObjectId entId;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt, OpenMode.ForWrite);
entId = btr.AppendEntity(ent);
trans.AddNewlyCreatedDBObject(ent, true);
trans.Commit();
}
return entId;
}
// 由插入点、文字内容、文字高度和倾斜角度创建单行文字的函数.
public static ObjectId AddText(Point3d position, string textString, ObjectId style, double height, double rotation)
{
try
{
DBText ent = new DBText();
ent.Position = position;
ent.TextString = textString;
ent.Height = height;
ent.Rotation = rotation;
ObjectId entId = AppendEntity(ent);
return entId;
}
catch
{
ObjectId nullId = ObjectId.Null;
return nullId;
}
}
//取得符号表的Id
public ObjectId GetIdFromSymbolTable(SymbolTable st, string key)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
if (st.Has(key))
{
ObjectId idres = st;
if (!idres.IsErased)
return idres;
foreach (ObjectId id in st)
{
if (!id.IsErased)
{
SymbolTableRecord str = (SymbolTableRecord)trans.GetObject(id, OpenMode.ForRead);
if (str.Name == key)
return id;
}
}
}
}
return ObjectId.Null;
}
//建立文字样式
public ObjectId AddTextStyle(string name, string smallfont, string bigfont, double height, double xscale)
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
TextStyleTable TST = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
ObjectId id = GetIdFromSymbolTable(TST, name);
if (id == ObjectId.Null)
{
TextStyleTableRecord TSTR = new TextStyleTableRecord();
TSTR.Name = name;
TSTR.FileName = smallfont;
TSTR.BigFontFileName = bigfont;
TSTR.TextSize = height;
TSTR.XScale = xscale;
TST.UpgradeOpen();
id = TST.Add(TSTR);
trans.AddNewlyCreatedDBObject(TSTR, true);
}
return id;
}
}
}
}
using (Transaction trans = db.TransactionManager.StartTransaction()){}
每个类里面我都要写一次,怎么解决?
<p>第一次来的时候就看了“DBTransaction 类”的贴子,当时看不懂。</p><p>现在再回来看,太棒了。</p><p><a href="http://www.mjtd.com/BBS/dispbbs.asp?BoardID=33&replyID=10813&id=76123&skin=0">http://www.mjtd.com/BBS/dispbbs.asp?BoardID=33&replyID=10813&id=76123&skin=0</a></p>
页:
[1]
2