- 积分
- 7540
- 明经币
- 个
- 注册时间
- 2006-4-6
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2009-6-25 09:00:00
|
显示全部楼层
本帖最后由 作者 于 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;
- [CommandMethod("Test")]
- 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[BlockTableRecord.ModelSpace], 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[key];
- 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()){}
每个类里面我都要写一次,怎么解决?
|
|