.NET

Reply
Distinguished Contributor
sgsawdy
Posts: 411
Registered: ‎09-19-2005
Message 11 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 06:19 AM 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
Contributor
myspambox111
Posts: 22
Registered: ‎10-07-2008
Message 12 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 01:29 PM 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.
Distinguished Contributor
NathTay
Posts: 1,691
Registered: ‎12-15-2003
Message 13 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 01:41 PM 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.
Contributor
myspambox111
Posts: 22
Registered: ‎10-07-2008
Message 14 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 02:02 PM 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?
Distinguished Contributor
NathTay
Posts: 1,691
Registered: ‎12-15-2003
Message 15 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 02:14 PM 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?
Contributor
myspambox111
Posts: 22
Registered: ‎10-07-2008
Message 16 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 02:19 PM 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
Distinguished Contributor
NathTay
Posts: 1,691
Registered: ‎12-15-2003
Message 17 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 02:34 PM 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.
*Tony Tanzillo
Message 18 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 04:32 PM 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
*Tony Tanzillo
Message 19 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 04:32 PM 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
*Tony Tanzillo
Message 20 of 63 (207 Views)

Re: Trouble getting textstring from attribute definition

02-02-2009 04:32 PM 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community