Modify value of parameter on existing Dynamic Block

Modify value of parameter on existing Dynamic Block

franciscovHWABV
Contributor Contributor
2,588 Views
8 Replies
Message 1 of 9

Modify value of parameter on existing Dynamic Block

franciscovHWABV
Contributor
Contributor

Hi all.

I have been trying to write a program which takes the value of an existing dynamic block parameter and changes into a new one.

Lets say I have a rectangle block in my drawing named "Rectangle" which has two parameters ( named long and wide) and I want to change them from their actual state to a new value.

I have been searching and I found the below Lee Mac code, but I do not understand it so I can not implement it to my purpose. First I was trying to change blk prp into "Rectangle" "long" 200 (200 is the original length of the rectangle) but this obviously did not work: "too few arguments". I have been getting information about every function on this code but I cannot understand the full procedure of it all to adapt it to my purpose. 

If someones please could tell me what I have been doing wrong or some information about the operations that the code is doing here it could be a great help.

Thanks!

 

;; Set Dynamic Block Property Value  -  Lee Mac
;; Modifies the value of a Dynamic Block property (if present)
;; blk - [vla] VLA Dynamic Block Reference object
;; prp - [str] Dynamic Block property name (case-insensitive)
;; val - [any] New value for property
;; Returns: [any] New value if successful, else nil

(defun LM:setdynpropvalue ( blk prp val )
    (setq prp (strcase prp))
    (vl-some
       '(lambda ( x )
            (if (= prp (strcase (vla-get-propertyname x)))
                (progn
                    (vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))
                    (cond (val) (t))
                )
            )
        )
        (vlax-invoke blk 'getdynamicblockproperties)
    )
)

 

0 Likes
Accepted solutions (3)
2,589 Views
8 Replies
Replies (8)
Message 2 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

Possibly like this...

 

(LM:setdynpropvalue (vlax-ename->vla-object (car (entsel))) "long" 200)

 

0 Likes
Message 3 of 9

franciscovHWABV
Contributor
Contributor

Hi and thanks Z9E3zK5E!

 

Now its working great but he asks me to select the block, I would need the program to automatically read the block by its name.

I tried to replace ...(vlax-ename->vla-object (car (entsel)))... into "Rectangle" (name of the current block) but it does not work. I have the feeling that this is related with the vla functions on the Lee Mac code and there is were I start to get lost.

 

Thanks very much for the help.

0 Likes
Message 4 of 9

ВeekeeCZ
Consultant
Consultant

Do you have only one reference of that block in the drawing? You know, it's a block so you can have multiple of them in the drawing.. and in that case, you need somehow identify the correct one. 

Or doo you have more references and you want to change this parameter to all of them?

0 Likes
Message 5 of 9

franciscovHWABV
Contributor
Contributor

Ooh yes. There will be multiple blocks but none of them will be called the same so there is no problem referring to them in the program by their name.

0 Likes
Message 6 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

Ok then, be it simple now.

 

(:setdynpropvalue "rectangle" "long" 200)

 

(vl-load-com)

(defun :setdynpropvalue (bnm prp val / blk s e i o)
  (setq bnm (strcase bnm)
	prp (strcase prp))
  (if (setq s (ssget "_X" '((0 . "INSERT"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i)))
	    o (vlax-ename->vla-object e))
      (if (= bnm (strcase (if (vlax-property-available-p o 'effectivename)
			    (vla-get-effectivename o)
			    (vla-get-name o))))
	(setq blk o))))
  (if blk
    (vl-some
      '(lambda (x)
	 (if (= prp (strcase (vla-get-propertyname x)))
	   (progn
	     (vla-put-value x (vlax-make-variant val (vlax-variant-type (vla-get-value x))))
	     (cond (val) (t)))))
      (vlax-invoke blk 'getdynamicblockproperties))))

 

0 Likes
Message 7 of 9

Sea-Haven
Mentor
Mentor

A dynamic block has a name like U23 next insert say U24 so how do you know its name ? A dynamic block does have an effective name and that may be what your referring to as I said already a current name is added by Autocad. You can get blocks and look for effective name but what happens when more than one in dwg ?

 

That is why entsel is used. For a shape like rectang I could imagine being inserted multiple times.

 

 

0 Likes
Message 8 of 9

franciscovHWABV
Contributor
Contributor

Hi all and thanks for the answers!

Sea-Heaven, I think I understand you and I have found out the problem right now.

 

What I really will do is something like having a block called "rectangle" in the drawing and then insert it as many times with coordinates, scale, rotation using the command "_-insert". The amount of rectangles and all its data are coming from an Excel but that is not a problem so I have that part of the program working already. So, when I insert every block I wanted to rename it as below with a counter variable, "rectangle1", "rectangle2" and so on:

 

(command "_-insert" "rectangle" insertpoint xscale yscale rotation)
(set (read (strcat "rectangle" (itoa nrectangle))) (entlast))

 

This is all inside a loop where I have placed the program by Z9E3zK5E, which was working perfectly great until I made my modifications...:

 

(:setdynpropvalue (eval (read (strcat "slat" (itoa nrectangle)))) "long" length)

 

As in every loop I am inserting a new block and recalling it I think the problem is that I am giving a new name to the entity but not to the block itself, so the program by Lee Mac is calling to the same block every time. Anyways, I have tried that and the program inserts the first block and then comes out with a type of argument error message. I am leaving you attached the full program and excel so you can see it properly.

 

Once again thanks and sorry if I did not explain properly at first.

 

 

 

0 Likes
Message 9 of 9

franciscovHWABV
Contributor
Contributor
Accepted solution

Ok. I have made it work!

 

I only had to use the first program by Z9E3zK5E and replace the upper line into the bottom one.

 

Thanks all for your messages and dedication, I am learning a lot with you and this forum. Really appreciate it boys!

 

(:setdynpropvalue "rectangle" "long" 200)

(LM:setdynpropvalue (vlax-ename->vla-object (eval (read (strcat "rectangle" (itoa nrectangle))))) "long" long)

 

 

0 Likes