Author Topic: Insert BlockRef with "calculated attributes"  (Read 3480 times)

0 Members and 1 Guest are viewing this topic.

MarioR

  • Newt
  • Posts: 64
Insert BlockRef with "calculated attributes"
« on: October 22, 2013, 09:32:29 PM »
Hello,

I want a block, and insert the reference as. net.
This block has a calculated attribute (label its z-coordinate,
attrDef.Preset = true attrDef.TextString = "InsertionPoint")

In the insert function
Code: [Select]
if (acEntity is AttributeDefinition)
{
attrDef = (AttributeDefinition)acEntity;
attrRef = new AttributeReference();
attrRef.SetAttributeFromBlock(attrDef, acBlockRef.BlockTransform);
...

But attrRef.IsPreset is always false?

How can i insert the "calculate Attribut"!?

regards Mario
« Last Edit: October 22, 2013, 10:06:15 PM by MarioR »

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Insert BlockRef with "calculated attributes"
« Reply #1 on: October 22, 2013, 11:07:58 PM »
Seems this is unavailable through the API as it is only set properly on blocks inserted through the interface.  You can check the property on the definition however

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Insert BlockRef with "calculated attributes"
« Reply #2 on: October 23, 2013, 01:21:05 PM »
I deleted my earlier post since its obvious now that I read your post that it wasn't helping.

If you're inserting the blockreference then why can't you set the attribute value at the point of insertion?

attrRef.TextString = "InsertionPoint"
« Last Edit: October 23, 2013, 01:27:19 PM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Insert BlockRef with "calculated attributes"
« Reply #3 on: October 23, 2013, 02:24:19 PM »
I am not sure exactly what your asking, but when you say "calculated value" do you mean a field?
 
I just got where I script inserts so not to worry about annotative, alignment parameters, atts, etc........, but I think that was another case I ran into that needed to be checked.
 

MarioR

  • Newt
  • Posts: 64
Re: Insert BlockRef with "calculated attributes"
« Reply #4 on: October 26, 2013, 01:44:07 PM »
Hello,

the attrDef has an Attribut like Jeff described.

in the attrDef.ExtensionDictionary is added an Field with:
Code: [Select]
bDefField.GetFieldCode() ="\\AcObjProp.16.2 Object(?BlockRefId,1).InsertionPoint \\f \"%lu2%pt4%pr1\""
on Manual insert Block has the blockreference.ExtensionDictionary  a field:
Code: [Select]
bRefField.GetFieldCode()="\\AcObjProp.16.2 Object(%<\\_ObjId 8796083898928>%,1).InsertionPoint \\f \"%lu2%pt4%pr1\""
How create by. NET code such fields in the block Refrence?

regards Mario

MarioR

  • Newt
  • Posts: 64
Found Solution
« Reply #5 on: October 27, 2013, 02:29:20 PM »
Hello,

i have found a Solution from http://forums.autodesk.com/t5/NET/Block-with-Fields-in-attributes/m-p/2374740#M12284.

Quote
Code is from Roland Feletic
Code: [Select]
public static
 void InsertBlockAttibuteRef(BlockReference blkRef, Transaction tr)
 {
BlockTableRecord btAttRec =  (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
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);
string NewAttributeString;                   
bool HasField = AttributeHasBlockplaceholder(tr, attRef, blkRef.ObjectId, out NewAttributeString);                   
if (HasField)
{
attRef.TextString = NewAttributeString;
}
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;
}

After insertBlock is the AttributValue="####", but a "REGEN" refresh the calculation and all is fine.

Thanks to all for braininput.

regards Mario