.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
1108 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 2 of 63
NathTay
in reply to: myspambox111

I presume you have just made a typo. You are retrieving the TextString from the AttributeDefinition.
Message 3 of 63
NathTay
in reply to: myspambox111

Having looked at your code closer I think you have other problems for what you are trying to achieve. You may want to explain exactly what you are doing and show all of your code.
Message 4 of 63

The textstring property of an AttributeDefinition will always hold the "Default" value.



What you need to do is get the value of the TextString Property in an AttributeReference.



The code you have in the first block appears to be proper code for adding the attribute references to a newly inserted block reference, but the second block is not what the code should look like to read the values of attribute references from a block reference.



this is a snippet of a function I have to retrieve attribute values.


Dim FromBlk As BlockReference = trans.GetObject(BrefID, .....

Dim AttObj As AttributeReference = Nothing



Dim AttCol As AttributeCollection = FromBlk.AttributeCollection



Dim AttEnum As System.Collections.IEnumerator = AttCol.GetEnumerator



Try

While AttEnum.MoveNext



AttObj = trans.GetObject(AttEnum.Current, .....

If AttObj.Tag = AttTag Then



IntoVar = AttObj.TextString



AttObj.Dispose()



Return True



End If



AttObj.Dispose()

End while

Return False



I believe if you try to massage that to suit your needs, you'll figure it out.

Dave O.                                                                  Sig-Logos32.png
Message 5 of 63
Anonymous
in reply to: myspambox111

Can I ask what book(s) you've been using to learn
AutoCAD .NET programming?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6113074@discussion.autodesk.com...
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?
Message 6 of 63
myspambox111
in reply to: myspambox111

I could not get the example provided to work in any variation. The enumerator would not work as if there were no attribute in the block.



Tony: as far as books, I have only the book by Jerry Winters. I know not of any others. All my code is an amalgamation of what I have seen in tutorials on the autodesk site, code from the forums and what I've learned in the book.



I've been forced to find a stupid workaround for this problem as you cannot access the attributes as you can enter them. Seems pretty stupid to me, but oh well.
Message 7 of 63
Anonymous
in reply to: myspambox111

Well, sorry to say it, but the bad programming that is
being spread by that book is a real tragedy..

You can search this newsgroup to see how many times
I've said this, and I'll say it one more time:

Do not use GetEnumerator()/MoveNext()/Current to
loop through collections.

You use For Each/Next (foreach() in C#).

It's becoming pretty clear that this book is responsible
for misguiding so many in this and other areas.

As far as your specific problem, if you can post your
code in a .txt file attachment (sorry, that's the only way
I can read it), we can see what's wrong.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6115809@discussion.autodesk.com...
I could not get the example provided to work in any variation. The
enumerator would not work as if there were no attribute in the block.



Tony: as far as books, I have only the book by Jerry Winters. I know not of
any others. All my code is an amalgamation of what I have seen in tutorials
on the autodesk site, code from the forums and what I've learned in the
book.



I've been forced to find a stupid workaround for this problem as you cannot
access the attributes as you can enter them. Seems pretty stupid to me, but
oh well.
Message 8 of 63
myspambox111
in reply to: myspambox111

Ya I assumed that there was some reason for using the enumerator as opposed to the foreach. Guess not.

I attached my function (its vb) with what I was trying to do. Its just gets the default values as I said earlier, and not the values I already loaded.

Thanks for your help Tony.
Message 9 of 63
sgsawdy
in reply to: myspambox111

How about some actual code to show how to get For Each/Next to work in place of GetEnumerator? You keep making this statement, but there is no example code to show it actually working where as there is code that shows the GetEnumerator working in VB.NET. As for Jerry's book spreading bad code, where is your book showing good code? Some of us have nothing else to work with and have been able to keep our jobs by using what we can find.

Please don't get me wong on this Tony, you are a programming guru, but sometimes you are more frustrating than helpful.

Scott
Message 10 of 63

I have something to say...



First of all the for/each does work in VB.net; it is like so



For Each oid as ObjectID in BTR

entity = trans.getobject(oid,...)

next



I have had trouble using For Each object in collection Next statements when I could have used For I as integer = 0 to 1 Next statements. It appears that some user's computer configurations don't get along with For Each loops at all. Now put that in your pipe and smoke it. When you come up with divine intervention on why, please let me know.



jvj
Message 11 of 63
sgsawdy
in reply to: myspambox111

Jamie, I use the For x = 1 to val.count/Next statements all over the place. What I can't seem to figure out is how to replace using GetEnumerator in the following code with For Each/Next. No matter what I have tried, I hang on these three lines of the code:

If TypeOf aadEnt Is AttributeDefinition Then
Dim aadAttdef As AttributeDefinition = aadEnt
Dim aadAttref As New AttributeReference

when using For Each/Next. The error I get has to do with object type. It just does not want to let me equate aadEnt as an AcadEntity with aadAttDef as an AttributeDefinition. I am stumped.


_
Public Sub AddAttDef(ByVal aadBref As DatabaseServices.ObjectId, _
ByVal aadTag As String, _
ByVal aadText As String, _
ByVal aadPrompt As String, _
ByVal aadVisible As Boolean, _
ByVal aadPoint As Boolean, _
ByVal aadAlign As Boolean)

Dim aadTransMan As DatabaseServices.TransactionManager
Dim aadTrans As DatabaseServices.Transaction
Dim aadDwg As Document
Dim aadBT As BlockTable
Dim aadBTR As BlockTableRecord
aadDwg = Application.DocumentManager.MdiActiveDocument
aadTransMan = aadDwg.TransactionManager
aadTrans = aadTransMan.StartTransaction
Dim aadBlkRef As DatabaseServices.BlockReference

'Open the database for Write
aadBT = aadDwg.Database.BlockTableId.GetObject(OpenMode.ForWrite)
aadBlkRef = aadBref.GetObject(OpenMode.ForWrite)

Dim aadAttColl As DatabaseServices.AttributeCollection
Dim aadEnt As Entity
Dim aadbtre As BlockTableRecordEnumerator

aadBTR = aadBT(aadBlkRef.Name).GetObject(OpenMode.ForWrite)
aadbtre = aadBTR.GetEnumerator

'Add attribute definition to BlockTableRecord
aadBTR.AppendEntity(New DatabaseServices.AttributeDefinition( _
New Geometry.Point3d(0, 0, 0), "", aadTag, aadPrompt, _
Nothing))

aadAttColl = aadBlkRef.AttributeCollection
While aadbtre.MoveNext
aadEnt = aadbtre.Current.GetObject(OpenMode.ForWrite)
If TypeOf aadEnt Is AttributeDefinition Then
Dim aadAttdef As AttributeDefinition = aadEnt
Dim aadAttref As New AttributeReference
If aadAttdef.Tag = "LENGTH" Then
aadAttref.SetAttributeFromBlock(aadAttdef, _
aadBlkRef.BlockTransform)
aadAttref.TextString = aadAttdef.TextString
aadAttColl.AppendAttribute(aadAttref)
aadTrans.AddNewlyCreatedDBObject(aadAttref, True)
End If
End If
End While


'Commit the Transaction
aadTrans.Commit()

'Dispose of the Transaction Objects
aadTrans.Dispose()
aadTransMan.Dispose()
End Sub

Never took up smoking, but I am absolutely open to being taught a better way to do something.

Scott
Message 12 of 63
myspambox111
in reply to: myspambox111

The for each is not my problem. I know it can be done, I use it all the time. I only used the enumerator here because I saw it in Jerry Winters book in his code for setting attributes.

The for each is just:

For each id as ObjectId in myBlockTableRecord
Dim ent as Entity = trans.getObject(id, OpenMode.ForRead)

If TypeOf ent is WhateverObjectYouWant Then
Dim whatever as WhateverObjectYouWant
etc....

I just want to access the attributes that I so easily set. Looping through the records is not my problem. Its getting the attributes that I've already set.
Message 13 of 63
NathTay
in reply to: myspambox111

This is what your code is doing and why you are not getting the results you expect.



It is creating a new block reference and setting its values from the block definition then returning what is obviously going to be the default value.
Message 14 of 63
myspambox111
in reply to: myspambox111

Well my blockReference (BRef) is based on my blockTableRecord. I assumed it would carry its current attribute values over.

How would I create the block reference used from the block which actually contains my values?
Message 15 of 63
NathTay
in reply to: myspambox111

The BlockTableRecord is the definition of the block. You obviously have no idea of how block definitions and references work so I would suggest you reread the relevant section of Jerry's book. I would hope that despite the criticisms of the book that it at least gives a reasonable explanation.



Are you a programmer jumping into AutoCAD or a AutoCAD user jumping into programming?
Message 16 of 63
myspambox111
in reply to: myspambox111

I'm about a mid-level programmer helping an architect automate drawings. Things have been working well so far but my biggest beef with the autocad .net api is that there is simply no explanations of methods and objects. In normal .NET programming, tooltips explain everything so well. There is simply no explanation at all when working with autocad so its pretty much shooting in the dark.

I've read what he said in the book as well as what has been said in just about every autocad .net tutorial on the net. The autocad .net api simply defies semantics and common sense in the way it deals with naming conventions and manipulation of objects. Its like you have to create and object to reference and object to change an object. Its madness when trying to get it down. Especially when its not your full time job. Edited by: myspambox111 on Feb 2, 2009 10:28 PM
Message 17 of 63
NathTay
in reply to: myspambox111

I am presuming you have a titleblock in a drawing that you want to read the attributes from. If you open that drawing if you type the insert command you will have a drop down list of blocks to choose from which will include the tileblock. This is the block definition that is defined by the BlockTableRecord. When it is inserted into the drawing it is a block reference. So programmaticaly you need to access any block references not the block definition. Hopefully Jerry's book explains this.
Message 18 of 63
Anonymous
in reply to: myspambox111

Why would I have to show somoene how to use a basic
language feature like For Each/Next ?

People learn those things when they learn to use the
programming language, and that seems to be the
problem with many who simply look to an AutoCAD-
specific book for that.

The book in question doesn't teach you how to use
the VB.NET language, and that's the missing piece
of the puzzle that some seem to think that book is
going to fill.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6116086@discussion.autodesk.com...
How about some actual code to show how to get For Each/Next to work in place
of GetEnumerator? You keep making this statement, but there is no example
code to show it actually working where as there is code that shows the
GetEnumerator working in VB.NET. As for Jerry's book spreading bad code,
where is your book showing good code? Some of us have nothing else to work
with and have been able to keep our jobs by using what we can find. Please
don't get me wong on this Tony, you are a programming guru, but sometimes
you are more frustrating than helpful. Scott
Message 19 of 63
Anonymous
in reply to: myspambox111

{quote}

I have had trouble using For Each object in collection Next statements when
I could have used For I as integer = 0 to 1 Next statements. It appears that
some user's computer configurations don't get along with For Each loops at
all

{quote}

This serves to show how clueless you are.

The VB.NET compiler takes For/Each and spits out the
same GetEnumerator()/MoveNext() code that you erroneously
write, but it also adds the needed call to Dispose() on the
object returned by GetEnumerator() which to date, I have seen
no VB.NET programmer (or C# programmer for that matter)
do, even though it is quite necessary.

Get lost.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6116143@discussion.autodesk.com...
I have something to say...



First of all the for/each does work in VB.net; it is like so



For Each oid as ObjectID in BTR

entity = trans.getobject(oid,...)

next



I have had trouble using For Each object in collection Next statements when
I could have used For I as integer = 0 to 1 Next statements. It appears that
some user's computer configurations don't get along with For Each loops at
all. Now put that in your pipe and smoke it. When you come up with divine
intervention on why, please let me know.



jvj
Message 20 of 63
Anonymous
in reply to: myspambox111

The AutoCAD .NET API is not nearly as easy to use as VBA, so
perhaps the problem is that some seem to have gotten that idea
from a book or something.

In fact, it takes some experience and training in order for one to
be able to effectively use the API.

If you're planning to use it, then you need to ask a lot of questions
here, and use any book that you can find, including the one that
I'm bashing for demonstrating bad/totally incorrect programming.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com

Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm


wrote in message news:6116631@discussion.autodesk.com...
I'm about a mid-level programmer helping an architect automate drawings.
Things have been working well so far but my biggest beef with the autocad
.net api is that there is simply no explanations of methods and objects. In
normal .NET programming, tooltips explain everything so well. There is
simply no explanation at all when working with autocad so its pretty much
shooting in the dark. I've read what he said in the book as well as what has
been said in just about every autocad .net tutorial on the net. The autocad
.net api simply defies semantics and common sense in the way it deals with
naming conventions and manipulation of objects. Its like you have to create
and object to reference and object to change an object. Its madness when
trying to get it down. Especially when its not your full time job. Edited
by: myspambox111 on Feb 2, 2009 10:28 PM

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