.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trouble getting textstring from attribute definition

62 REPLIES 62
Reply
Message 1 of 63
myspambox111
1089 Views, 62 Replies

Trouble getting textstring from attribute definition

I have been creating block attributes with some good success. Example:







While btrEnum.MoveNext

Dim ent As Entity = btrEnum.Current.GetObject(OpenMode.ForWrite)

If TypeOf ent Is AttributeDefinition Then

Dim attdef As AttributeDefinition = ent

Dim attref As New AttributeReference

attref.SetAttributeFromBlock(attdef, myBRef.BlockTransform)

attref.TextString = myReader.ReadLine

attColl.AppendAttribute(attref)

trans.AddNewlyCreatedDBObject(attref, True)

End If



End While

Now, when I try to read those attribute's textstrings back later for different reasons, I receive only the "default" attribute textstring instead of the one I inserted earlier in the program. Example:



While btrEnum.MoveNext

Dim ent As Entity = btrEnum.Current.GetObject(OpenMode.ForWrite)

If TypeOf ent Is AttributeDefinition Then

Dim attdef As AttributeDefinition = ent

Dim attref As New AttributeReference

attref.SetAttributeFromBlock(attdef, myBRef.BlockTransform)

If attdef.Tag.ToUpper = myTag.ToUpper Then

value = attdef.TextString

End If

End If

End While

Why doesn't this return the textstring value recorded earlier?



Is there an easier way to do this?
62 REPLIES 62
Message 61 of 63
HEnnulat
in reply to: myspambox111

Did you ever get your answer (either here or elsewhere)? If so could you you post the solution that you implemented successfully?

When I search for posts I find myself looking to the original poster person to say that he got an answer that helped him or not.

Let us/me know. I'm trying to figure out how to do this too. And I just ordered the controversial book too.....

My impression is that the answer is in this thread, however I'm not seeing that you agree... Will likely come back to this thread for the sample code.
Message 62 of 63
HEnnulat
in reply to: myspambox111

Here is the For Each Next Solution that Jerry Winters actually worked out for me after I mentioned this discussion to him.
It works, but only for the top level blocks. It does not find any nested block attributes. I'm thinking that the enumeration method will find all the nested blocks as well. The same code is in the attached file also.

Some of the discussion on this subject indicates this is not a difficult problem. I'm not finding it so smple.... but then I'm pretty new as well... However since the question was raised, I wanted to close the loop on this sub discussion.
{code}
Public Function AddBlockAndAtts(ByVal InsPt As Geometry.Point3d, _
ByVal BlockName As String) As DatabaseServices.ObjectId
'Add Block Reference with all the attributes from the block definition.
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As Document
Dim myBT As BlockTable
Dim myBTR As BlockTableRecord

myDwg = Application.DocumentManager.MdiActiveDocument
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction

'Open the database for Write
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
myBTR = myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)

'Insert the Block
Dim myBlockDef As BlockTableRecord = myBT(BlockName).GetObject(OpenMode.ForRead)
Dim myBlockRef As New BlockReference(InsPt, myBT(BlockName))
myBTR.AppendEntity(myBlockRef)
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)

'Set the Attribute Value
Dim myAttColl As AttributeCollection
Dim myEnt As Entity
' Dim myBTREnum As BlockTableRecordEnumerator
myAttColl = myBlockRef.AttributeCollection
Dim myEntID As ObjectId
' myBTREnum = myBlockDef.GetEnumerator
For Each myEntID In myBlockDef
' While myBTREnum.MoveNext
'myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
myEnt = CType(myEntID.GetObject(OpenMode.ForWrite), Entity)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition = myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef, myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
' End While
Next

'Commit the Transaction
myTrans.Commit()
'Dispose of the Transaction Objects
myTrans.Dispose()
myTransMan.Dispose()
Return myBlockRef.ObjectId
End Function
{code}
Message 63 of 63
Anonymous
in reply to: myspambox111

Not sure what you mean by 'nested blocks/attributes'.

A BlockReference has attributes, which correspond to
attribute definitions in the block's defining entities.

If there are BlockReferences that are nested in the
definition of a block, which also have attributes, those
attributes are part of another BlockReference that is
inserted into/resides in the 'parent' block's definition.

I think you're going to need to explain more clearly
what you are trying to do. If you have blocks nested
within other blocks, you deal with them in the same
way you deal with 'top-level' block references, except
that the owner is not the modelspace or a paperspace
block, it is the block that contains the nested block
references.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6279865@discussion.autodesk.com...
Here is the For Each Next Solution that Jerry Winters actually worked out
for me after I mentioned this discussion to him.
It works, but only for the top level blocks. It does not find any nested
block attributes. I'm thinking that the enumeration method will find all
the nested blocks as well. The same code is in the attached file also.

Some of the discussion on this subject indicates this is not a difficult
problem. I'm not finding it so smple.... but then I'm pretty new as well...
However since the question was raised, I wanted to close the loop on this
sub discussion.
{code}
Public Function AddBlockAndAtts(ByVal InsPt As Geometry.Point3d, _
ByVal BlockName As String) As DatabaseServices.ObjectId
'Add Block Reference with all the attributes from the block
definition.
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myDwg As Document
Dim myBT As BlockTable
Dim myBTR As BlockTableRecord

myDwg = Application.DocumentManager.MdiActiveDocument
myTransMan = myDwg.TransactionManager
myTrans = myTransMan.StartTransaction

'Open the database for Write
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
myBTR =
myBT(BlockTableRecord.ModelSpace).GetObject(OpenMode.ForWrite)

'Insert the Block
Dim myBlockDef As BlockTableRecord =
myBT(BlockName).GetObject(OpenMode.ForRead)
Dim myBlockRef As New BlockReference(InsPt, myBT(BlockName))
myBTR.AppendEntity(myBlockRef)
myTrans.AddNewlyCreatedDBObject(myBlockRef, True)

'Set the Attribute Value
Dim myAttColl As AttributeCollection
Dim myEnt As Entity
' Dim myBTREnum As BlockTableRecordEnumerator
myAttColl = myBlockRef.AttributeCollection
Dim myEntID As ObjectId
' myBTREnum = myBlockDef.GetEnumerator
For Each myEntID In myBlockDef
' While myBTREnum.MoveNext
'myEnt = myBTREnum.Current.GetObject(OpenMode.ForWrite)
myEnt = CType(myEntID.GetObject(OpenMode.ForWrite), Entity)
If TypeOf myEnt Is DatabaseServices.AttributeDefinition Then
Dim myAttDef As DatabaseServices.AttributeDefinition =
myEnt
Dim myAttRef As New DatabaseServices.AttributeReference
myAttRef.SetAttributeFromBlock(myAttDef,
myBlockRef.BlockTransform)
myAttColl.AppendAttribute(myAttRef)
myTrans.AddNewlyCreatedDBObject(myAttRef, True)
End If
' End While
Next

'Commit the Transaction
myTrans.Commit()
'Dispose of the Transaction Objects
myTrans.Dispose()
myTransMan.Dispose()
Return myBlockRef.ObjectId
End Function
{code}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost