- 积分
- 306
- 明经币
- 个
- 注册时间
- 2012-5-29
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 James.W.H.Li 于 2012-6-23 16:12 编辑
首先祝大家端午节快乐!
我在用C#进行ACAD2010二次开发的时候,遇到一个奇怪的问题,因为是新手,所以折腾了很长时间,也没有搞明白,最后用C++写了一个类似功能的测试程序,倒是没有问题。
莫非是C#运行效率的问题?
不敢妄加猜测,所以发上来请各位大虾诊断一下。
下面请先看代码:首先生成一个带属性的块定义,然后插入块参照。
[CommandMethod("TD")]
public void DotestDrawLabel()
{
//数据库对象
Database db = HostApplicationServices.WorkingDatabase;
/*
* 创建件号块定义
*/
ObjectId labelblkid = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord btr_Label = null;
if (bt.Has("CNPTDwgLabelBlock_test"))
{
btr_Label = trans.GetObject(bt["CNPTDwgLabelBlock_test"], OpenMode.ForWrite) as BlockTableRecord;
btr_Label.Erase();
}
btr_Label = new BlockTableRecord();
btr_Label.Name = "CNPTDwgLabelBlock_test";
//引线1
Line line1 = new Line(new Point3d(0, 0, 0), new Point3d(100, 100, 0));
//line1.Layer = "02";//所在图层
btr_Label.AppendEntity(line1);
//标线
Line line3 = new Line(new Point3d(100, 100, 0), new Point3d(115, 100, 0));
//line3.Layer = "01";//所在图层
btr_Label.AppendEntity(line3);
//序号 属性
AttributeDefinition label_xh = new AttributeDefinition();
//label_xh.Layer = "02";
label_xh.Constant = false;
label_xh.Tag = "Label序号";
label_xh.Prompt = "LabelXUHAO";
label_xh.TextString = "6";
label_xh.Position = new Point3d(105, 103, 0);
label_xh.Height = Double.Parse("50") ;
btr_Label.AppendEntity(label_xh);
labelblkid = bt.Add(btr_Label);
trans.AddNewlyCreatedDBObject(btr_Label, true);
trans.Commit();
}//end using
/*
* 创建件号块参照
*/
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord mbtr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord btr = trans.GetObject(labelblkid, OpenMode.ForRead) as BlockTableRecord;
// 通过块定义添加块参照
BlockReference br = new BlockReference(new Point3d(0, 0, 0), labelblkid);
//把块参照添加到块表记录
Object blkrefid = mbtr.AppendEntity(br);
AttributeDefinition ad = null;
AttributeReference ar = null;
foreach (ObjectId id in btr)
{
if (id.ObjectClass.Equals(RXClass.GetClass(typeof(AttributeDefinition))))
{
ad = trans.GetObject(id, OpenMode.ForRead) as AttributeDefinition;
ar = new AttributeReference();
ar.SetAttributeFromBlock(ad, br.BlockTransform);
//设置快参照中个属性的值
ar.Height = ad.Height;
ar.TextString = ad.TextString;
ar.WidthFactor = ad.WidthFactor;
ar.Position = ad.Position;
br.AttributeCollection.AppendAttribute(ar);
}
}
// 通过事务添加块参照到数据库
trans.AddNewlyCreatedDBObject(br, true);
trans.Commit();
}//end using
}
运行效果:块中的线条在执行命令后,立即显示,而属性确没有立即显示,而是等一会儿!
测试了很多次,会发现等的时间长短不一定,再就是还有一种情况,执行完命令后鼠标不动不显示,鼠标动一下立即显示?
……晕啊……
请各位大虾指点迷津,谢谢啦!!!
|
|