明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 6903|回复: 14

[符号表] [求助]创建指定样式名的单行文字

  [复制链接]
发表于 2009-6-24 17:40 | 显示全部楼层 |阅读模式
  1.         // 由插入点、文字内容、文字高度和倾斜角度创建单行文字的函数.
  2.         public static ObjectId AddText(Point3d position, string textString, ObjectId style, double height, double oblique)
  3.         {
  4.             try
  5.             {
  6.                 DBText ent = new DBText();
  7.                 ent.Position = position;
  8.                 ent.TextString = textString;
  9.                 ent.Height = height;
  10.                 ent.Oblique = oblique;
  11.                 ObjectId entId = AppendEntity(ent);
  12.                 return entId;
  13.             }
  14.             catch
  15.             {
  16.                 ObjectId nullId = ObjectId.Null;
  17.                 return nullId;
  18.             }
  19.         }
复制代码
  1. public void CreateStyle()
  2.         {
  3.             Database db = HostApplicationServices.WorkingDatabase;
  4.             using (Transaction trans = db.TransactionManager.StartTransaction())
  5.             {
  6.                 TextStyleTable st = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
  7.                 String StyleName = "工程图";
  8.                 if (st.Has(StyleName) == false)
  9.                 {
  10.                     TextStyleTableRecord str = new TextStyleTableRecord();
  11.                     str.Name = StyleName;
  12.                     str.FileName = "simfang.ttf";
  13.                     //---------------------------------------------
  14.                     // 设置SHX字体
  15.                     // str.FileName = "gbenor"
  16.                     //设置大字体.
  17.                     // str.BigFontFileName = "gbcbig"
  18.                     // --------------------------------------------
  19.                     str.ObliquingAngle = 15 * Math.PI / 180;
  20.                     str.XScale = 0.67;
  21.                     ObjectId TextstyleId = st.Add(str);
  22.                     trans.AddNewlyCreatedDBObject(str, true);
  23.                     db.Textstyle = TextstyleId;
  24.                     trans.Commit();
  25.                 }
  26.             }
  27.         }

                String StyleName = "工程图";
                string textStr = "%%u" + "单行文字ABC123" + "%%u";
                // 我现在不知道怎么将类型为string的StyleName转成我需要的style(其类型为ObjectId)。。。。。
                AddText(new Point3d(0, 0, 0), textStr, style, 0, 0);


 楼主| 发表于 2009-6-24 17:47 | 显示全部楼层

上面的两个函数原文取自:

《AutoCAD VBA&VB.NET开发基础与实例教程》

作者:曾洪飞 张帆 卢择临编著 书自带光盘

如果没有看错,第一个函数可能少了一句。ent.TextStyle = style;

oblique属性应该是文字倾斜度(一般用不上这个属性)

应该改为角度Rotation比较实用吧

 楼主| 发表于 2009-6-24 17:49 | 显示全部楼层
给个Lisp的建立单行文字的。
  1. ;;;(JW-MK_TEXT PT STRING STYLE HEIGHT ANG)
  2. (defun JW-MK_TEXT (PT STRING STYLE HEIGHT ANG)
  3.   (if (entmake (list (cons 0 "TEXT")
  4.        (cons 100 "AcDbEntity")
  5.        (cons 100 "AcDbText")
  6.        (cons 1 STRING)
  7.        (cons 7 STYLE)
  8.        (cons 10 PT)
  9.        (cons 40 HEIGHT)
  10.        (cons 50 ANG)
  11.         )
  12.       )
  13.     (entlast)
  14.   )
  15. )
发表于 2009-6-24 19:23 | 显示全部楼层
文字式样记录存在于文字式样表里
首先打开文字式样表,然后
txt.TextStyle = tst[tsname]
另,创建文字式样记录的函数最好返回Id:
下面是我的自定义类里的函数
  1.         public ObjectId AddTextStyle(string name, string smallfont, string bigfont, double height, double xscale)
  2.         {
  3.             ObjectId id = GetRecordId(m_TextStyleTable, name);
  4.             if (id == ObjectId.Null)
  5.             {
  6.                 TextStyleTableRecord tstr = new TextStyleTableRecord();
  7.                 tstr.Name = name;
  8.                 tstr.FileName = smallfont;
  9.                 tstr.BigFontFileName = bigfont;
  10.                 tstr.TextSize = height;
  11.                 tstr.XScale = xscale;
  12.                 m_TextStyleTable.UpgradeOpen();
  13.                 id = m_TextStyleTable.Add(tstr);
  14.                 m_Transaction.AddNewlyCreatedDBObject(tstr, true);
  15.             }
  16.             return id;
  17.         }
复制代码
发表于 2009-6-24 20:00 | 显示全部楼层
  1. Public Sub addtext()
  2.         Dim mytext As New DBText
  3.         mytext.TextString = "测试"
  4.         mytext.Position = New Point3d(0, 0, 0)
  5.         Dim Db As Database = HostApplicationServices.WorkingDatabase
  6.         Using Trans As Transaction = Db.TransactionManager.StartTransaction
  7.             Dim TSB As TextStyleTable = Trans.GetObject(Db.TextStyleTableId, OpenMode.ForRead)
  8.             mytext.TextStyle = TSB.Item("工程图")
  9.             Dim spc As BlockTableRecord = Trans.GetObject(Db.CurrentSpaceId, OpenMode.ForWrite)
  10.             spc.AppendEntity(mytext)
  11.             Trans.AddNewlyCreatedDBObject(mytext, True)
  12.             Trans.Commit()
  13.         End Using
  14.     End Sub
