[求助]如何插入带属性的块参照?已解决!
本帖最后由 作者 于 2009-9-28 21:28:36 编辑 <br /><br /> /<p> operBlkRec = (BlockTableRecord)operTrans.GetObject(operBlk, OpenMode.ForWrite);</p><p> InsertBlkRef = new BlockReference(Point3d.Origin, InsertBlkID);<br/> InsertBlkRef.SetDatabaseDefaults();<br/> <br/> operBlkRec.AppendEntity(InsertBlkRef);<br/> operTrans.AddNewlyCreatedDBObject(InsertBlkRef, true);</p><p></p><p>我的块定义是一个圆加一个属性,结果发现圆插入了,但属性没有插入,在cad中打开插入的块参照的特性,发现根本就没有属性那一栏。</p><p>还有,调试过程中发现插入后InsertBlkRef的<a href="mk:@MSITStore:E:\study\CAD\arxref(.net).chm::/2/Autodesk.AutoCAD.DatabaseServices.BlockReference.AttributeCollection.html">AttributeCollection</a>是null。</p><p>不知道应该怎么样插入带属性定义的块参照,并且赋给块参照的属性具体的tag?</p><p><font color="#f76809">//插入快参照后还需要插入属性<br/></font> int i = 0;<br/> BlockTableRecord blkdef = (BlockTableRecord)operTrans.GetObject(InsertBlkRef.BlockTableRecord, OpenMode.ForRead);<br/> foreach (ObjectId id in blkdef)<br/> {<br/> DBObject ent = operTrans.GetObject(id, OpenMode.ForRead);<br/> if (ent is AttributeDefinition)<br/> {<br/> AttributeDefinition attdef = (AttributeDefinition)ent;<br/> AttributeReference attref = new AttributeReference();<br/> attref.SetAttributeFromBlock(attdef, InsertBlkRef.BlockTransform);<br/> attref.TextString = loopInfor;<br/> i++;<br/> InsertBlkRef.AttributeCollection.AppendAttribute(attref);<br/> operTrans.AddNewlyCreatedDBObject(attref, true);<br/> }<br/> }</p><p><br/></p> 我的DBTransaction类有类似的函数,转帖过来吧public Dictionary<AttributeDefinition, AttributeReference> AppendAttribToBlock(BlockReference blkref, List<string> atts)
{
BlockTableRecord blkdef = (BlockTableRecord)m_Transaction.GetObject(blkref.BlockTableRecord, OpenMode.ForRead);
int i = 0;
if (blkdef.HasAttributeDefinitions)
{
Dictionary<AttributeDefinition, AttributeReference> attribs = new Dictionary<AttributeDefinition, AttributeReference>();
foreach (ObjectId id in blkdef)
{
DBObject ent = GetObject(id, OpenMode.ForRead);
if (ent is AttributeDefinition)
{
AttributeDefinition attdef = (AttributeDefinition)ent;
AttributeReference attref = new AttributeReference();
attref.SetAttributeFromBlock(attdef, blkref.BlockTransform);
if (i < atts.Count)
attref.TextString = atts;
else
attref.TextString = attdef.TextString;
i++;
blkref.AttributeCollection.AppendAttribute(attref);
m_Transaction.AddNewlyCreatedDBObject(attref, true);
attribs.Add(attdef, attref);
}
}
return attribs;
}
return null;
}
页:
[1]