添加块属性程序
本帖最后由 kvstone 于 2012-12-11 22:01 编辑首先,我有个疑问,为Blockreference 添加新的属性,是否需要先给 BlockTableRecored 添加属性,在对每个BlockReference添加属性
然后,下面的程序是为了给BlockTableRecord添加新属性,程序可以运行,没有任何错误,但是属性并没有添加成功,请大家帮忙看看,挑挑毛病在哪
Public Sub AddAttribute(ByVal attTag As String, ByVal attText As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim attRef As AttributeReference
Dim blkTr As BlockTableRecord
Using docLock As DocumentLock = doc.LockDocument
Using trs As Transaction = doc.Database.TransactionManager.StartTransaction()
bkRef = trs.GetObject(MyClass.BlockId, OpenMode.ForRead)
If bkRef.IsDynamicBlock Then
_blkTbRecId = bkRef.DynamicBlockTableRecord
Else
_blkTbRecId = bkRef.BlockTableRecord
End If
blkTr = TryCast(trs.GetObject(_blkTbRecId, OpenMode.ForWrite), BlockTableRecord)
Dim attDef As AttributeDefinition = New AttributeDefinition(blkTr.Origin, attText, attTag, "", doc.Database.Textstyle)
blkTr.AppendEntity(attDef)
trs.AddNewlyCreatedDBObject(attDef, True)
trs.Commit()
End Using
End Using
End Sub
1,为Blockreference 添加新的属性,是否需要先给 BlockTableRecored 添加属性
答:需要
2,另外在生成块参考Blockreference 时还需要把BlockTableRecored 的属性重新往Blockreference添加一次,否则不会显示 Transaction ctrans, BlockReference bref, BlockTableRecord btr
foreach (ObjectId idAtt in btr)
{
Entity ent = (Entity)ctrans.GetObject(idAtt, OpenMode.ForRead);
if (ent is AttributeDefinition)
{
AttributeDefinition attDef = (AttributeDefinition)ent;
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
attRef.TextString = attDef.TextString; //或设置别的值
bref.AttributeCollection.AppendAttribute(attRef);
ctrans.AddNewlyCreatedDBObject(attRef, true);
}
}
页:
[1]