• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

Add block attributes

  • Thread starter Thread starter Principiante87
  • Start date Start date

Principiante87

Guest
Hello, everyone!
I am continuing my thesis inherent in autocad customization.. great part of the work has already been done, but with expedients that have little to do with autocad.. I have resorted to my most trivial programming capabilities. I am, at this point, asking you a question.
I should add some blocks attributes, in case these are lacking.. The code I wrote for the extrapolation of existing attributes is as follows (and for now it works :) :

dim u as acadblockreference
dim p as string
dim myattrref as autocad.acadattributereference
for each u in thisdrawing.blocks(0)

= 0
c = u.name
if u.hasattributes then
x = (u.getattributes())
set myattrref = x(i)
p = p & " & myattrref.textstring
i = i + 1
end if
end with


I should now proceed with the following logic: if u.hasattribute= false, then add an attribute (which is a string) to that block. Can someone help me? Thank you so much in advance to all!
 
Hello, everyone!
I am continuing my thesis inherent in autocad customization.. great part of the work has already been done, but with expedients that have little to do with autocad.. I have resorted to my most trivial programming capabilities. I am, at this point, asking you a question.
I should add some blocks attributes, in case these are lacking.. The code I wrote for the extrapolation of existing attributes is as follows (and for now it works :) :

dim u as acadblockreference
dim p as string
dim myattrref as autocad.acadattributereference
for each u in thisdrawing.blocks(0)

= 0
c = u.name
if u.hasattributes then
x = (u.getattributes())
set myattrref = x(i)
p = p & " & myattrref.textstring
i = i + 1
end if
end with


I should now proceed with the following logic: if u.hasattribute= false, then add an attribute (which is a string) to that block. Can someone help me? Thank you so much in advance to all!
use the addattributed method
on the help autocad there is qs example
sub example_addattribute()
' this example creates an attribute definition in model space.

dim attributeobj as acadattribute
dim height as double
dim mode as long
dim prompt as string
dim insertionpoint(0 to 2) as double
dim tag as string
dim value as string

' define the attribute definition
height = 1#
mode = acattributemodeverify
prompt = "new prompt"
insertionpoint(0) = 5#: insertionpoint(1) = 5#: insertionpoint(2) = 0
tag = "new_tag"
value = "new value"
' create the attribute definition object in model space
set attributeobj = thisdrawing.modelspace. addattribute(height, mode, prompt, insertionpoint, tag, value)
Zoom in

end

a question: in your example you investigate/act on the attributes of all block references of only 1st block ("item(0)"). Is that what you want? otherwise you must insert an external cycle on all blocks (and consequently declaration of variable acadblock)

Note: Probably your code is just a draft but mark it:
- use "with u" but then continue to use "u" in subsequent references in the with
- the cycle on the "i" is not a real cycle, so eventually you investigate the only first attribute (i=0) for each block reference "u"
-
- impose the value of "c" without having declared or used it
- missing the statement of "x" and "i"
below an example of how I would impose it
dim u as acadblockreference
dim p as string
dim c as string ' aggiunto
'dim myattrref as autocad.acadattributereference ' non "sprecherei una variabile, visto che lo usi solo una volta
dim x as variant ' aggiunto
dim i as integer

for each u in thisdrawing.blocks(0)
with u
if .hasattributes then
x = .getattributes
for i = lbound(x) to ubound(x)
c = .name ' ma dove la usi?

' non userei una variabile myattrref apposta
' se devi richiamere/usare più volte l'attributo x(i) usa un "with x(i)"
' set myattrref = x(i)
' p = p & " " & myattrref.textstring
p = p & " " & x(i).textstring
i = i + 1
next i
end if
end with
next u
It's okay, but I didn't turn it!
Hi.
 
use the addattributed method
on the help autocad there is qs example
sub example_addattribute()
' this example creates an attribute definition in model space.

dim attributeobj as acadattribute
dim height as double
dim mode as long
dim prompt as string
dim insertionpoint(0 to 2) as double
dim tag as string
dim value as string

