Problems with setting attributes directly

Problems with setting attributes directly

kamal_kouidar
Contributor Contributor
851 Views
11 Replies
Message 1 of 12

Problems with setting attributes directly

kamal_kouidar
Contributor
Contributor

Hi I get this error when I want to use this funtion.

 

Can someone find me a solution?

 

Or maybe a different way to achieve the functionality.

 

I have block named "Verbruiker". The block has some attributes and I want to have a function to set directly the values for the tag.

 

Thank you!

 

Schermafbeelding 2024-04-17 113946.pngSchermafbeelding 2024-04-17 114104.png

0 Likes
852 Views
11 Replies
Replies (11)
Message 2 of 12

Kent1Cooper
Consultant
Consultant

Your 'attlist' variable is the entity data list of the Block, which does not include the attributes [they have their own entity names].  Therefore each item represented by 'att' in your (foreach) function is an entry like:

(10 30.0936 9.31157 0.0)  [for the insertion point]

or:

(5 . "22F")  [for the handle]

and something like (assoc 1 att) is meaningless.

 

I would suggest you use (getpropertyvalue) and (setpropertyvalue), which can treat Attributes' tags as "properties" of the Block.

(setpropertyvalue (entlast) "TAGCODE_VERBRUIKER" tagcode)

[If the Blocks may not all include all those Attribute tags, you would need to verify which ones are present, because that will fail if that Attribute isn't present.]

Kent Cooper, AIA
0 Likes
Message 3 of 12

kamal_kouidar
Contributor
Contributor

Hi, thank you for your response/solution.

 

I have tried to implement your suggestion and this is what I got.

 

Schermafbeelding 2024-04-17 162415.pngSchermafbeelding 2024-04-17 162437.png

 

I hope we can find a solution!

0 Likes
Message 4 of 12

komondormrex
Mentor
Mentor

hey there,

another approach.

 

(defun insert-verbruiker (x y omschrijving vermogen tagcode rotation / customer_insert text_pos)
	(if (vl-catch-all-error-p 
			(setq customer_insert (vl-catch-all-apply 'vla-insertblock 
													  (list (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
							 					   			(vlax-3d-point x y) 
							 					   			"Verbruiker" 
							 					   			1 1 1 rotation ;	(in radians)
													  )
								  )
			)
		)
		(alert "\nFout bij het invoegen van het block.")
		(foreach attribute (vlax-invoke customer_insert 'getattributes) 
			(if (setq text_pos (vl-position (vla-get-tagtstring attribute) '("OMSCHRIDVING_VERBRUIKER" "GEINST_VERMOGEN_VERBRUIKER" "TAGCODE_VERBRUIKER")))
				(progn
					(vla-put-textstring attribute (nth text_pos (list omschrijving vermogen tagcode)))
					(vla-update customer_insert)
				)
			)
		)
	)
	(princ)
)

 

 

 

0 Likes
Message 5 of 12

ec-cad
Collaborator
Collaborator

I didn't look closely to your original post - the portion of getting the attributes. If your block attributes fill-in

in a certain order, Sea-Haven's response will work fine.

e.g.

(setvar "ATTREQ" 1)

(command "-insert" "verbuiker" (list x y) rotation omschrijving vermogen tagcode)

 

If they (may) be inconsistent in fill-in order then this will do it.

(defun insert-verbruiker (x y omschrijving vermogen tagcode rotation)
(setvar "ATTREQ" 0) ; no attribute dialog please
(command "-insert" "verbuiker" (list x y) "1" "1" "0" rotation)
(setq ent (entlast))
(setq elist (entget ent))
(if (= (cdr (assoc 0 elist)) "INSERT")
(progn
(setq blk (vlax-ename->vla-object ent))
(if (safearray-value (setq atts (vlax-variant-value (vla-getattributes blk))))
(progn
(setq atts (vlax-safearray->list (vlax-variant-value (vla-getattributes blk))))
(foreach att atts
(setq Tag (vla-get-tagstring att))
(if (= Tag "OMSCHRIJVING_VERBRUIKER")
(vla-put-textstring att omschrijving)
); if
(if (= Tag "GEINST_VERMOGEN_VERBRUIKER)
(vla-put-textstring att vermogen)
); if
(if (= Tag "TAGCODE_VERBRUIKER")
(vla-put-textstring att tagcode)
); if
); foreach
); progn
); if
); end progn
); end if
(setvar "ATTREQ" 1); return attribute required

 

ECCAD

 

0 Likes
Message 6 of 12

ec-cad
Collaborator
Collaborator

komondormrex,

Looks like you beat me to the reply by 3 minutes. Your code looks good, except for one tagname.

 

When I post from notepad, copy / paste here in this forum, I get the code (all left-justified), no spaces or

tabs. Could you tell me your method ? What software (adds the line numbers) and maintains the spaces,

tabs ??

I'd appreciate it.

Thanks.

ECCAD

0 Likes
Message 7 of 12

Kent1Cooper
Consultant
Consultant

@ec-cad wrote:

....

When I post from notepad, copy / paste here in this forum, I get the code (all left-justified), no spaces or

tabs. ....


Put it in a

code window

by picking this:

Kent1Cooper_0-1713368545308.png

Kent Cooper, AIA
0 Likes
Message 8 of 12

Kent1Cooper
Consultant
Consultant

Is it the fact that this:

(command "-insert" "verbuiker" (list x y) rotation)

contains nothing for the scale factor(s)?

 

And why not just give it the Attribute values in the process of Inserting?  Set ATTDIA to 0, and ATTREQ to 1, and:

 

(command "_.insert" "verbuiker" (list x y)

  YourScaleFactorX YourScaleFactorY rotation

  omschrijving vermogen tagcode

)

 

If the Block is defined for uniform scaling, use only one YourScaleFactor entry.  And of course you need to supply the Attributes in the right order for the way the Block is defined.

Kent Cooper, AIA
0 Likes
Message 9 of 12

ec-cad
Collaborator
Collaborator
Kent,
The OP (is) passing in the x y and rotation .. e.g.
(defun insert-verbruiker (x y omschrijving vermogen tagcode rotation)

 

And thanks for the tip of how to get that code a little neater. !

ECCAD

0 Likes
Message 10 of 12

ec-cad
Collaborator
Collaborator

Kent,

Spoke before study..

You are correct. The insert needs 'blockname' (list x y) 'ScaleX' 'ScaleY' 'Rotation' 'Att1' 'Att2' 'Att3'

Sorry for barging in...

 

0 Likes
Message 11 of 12

Kent1Cooper
Consultant
Consultant

Another potential issue with the original code:

Kent1Cooper_0-1713380338772.png

Suppose the Insertion fails for some reason, in a way that doesn't kill the whole routine, but the last other thing before that was another Block Insertion?  Then the (if) test will be "satisfied" [the object type of the last object will be "INSERT"], and it will go on to try to assign those Attribute values to that other Block.  That could be a Block for which those Attributes are not applicable, or it could even be another of the same Block, to which you could then be assigning incorrect new values!

 

But if you know the Block has the Attributes, and structure it correctly to supply them during Insertion [see my previous Reply], instead of Inserting it first and afterwards supplying the Attribute values, then the above concern should be moot.

Kent Cooper, AIA
0 Likes
Message 12 of 12

komondormrex
Mentor
Mentor

oops, i did auto recognition and it missed a couple of underscores.

0 Likes