- 积分
- 31765
- 明经币
- 个
- 注册时间
- 2005-5-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
各位专家,以下程序先生成由一个圆和标记为"Height”、"DotMake"的两个属性文字组成的块,然后再选取点插入这个属性块,但是只有圆显示出来,属性文字没有显示出来,请问这是为什么?- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.Colors;
- namespace Ctest
- {
- public class Class1
- {
- [CommandMethod("test")]
- public void test()
- {
- ObjectId TkId = CreateB2();
- Document acDoc = Application.DocumentManager.MdiActiveDocument;
- PromptPointResult pPtRes;
- PromptPointOptions pPtOpts = new PromptPointOptions("\n请指定块的插入点<按ESC键结束>: ");
- pPtOpts.UseBasePoint = false;
- bool Pdbz = true;
- int i = 1;
- while (Pdbz == true)
- {
- pPtRes = acDoc.Editor.GetPoint(pPtOpts);
- Point3d ptCrd = pPtRes.Value;
- if (pPtRes.Status == PromptStatus.OK)
- {
- InsertB2(TkId, i.ToString(), (1000 * i).ToString(), ptCrd);
- i = i + 1;
- }
- else
- {
- Pdbz = false;
- }
- }
- }
- //生成B2"图块
- public ObjectId CreateB2()
- {
- ObjectId newBtrId = new ObjectId();
- Database db = HostApplicationServices.WorkingDatabase;
- Transaction trans = db.TransactionManager.StartTransaction();
- Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
- try
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
- if ((bt.Has("B2")))
- {
- newBtrId = bt["B2"];
- }
- else
- {
- Point3d center = new Point3d(0.0, 0.0, 0.0);
- Circle circle = new Circle(center, Vector3d.ZAxis, 0.5);
- circle.ColorIndex = 1;
- AttributeDefinition text1 = new AttributeDefinition(new Point3d(0.5, 0.0, 0.0), "", "Height", "", db.Textstyle);
- text1.ColorIndex = 1;
- text1.Height = 1.0;
- text1.HorizontalMode = TextHorizontalMode.TextLeft;
- text1.VerticalMode = TextVerticalMode.TextBottom;
- AttributeDefinition text2 = new AttributeDefinition(new Point3d(0.5, 0.0, 0.0), "", "DotMake", "", db.Textstyle);
- text2.ColorIndex = 3;
- text2.Height = 1.0;
- text2.HorizontalMode = TextHorizontalMode.TextLeft;
- text2.VerticalMode = TextVerticalMode.TextTop;
- BlockTableRecord newBtr = new BlockTableRecord();
- newBtr.Name = "B2";
- newBtrId = bt.Add(newBtr);
- trans.AddNewlyCreatedDBObject(newBtr, true);
- newBtr.AppendEntity(circle);
- newBtr.AppendEntity(text1);
- newBtr.AppendEntity(text2);
- trans.AddNewlyCreatedDBObject(circle, true);
- trans.AddNewlyCreatedDBObject(text1, true);
- trans.AddNewlyCreatedDBObject(text2, true);
- }
- trans.Commit();
- }
- catch
- {
- ed.WriteMessage("生成B2图块出错!");
- }
- finally
- {
- trans.Dispose();
- }
- return newBtrId;
- }
- //插入名为"B2"块参照到模型空间
- public ObjectId InsertB2(ObjectId XBId, string Xstr1, string Xstr2, Point3d pos)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- Transaction trans = db.TransactionManager.StartTransaction();
- try
- {
- BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- BlockReference br = new BlockReference(pos, XBId);
- AttributeReference attRef = new AttributeReference();
- BlockTableRecord empBtr = (BlockTableRecord)trans.GetObject(bt["B2"], OpenMode.ForRead);
- foreach (ObjectId id in empBtr)
- {
- Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
- if (ent is AttributeDefinition)
- {
- AttributeDefinition attDef = ((AttributeDefinition)(ent));
- attRef.SetPropertiesFrom(attDef);
- attRef.Position = new Point3d(0.5 + br.Position.X, 0.0 + br.Position.Y, 0.0 + br.Position.Z);
- if ((string)attRef.Tag == "Height")
- {
- attRef.TextString = Xstr1;
- attRef.HorizontalMode = TextHorizontalMode.TextLeft;
- attRef.VerticalMode = TextVerticalMode.TextBottom;
- }
- else
- {
- attRef.TextString = Xstr2;
- attRef.HorizontalMode = TextHorizontalMode.TextLeft;
- attRef.VerticalMode = TextVerticalMode.TextTop;
- }
- }
- }
- btr.AppendEntity(br);
- br.AttributeCollection.AppendAttribute(attRef);
- trans.AddNewlyCreatedDBObject(attRef, true);
- trans.AddNewlyCreatedDBObject(br, true);
- trans.Commit();
- return br.ObjectId;
- }
- finally
- {
- trans.Dispose();
- }
- }
- }
- }
|
|