It is long ago that I tried to do something with
fields but had the biggest problems. If there is just one field without nested
fields it was no problem to got it to work.
Maybe there is a much better solution, but here is
mine. Don't forget that it is just some code you have to clean up because it is
just my testcode.
I also don't remember if you need to set a reference to
acmgdinternal.
[code]
<code>
{code}
public static
void InsertBlockAttibuteRef(BlockReference blkRef, Transaction
tr)
{
BlockTableRecord btAttRec =
(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord,
OpenMode.ForRead);
size=2>
foreach (ObjectId idAtt in
btAttRec)
{
Entity ent = (Entity)tr.GetObject(idAtt,
OpenMode.ForRead);
if (ent is
AttributeDefinition)
{
AttributeDefinition attDef =
(AttributeDefinition)ent;
AttributeReference attRef = new
AttributeReference();
attRef.SetAttributeFromBlock(attDef,
blkRef.BlockTransform);
ObjectId idTemp =
blkRef.AttributeCollection.AppendAttribute(attRef);
size=2>
string
NewAttributeString;
bool HasField = AttributeHasBlockplaceholder(tr, attRef, blkRef.ObjectId, out
NewAttributeString);
if
(HasField)
{
attRef.TextString =
NewAttributeString;
}
size=2>
tr.AddNewlyCreatedDBObject(attRef,
true);
}
}
}
/// <param
name="tr">Transaction</param>
/// <param
name="AttRef">AttributeReference</param>
/// <param name="BlockRefId">Id of the
BlockReference</param>
///
<param name="AttTextString">The new TextString if it does have a
blockplaceholder</param>
///
<returns>"True" if Blockdeffinition has a Blockplaceholder, "False" if
not</returns>
public static
bool AttributeHasBlockplaceholder(Transaction tr, AttributeReference AttRef,
ObjectId BlockRefId, out string
AttTextString)
{
Document
doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed =
doc.Editor;
bool
HasBlockplaceholder =
false;
AttTextString =
"";
if
(AttRef.HasFields)
{
// Open the extension
dictionary
DBDictionary extDict = (DBDictionary)tr.GetObject(AttRef.ExtensionDictionary,
OpenMode.ForRead);
const string fldDictName =
"ACAD_FIELD";
const string fldEntryName =
"TEXT";
// Get the field
dictionary
if
(extDict.Contains(fldDictName))
{
ObjectId fldDictId =
extDict.GetAt(fldDictName);
if (fldDictId !=
ObjectId.Null)
{
DBDictionary fldDict = (DBDictionary)tr.GetObject(fldDictId,
OpenMode.ForRead);
// Get the field
itself
if
(fldDict.Contains(fldEntryName))
{
ObjectId fldId =
fldDict.GetAt(fldEntryName);
if (fldId !=
ObjectId.Null)
{
DBObject obj = tr.GetObject(fldId,
OpenMode.ForRead);
Field fld = obj as
Field;
if (fld !=
null)
{
string strBlockRefId =
BlockRefId.ToString();
strBlockRefId = strBlockRefId.Substring(1, strBlockRefId.Length - 2);
string fldCode =
fld.GetFieldCode();
if
(fldCode.Contains("?BlockRefId"))
{
HasBlockplaceholder =
true;
AttTextString = FieldTextBlockplaceholder(fld,
strBlockRefId);
}
}
}
}
}
}
}
return
HasBlockplaceholder;
}
/// <param
name="objField">AttributeReference</param>
/// <param name="BlockRefIdFieldText">Id of the BlockReference as string
like "%<
\\_ObjId " + BlockRefId +
">%"</param>
///
<param name="AttTextString">The TextString for the
Attribute</param>
///
<returns>"True" if Field has a Blockplaceholder, "False" if
not</returns>
private static
string FieldTextBlockplaceholder(Field objField, string
BlockRefIdFieldText)
{
Document
doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
FieldCodeWithChildren fldWChidren = objField.GetFieldCodeWithChildren();//
(FieldCodeFlags.TextField);
Field[] Kinder =
fldWChidren.Children;
string AttTextString = fldWChidren.FieldCode;
int
CountChild =
0;
foreach
(Field var in
Kinder)
{
string FieldChildren =
var.GetFieldCode();
string newFieldChildText =
"";
++CountChild;
if
(FieldChildren.Contains("?BlockRefId"))
{
string Search =
"?BlockRefId";
newFieldChildText = FieldChildren.Replace(Search, BlockRefIdFieldText);
}
else
{
newFieldChildText =
FieldChildren;
}
string StringIDChildren =
var.Id.ToString();
StringIDChildren = StringIDChildren.Substring(1, StringIDChildren.Length -
2);
string SearchAtt = "
\\_FldId " +
StringIDChildren;
AttTextString = AttTextString.Replace(SearchAtt, newFieldChildText);
}
return
AttTextString;
}
{code}
</code>
[/code]