.NET

Reply
Active Member
trevor_templeton
Posts: 8
Registered: ‎02-26-2007
Message 1 of 4 (302 Views)

Block with Fields in attributes

302 Views, 3 Replies
11-04-2008 10:36 AM
I have code that inserts a block into a drawing then adds the attributes. This has worked great until now. I now have a block that uses the field code blockplaceholder in its attributes. using .net I can not get the field to read its value after it is inserted.
Active Member
trevor_templeton
Posts: 8
Registered: ‎02-26-2007
Message 2 of 4 (302 Views)

Re: Block with Fields in attributes

11-12-2008 12:35 PM in reply to: trevor_templeton
I found the solution you need to add the field to the attribute after it is inserted and instead of using the blockplaceholder you need to reference the object ID
Contributor
shezak
Posts: 17
Registered: ‎01-20-2009
Message 3 of 4 (302 Views)

Re: Block with Fields in attributes

01-27-2009 01:08 AM in reply to: trevor_templeton
It's very kind of you to post the solution so that it help others.

By the way,
Could you please help me to solve my problem?
I do have a block table record in which i am appending a raster image. That is working fine but i want to create a block with specified size which i can move according to my choice and want to insert the raster image into that. Please see my piece of code below:

Dim btr As AcDb.BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), AcDb.BlockTableRecord)

btr.AppendEntity(raster)
tr.AddNewlyCreatedDBObject(raster, True)



Your help would be highly appreciated...

Thanks...
*Roland Feletic
Message 4 of 4 (302 Views)

Re: Block with Fields in attributes

01-28-2009 01:05 AM in reply to: trevor_templeton

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]


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
It's
very kind of you to post the solution so that it help others. By the way,
Could you please help me to solve my problem? I do have a block table record
in which i am appending a raster image. That is working fine but i want to
create a block with specified size which i can move according to my choice and
want to insert the raster image into that. Please see my piece of code below:
Dim btr As AcDb.BlockTableRecord = TryCast(tr.GetObject(db.CurrentSpaceId,
OpenMode.ForWrite), AcDb.BlockTableRecord) btr.AppendEntity(raster)
tr.AddNewlyCreatedDBObject(raster, True) Your help would be highly
appreciated... Thanks...
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.