' define the attribute definition
height = 1#
mode = acattributemodeverify
prompt = "new prompt"
insertionpoint(0) = 5#: insertionpoint(1) = 5#: insertionpoint(2) = 0
tag = "new_tag"
value = "new value"
' create the attribute definition object in model space
set attributeobj = thisdrawing.modelspace. addattribute(height, mode, prompt, insertionpoint, tag, value)
Zoom in

end

a question: in your example you investigate/act on the attributes of all block references of only 1st block ("item(0)"). Is that what you want? otherwise you must insert an external cycle on all blocks (and consequently declaration of variable acadblock)

Note: Probably your code is just a draft but mark it:
- use "with u" but then continue to use "u" in subsequent references in the with
- the cycle on the "i" is not a real cycle, so eventually you investigate the only first attribute (i=0) for each block reference "u"
-
- impose the value of "c" without having declared or used it
- missing the statement of "x" and "i"
below an example of how I would impose it
dim u as acadblockreference
dim p as string
dim c as string ' aggiunto
'dim myattrref as autocad.acadattributereference ' non "sprecherei una variabile, visto che lo usi solo una volta
dim x as variant ' aggiunto
dim i as integer

for each u in thisdrawing.blocks(0)
with u
if .hasattributes then
x = .getattributes
for i = lbound(x) to ubound(x)
c = .name ' ma dove la usi?

' non userei una variabile myattrref apposta
' se devi richiamere/usare più volte l'attributo x(i) usa un "with x(i)"
' set myattrref = x(i)
' p = p & " " & myattrref.textstring
p = p & " " & x(i).textstring
i = i + 1
next i
end if
end with
next u
It's okay, but I didn't turn it!
Hi.
First of all thank you, you were very kind! :) the x and c var had been declared, but I forgot to copy those two lines of code, however thanks for the straight! I will immediately try to implement how much you recommended me.. I only have a doubt, I can with an else to the previous code (or if u.hasattribute is not defined) to add an attribute to that block? because yesterday I made several attempts: extracting attributes from the blocks that possess them, works, but attribution to the blocks that have no attributes assigned no.. I don't know if I explained well. Anyway thank you again!
 
First of all thank you, you were very kind! :) the x and c var had been declared, but I forgot to copy those two lines of code, however thanks for the straight! I will immediately try to implement how much you recommended me.. I only have a doubt, I can with an else to the previous code (or if u.hasattribute is not defined) to add an attribute to that block? because yesterday I made several attempts: extracting attributes from the blocks that possess them, works, but attribution to the blocks that have no attributes assigned no.. I don't know if I explained well. Anyway thank you again!
test so
dim u as acadblockreference
dim p as string
dim c as string ' added
'dim myattrref as autocad.acadattributereference' I would not "preach a variable, since you only use it once
dim myattrref as autocad.acadattributereference
dim x as variant ' added
dim i as integer

dim height as double
dim prompt as string, tag as string, value as string
dim mode as integer
dim insertionpoint(0 to 2) as double

for each u in thisdrawing.blocks(0)

if .hasattributes then
x = .getattributes
for i = lbound(x) to ubound(x)
c = .name but where do you use it?

'I wouldn't use a myattrref variable on purpose
If you need to call/use the x(i) attribute several times, use a "with x(i)"
'set myattrref = x(i)
'p = p & " & myattrref.textstring
p = p & " " & x(i).textstring
i = i + 1
next i



' define the attribute definition
height = 1# ' see what size you need if you need
mode = acattributemodeinvisible ' see what is the way it does to your case (see help)
prompt = "new prompt" ' see what you need. Note: If you put mode=acattributemodeconstant, the prompt is disabled
insertionpoint(0) = 5#: insertionpoint(1) = 5#: insertionpoint(2) = 0' see how to define it, maybe using the coordinates of one of the tops you get from "boundingbox" or other

tag = "new_tag"
value = "new value"

set myattrref = .addattribute(height, acattributemodeinvisible, prompt, insertionpoint, tag, value)

end if
end with


end
usually, as compilation works, but from here on you have to bang your head to get the operation you want.
the online help of autocad vba is a good reference. but my experience is that you often have to try and try again to find out how a command actually works or something. and Spesos discover that certain things work counterintuitively. sometimes you find that there is no way to do something. the fact that autocad has abandoned the development of the vba is a fact to keep always held
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top