newbie. help appreciated

newbie. help appreciated

Anonymous
Not applicable
436 Views
12 Replies
Message 1 of 13

newbie. help appreciated

Anonymous
Not applicable
i know vba but am new to autocad. need to loop through all blocks (there's 153 different blocks) and edit the value of the attribute that each block has (attribute has the same name in each block). can anyone please point me to some quality tutorials or examples so i can figure out how to proceed please. i would greatly appreciate it. i'd like to do it w/out selecting anything or using forms. just open the drawing and the values are updated. the updated text will come from a database. i already know this part. just can't find any info on attribute collections or anything to loop through.

thanks in advance.
ms
0 Likes
437 Views
12 Replies
Replies (12)
Message 2 of 13

Anonymous
Not applicable
const BlName = "myBlockName"
const AttTagString = "myAttributeName"
const NewValue = "myNewValue"

dim tBlRef as acadblockreference
dim tAtts as variant
dim i as integer
dim tSSet as acadselectionset
dim tDXFCode(0) as integer
dim tDXFValue(0) as variant

'first built a selectionset to get all blockrefs you need to change
on error resume next
set tSSet = thisdrawing.selectionsets.add("mySelSetName")
if err.number <> 0 then
set tSSet = thisdrawing.selectionsets("mySelSetName")
tSSet.clear
end if
tdxfcode(0) = 2:tdxfvalue(0) = BlName
tsset.select acselectionsetall,,,tDXFCode,tDXFValue

'for each blockref scan through the attributes
for each tBlRef in tsset
if tBlRef.hasattributes then
tatts=tBlRef.getattributes
for i = lbound(tatts) to ubound(tatts)
if ucase(tatts(i).tagstring = ucase(AttTagString) then
'now we have the attribute to change
tatts(i).textstring = NewValue
exit for
endif
next
end if
next


hope not to make to much typing-errors, - alfred -



In article , mschaffhausen says...
> know vba but am new to autocad. need to loop through all blocks (there's 153 different blocks) and edit the value of the attribute that each block has (attribute has the same name in each block). can anyone please point me to some quality tutorials or examples so i can figure out how to proceed please. i would greatly appreciate it. i'd like to do it w/out selecting anything or using forms. just open the drawing and the values are updated. the updated text will come
from a database. i already know this part. just can't find any info on attribute collections or anything to loop through.

thanks in advance.

> ms
>

0 Likes
Message 3 of 13

Anonymous
Not applicable
wow. thanks. i'll try out the code. didn't expect you to actually do the work for me. i do have some questions though about the code you posted.

what is a selectionset? i still need one even if i don't have anything selected? there isn't a collection or anything i can loop through?

what do these 2 lines of code do?
tdxfcode(0) = 2:tdxfvalue(0) = BlName
tsset.select acselectionsetall,,,tDXFCode,tDXFValue

if each block in my drawing is unique do i still use acadblockreference or is each one just an acadblock? the drawing was converted from an archview shapefile.

each block has its own name. how do i use your line:
BlName = "myBlockName"

thanks again.
0 Likes
Message 4 of 13

Anonymous
Not applicable
a selectionset gives you a collection of entities filtered by geometry-type, so
you don't have to scan the whole drawing each line, circle, arc, .... to get
only that type of geom you'd like to work on.

the collection (which you missed) is "tSSet", this holds all blockreferences
and with my line "for each tBlRef in tSSet" i do a loop for each blockreference
found.

the definition of what to filter, in this case that means:
tDXFCode(0) = 2...i want to filter for blockname
tDXFValue(0) = "myBlockName" (or the const) ... is the blockname to search for

you mentioned: your blockname is unique doesn't change anything to my example
except that you don't need do loop through the selectionset but to check if
this is found. but carefully (because there are often misunderstandings) for
example you do have a titleblock called "TITLE" and you have placed it 4 times
into your drawing, then you do have ONE block(-definition) but FOUR
blockrefences and only the blockreferences have values in attributes (and will
be vilsible on your drawing.

const BlName = "myBlockName" ==> is just for having a readable sample, simply
change this value to your blockname and the sample works or give it to the sub
as a parameter.

- alfred -

In article , mschaffhausen says...
> wow. thanks. i'll try out the code. didn't expect you to actually do the work for me. i do have some questions though about the code you posted.

what is a selectionset? i still need one even if i don't have anything selected? there isn't a collection or anything i can loop through?

what do these 2 lines of code do?

> tdxfcode(0) = 2:tdxfvalue(0) = BlName

> tsset.select acselectionsetall,,,tDXFCode,tDXFValue

if each block in my drawing is unique do i still use acadblockreference or is each one just an acadblock? the drawing was converted from an archview shapefile.

each block has its own name. how do i use your line:

> BlName = "myBlockName"

thanks again.
>

0 Likes
Message 5 of 13

Anonymous
Not applicable
1.a selectionset is like a group, it's an autocad
term.  before you can process a bunch of autocad entities they must first
be selected.  In visual basic this translates to a selection set
object.

 

 

2.tdxfcode(0) =
2:tdxfvalue(0) = BlName

 

If you are going to program in autocad it's very
imprtant that yo become familiar with group codes.  I think it may be in
autocad's help.  I use a autolisp programming book where they are
listed.  Group codes are basically the way that information regarding
entities are stored in the autCAD 'database'.

 

3. blocks in autocad are a sort of dictionary
definition.  they are a group of enitities given a name and stored behind
the scenes.  a reference is an instance of the block that actually appears
in the drawing.

 

4. I believe that BlName here should be "INSERT"
and  tdsxfcode equal 0.  This will select all block entities. 
the other selected any entity with a given name.  again, familiarize
yourself with group codes.

 

Edward

 

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
wow.
thanks. i'll try out the code. didn't expect you to actually do the work for
me. i do have some questions though about the code you posted.

what is a selectionset? i still need one even if i don't have anything
selected? there isn't a collection or anything i can loop through?

what do these 2 lines of code do?
tdxfcode(0) = 2:tdxfvalue(0) = BlName

tsset.select acselectionsetall,,,tDXFCode,tDXFValue

if each block in my drawing is unique do i still use acadblockreference or
is each one just an acadblock? the drawing was converted from an archview
shapefile.

each block has its own name. how do i use your line:
BlName =
"myBlockName"

thanks again.

0 Likes
Message 6 of 13

Anonymous
Not applicable
sorry to make a little correction

tdxfcode(0) = 2:tdxfvalue(0) = BlName
...is exactly what i wanted, because code 2 means "value is blockname" (so i
don't have to add 0|"INSERT" because this is already included.

my sample searches only for blockreferences with the name given at the top of
my code:
const BlName = "myBlockName"

- alfred -

In article <6DFE90B8742C3981A16E7178CAFB3091@in.WebX.maYIadrTaRb>,
Ebagby@superstructures.com says...
>
>
1.a selectionset is like a group, it's an autocad
> term.  before you can process a bunch of autocad entities they must first
> be selected.  In visual basic this translates to a selection set
> object.

>
 

>
 

>
2.tdxfcode(0) =
> 2:tdxfvalue(0) = BlName

>
 

>
If you are going to program in autocad it's very
> imprtant that yo become familiar with group codes.  I think it may be in
> autocad's help.  I use a autolisp programming book where they are
> listed.  Group codes are basically the way that information regarding
> entities are stored in the autCAD 'database'.

>
 

>
3. blocks in autocad are a sort of dictionary
> definition.  they are a group of enitities given a name and stored behind
> the scenes.  a reference is an instance of the block that actually appears
> in the drawing.

>
 

>
4. I believe that BlName here should be "INSERT"
> and  tdsxfcode equal 0.  This will select all block entities. 
> the other selected any entity with a given name.  again, familiarize
> yourself with group codes.

>
 

>
Edward

>
 

>
 

>
 

>

> style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
> wow.
> thanks. i'll try out the code. didn't expect you to actually do the work for
> me. i do have some questions though about the code you posted.
>

what is a selectionset? i still need one even if i don't have anything
> selected? there isn't a collection or anything i can loop through?
>

what do these 2 lines of code do?
tdxfcode(0) = 2:tdxfvalue(0) = BlName
>
tsset.select acselectionsetall,,,tDXFCode,tDXFValue
>

if each block in my drawing is unique do i still use acadblockreference or
> is each one just an acadblock? the drawing was converted from an archview
> shapefile.
>

each block has its own name. how do i use your line:
BlName =
> "myBlockName"
>

thanks again.


>
0 Likes
Message 7 of 13

Anonymous
Not applicable
thanks again to all of you. i definitely have some reading to do before everything soaks in. i have a somewhat unrelated question that maybe you would be willing to help with. i found another piece of code to loop through the blocks and i thought i'd try it to rename each block to something that made more sense. the names appear somewhat random now from the translation. the drawing is a county map w/ zipcodes. 'zip' is the attribute and each zip code area is it's own (uniquely) named block. i thought i'd rename each block to match the zipcode. the following code gives an error and doesn't rename the block. any thoughts? the error is 'key not found' (#80200014)

Dim blkref As AcadBlockReference

For Each Obj In ThisDrawing.ModelSpace
If TypeOf Obj Is AcadBlockReference Then
Set blkref = Obj
If blkref.HasAttributes Then
AttArray = blkref.GetAttributes
For I = LBound(AttArray) To UBound(AttArray)
If AttArray(I).TagString = "ZIP" Then
'MsgBox AttArray(I).TextString 'returns zipcode
'MsgBox blkref.Name 'returns current block name
blkref.Name = AttArray(I).TextString 'error is here
End If
Next I
End If
End If
Next

thanks again. i truly appreciate it.
0 Likes
Message 8 of 13

Anonymous
Not applicable
that's comes to what i mentioned, please don't get confused on the vocabulary
"Block" and "BlockReference". block is just the definition of how a symbol is
built of where the blockreference is nothing more then a simple geometrytype
like a point including a pointer to the block, to know how it is visualised in
the drawing. and so you cannot change the name of a blockreference (in anouther
programming language you set the pointer to it's BaseClass to ??????.

let's start to what is the goal you want to reach? in case of autocad dealing
witch blockreferences has so much advantages which will all get lost when you
make for every postalcode an own block.

- alfred -

(maybe i don't stay more time today (it's 1 am at my time) but i will follow up
in a few hours after a short sleep)

In article , mschaffhausen says...
> thanks again to all of you. i definitely have some reading to do before everything soaks in. i have a somewhat unrelated question that maybe you would be willing to help with. i found another piece of code to loop through the blocks and i thought i'd try it to rename each block to something that made more sense. the names appear somewhat random now from the translation. the drawing is a county map w/ zipcodes. 'zip' is the attribute and each zip code area is it's own
(uniquely) named block. i thought i'd rename each block to match the zipcode. the following code gives an error and doesn't rename the block. any thoughts? the error is 'key not found' (#80200014)

Dim blkref As AcadBlockReference

For Each Obj In ThisDrawing.ModelSpace

>  If TypeOf Obj Is AcadBlockReference Then

>   Set blkref = Obj

>    If blkref.HasAttributes Then

>     AttArray = blkref.GetAttributes

>     For I = LBound(AttArray) To UBound(AttArray)

>      If AttArray(I).TagString = "ZIP" Then

>       'MsgBox AttArray(I).TextString 'returns zipcode

>       'MsgBox blkref.Name 'returns current block name

>       blkref.Name = AttArray(I).TextString 'error is here

>      End If

>     Next I

>    End If

>  End If

> Next

thanks again. i truly appreciate it.
>

0 Likes
Message 9 of 13

Anonymous
Not applicable
i see. after reading your message i was able to get it to work by editing a few lines. here's what i did:

If Attarray(I).TagString = "ZIP" Then 'same as before

myname = blkref.Name 'begin new
Set blk = ThisDrawing.Blocks.Item(myname)
blk.Name = Attarray(0).TextString

End If

getting late here too. i'll pick up tomorrow with the database part. maybe i know enough now to finish it. thanks for everything.
0 Likes
Message 10 of 13

Anonymous
Not applicable
that'ok and now i remember that on one side you said, that your blockref is
unique within your drawing, then this code works.
but do you really have counties (you mentioned each drawing one county) with
only one zip-code?

by the way, it seems that you are working with gis-data, are you using autocad
or autodesk-map? if map, let me know for the next steps today you want to go,
because this could help.

- alfred -

In article , mschaffhausen says...
> see. after reading your message i was able to get it to work by editing a few lines. here's what i did:

If Attarray(I).TagString = "ZIP" Then 'same as before

myname = blkref.Name 'begin new

>  Set blk = ThisDrawing.Blocks.Item(myname)

>  blk.Name = Attarray(0).TextString

End If

getting late here too. i'll pick up tomorrow with the database part. maybe i know enough now to finish it. thanks for everything.
>

0 Likes
Message 11 of 13

Anonymous
Not applicable
actually i probably wrote the message wrong. the drawing (in autocad 2000, not map) contains shapes that represent each zip code for one county. the map doesn't actually contain counties.

while i'm writing. do you happen to know (don't put much time into it, i haven't done any research yet) if it's possible to test whether a block by name exists? i'm going to loop through a database and compare a value to a possible block by the same name. might not exist and i don't want a error message. don't really want to bypass the issue w/ onerror resume next. also, i was playing w/ the dbconnect function to see if it would do the same thing as my vba code. i could link the drawing to the table but is it possible to link a drawing object or attribute to a query? i need to do an aggregate function (sum) and only want that data to show. probably not the right newsgroup for this question.

thanks,
ms
0 Likes
Message 12 of 13

Anonymous
Not applicable
if you want to check, wether a block(-definition) with a specific name exists:

on error resume next
dim tBlk as acadblock
set tblk = thisdrawing.blocks("yourBlockName")
if (err.number = 0) and (not (tblk is nothing)) then
'ok, the block with name "yourBlockName" is defined in the current dwg
endif
--------------------------

if above test is done, we know if the block is defined (or not), but even if it
is defined, it's not clear wether the block was used (inserted), to check that
you can make the steps with the selection set as mentioned:

const BlName = "myBlockName"
dim tSSet as acadselectionset
dim tDXFCode(0) as integer
dim tDXFValue(0) as variant

on error resume next
set tSSet = thisdrawing.selectionsets.add("mySelSetName")
if err.number <> 0 then
set tSSet = thisdrawing.selectionsets("mySelSetName")
tSSet.clear
end if
tdxfcode(0) = 2:tdxfvalue(0) = BlName
tsset.select acselectionsetall,,,tDXFCode,tDXFValue

if tsset.count > 0 '==> ok, there are blockref's used/inserted


- alfred -



In article , mschaffhausen says...
> actually i probably wrote the message wrong. the drawing (in autocad 2000, not map) contains shapes that represent each zip code for one county. the map doesn't actually contain counties.

while i'm writing. do you happen to know (don't put much time into it, i haven't done any research yet) if it's possible to test whether a block by name exists? i'm going to loop through a database and compare a value to a possible block by the same name. might not exist and i don't
want a error message. don't really want to bypass the issue w/ onerror resume next. also, i was playing w/ the dbconnect function to see if it would do the same thing as my vba code. i could link the drawing to the table but is it possible to link a drawing object or attribute to a query? i need to do an aggregate function (sum) and only want that data to show. probably not the right newsgroup for this question.

thanks,

> ms
>

0 Likes
Message 13 of 13

Anonymous
Not applicable
once again, thanks for everythings. looks like i have everything i need to get this job done. i appreciate it.

ms
0 Likes