发表于 2009-6-24 20:51 | 显示全部楼层

TextStyleTable 有默认索引器

VBNet直接写

mytext.TextStyle  = TSB("工程图")

另外,Cad2008以下符号表索引器和Has函数有Bug,2009没试过
ObjectId id = GetRecordId(m_TextStyleTable, name);
参见我的DBTransaction 类的相关函数

发表于 2009-6-24 22:10 | 显示全部楼层
那是item的省略模式,在VB时代就有了,不过我还是喜欢用item比较清楚明了
 楼主| 发表于 2009-6-25 08:50 | 显示全部楼层
本帖最后由 作者 于 2009-6-25 9:19:16 编辑

感谢,感动啊。。。

谢谢版主。(问遍超级群,没有高手回答。还是这里好)

要是能把 sailorcwx 翻译成C#就更好了。

虽然是很简单的事情,但是对于初学者来说,那层窗户纸就是不知道怎么捅。。

 楼主| 发表于 2009-6-25 09:00 | 显示全部楼层
本帖最后由 作者 于 2009-6-25 10:27:33 编辑
  1. using System;
  2. using System.Text;
  3. using Autodesk.AutoCAD.ApplicationServices;
  4. using Autodesk.AutoCAD.DatabaseServices;
  5. using Autodesk.AutoCAD.EditorInput;
  6. using Autodesk.AutoCAD.Geometry;
  7. using Autodesk.AutoCAD.Runtime;
  8. namespace CsMgd25
  9. {
  10.     public class Class1
  11.     {
  12.         Database db = HostApplicationServices.WorkingDatabase;
  13.         Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  14.         [CommandMethod("Test")]
  15.         public void Test()
  16.         {
  17.             ObjectId styleid = AddTextStyle("工程图", "txt.shx", "gbcbig.shx", 0, 0.7);
  18.             ObjectId id = AddText(new Point3d(0, 0, 0), "明经通道", styleid, 2.5, 0);
  19.         }
  20.         // 将图形对象加入到模型空间的函数.
  21.         public static ObjectId AppendEntity(Entity ent)
  22.         {
  23.             Database db = HostApplicationServices.WorkingDatabase;
  24.             ObjectId entId;
  25.             using (Transaction trans = db.TransactionManager.StartTransaction())
  26.             {
  27.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
  28.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  29.                 entId = btr.AppendEntity(ent);
  30.                 trans.AddNewlyCreatedDBObject(ent, true);
  31.                 trans.Commit();
  32.             }
  33.             return entId;
  34.         }
  35.         // 由插入点、文字内容、文字高度和倾斜角度创建单行文字的函数.
  36.         public static ObjectId AddText(Point3d position, string textString, ObjectId style, double height, double rotation)
  37.         {
  38.             try
  39.             {
  40.                 DBText ent = new DBText();
  41.                 ent.Position = position;
  42.                 ent.TextString = textString;
  43.                 ent.Height = height;
  44.                 ent.Rotation = rotation;
  45.                 ObjectId entId = AppendEntity(ent);
  46.                 return entId;
  47.             }
  48.             catch
  49.             {
  50.                 ObjectId nullId = ObjectId.Null;
  51.                 return nullId;
  52.             }
  53.         }
  54.         //取得符号表的Id
  55.         public ObjectId GetIdFromSymbolTable(SymbolTable st, string key)
  56.         {
  57.             using (Transaction trans = db.TransactionManager.StartTransaction())
  58.             {
  59.                 if (st.Has(key))
  60.                 {
  61.                     ObjectId idres = st[key];
  62.                     if (!idres.IsErased)
  63.                         return idres;
  64.                     foreach (ObjectId id in st)
  65.                     {
  66.                         if (!id.IsErased)
  67.                         {
  68.                             SymbolTableRecord str = (SymbolTableRecord)trans.GetObject(id, OpenMode.ForRead);
  69.                             if (str.Name == key)
  70.                                 return id;
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.             return ObjectId.Null;
  76.         }
  77.         //建立文字样式
  78.         public ObjectId AddTextStyle(string name, string smallfont, string bigfont, double height, double xscale)
  79.         {
  80.             using (Transaction trans = db.TransactionManager.StartTransaction())
  81.             {
  82.                 TextStyleTable TST = (TextStyleTable)trans.GetObject(db.TextStyleTableId, OpenMode.ForWrite);
  83.                 ObjectId id = GetIdFromSymbolTable(TST, name);
  84.                 if (id == ObjectId.Null)
  85.                 {
  86.                     TextStyleTableRecord TSTR = new TextStyleTableRecord();
  87.                     TSTR.Name = name;
  88.                     TSTR.FileName = smallfont;
  89.                     TSTR.BigFontFileName = bigfont;
  90.                     TSTR.TextSize = height;
  91.                     TSTR.XScale = xscale;
  92.                     TST.UpgradeOpen();
  93.                     id = TST.Add(TSTR);
  94.                     trans.AddNewlyCreatedDBObject(TSTR, true);
  95.                 }
  96.                 return id;
  97.             }
  98.         }
  99.     }
  100. }
using (Transaction trans = db.TransactionManager.StartTransaction()){}
每个类里面我都要写一次,怎么解决?
 楼主| 发表于 2009-6-25 09:24 | 显示全部楼层

第一次来的时候就看了“DBTransaction 类”的贴子,当时看不懂。

现在再回来看,太棒了。

http://www.mjtd.com/BBS/dispbbs.asp?BoardID=33&replyID=10813&id=76123&skin=0

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-20 02:16 , Processed in 0.380701 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表