Author Topic: copy attribute value to block's winthin a distance  (Read 3412 times)

0 Members and 1 Guest are viewing this topic.

DraxJDM

  • Newt
  • Posts: 47
copy attribute value to block's winthin a distance
« on: October 25, 2016, 04:54:48 AM »
hey all,

a request, and not a small one i'm sorry.

i try to copy a list of attribute values from a block, to other block's that are within a given distance of de source block.
the block's to match are 2 are 3 not all the blocks in the distance from the source block

The drawing contains a few hundred cabinet blocks, all the switches and lightning blocks within a distance of 15m of a cabinet should get 4 to 6 attribute values from de source block, not all. 
The blocks are dynamic, visibility and rotation. The attribute tags are the same within the different blocks.
and if possible with a choise of a sqare are circilar distance.



i just don't have a idea where to start :-(

greetz John
« Last Edit: October 25, 2016, 04:23:48 PM by john »

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #1 on: October 25, 2016, 04:30:19 PM »

i forgot to add a exemple drawing, now the is one in the top post.

i whant to copy from block "CFab__To_LS-Bord" the attribute values from, "GEO__ZONE", "GEO__OBJECT", "ELEC__ZONE" and "ELEC__BORD_ID"
to the blocks around inside the cirkel, the cirkel is just a reference distance, not  precent in the real drawing.

hope this helps you guys to help me

greetz John

ronjonp

  • Needs a day job
  • Posts: 7527
Re: copy attribute value to block's winthin a distance
« Reply #2 on: October 25, 2016, 05:20:09 PM »
Here's a quick example to fill in blocks within a radius of a 'base' block ( not much error checking ):

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b blocks d p s tags x)
  2.   (setq tags '("GEO__ZONE" "GEO__OBJECT" "ELEC__ZONE" "ELEC__BORD_ID"))
  3.   (if (and (setq b (car (entsel "\nPick base block: ")))
  4.       (vlax-property-available-p (setq b (vlax-ename->vla-object b)) 'hasattributes)
  5.       (setq p (vlax-get b 'insertionpoint))
  6.       (setq tags (vl-remove 'nil
  7.              (mapcar '(lambda (x)
  8.                    (if   (vl-position (vla-get-tagstring x) tags)
  9.                      (cons (vla-get-tagstring x) (vla-get-textstring x))
  10.                    )
  11.                  )
  12.                 (vlax-invoke b 'getattributes)
  13.              )
  14.             )
  15.       )
  16.       (setq d (getdist "\nEnter distance to search: "))
  17.       (setq blocks (ssget "_x" '((0 . "insert") (66 . 1))))
  18.       (setq blocks (mapcar 'vlax-ename->vla-object (mapcar 'cadr (ssnamex blocks))))
  19.       )
  20.     (foreach block blocks
  21.       (if (<= (distance p (vlax-get block 'insertionpoint)) d)
  22.    (mapcar   '(lambda (x)
  23.          (if (setq s (cdr (assoc (vla-get-tagstring x) tags)))
  24.            (vla-put-textstring x s)
  25.          )
  26.        )
  27.       (vlax-invoke block 'getattributes)
  28.    )
  29.       )
  30.     )
  31.   )
  32.   (princ)
  33. )
« Last Edit: October 26, 2016, 09:31:22 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #3 on: October 26, 2016, 02:51:13 AM »
Thx ronjonp.

i think i do something wrong ..
i get a  "error: bad argument type: lselsetp nil" ...

and i only whant to put the attribute value to the twoo blocks in the sample drawing, not in others



roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: copy attribute value to block's winthin a distance
« Reply #4 on: October 26, 2016, 04:19:36 AM »
Code: [Select]
(ssnamex ss)should be:
Code: [Select]
(ssnamex blocks)

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #5 on: October 26, 2016, 05:29:36 AM »
Hey Roy, thank you

now it is working.


is it hard to change, so that it doing this for every block instance of the selected block in the drawing, not just the one i picked?


greetz john

ronjonp

  • Needs a day job
  • Posts: 7527
Re: copy attribute value to block's winthin a distance
« Reply #6 on: October 26, 2016, 09:32:33 AM »
Code: [Select]
(ssnamex ss)should be:
Code: [Select]
(ssnamex blocks)
Oops 😁 my bad .. thanks for picking this up Roy.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #7 on: December 21, 2016, 04:43:12 PM »
a little exra help please...

is it possible to let this work for every instance of the selected block, it is a dynamic block.
there are a few hundred "base" blocks in the drawing.

this would be very helpful for me


as a extra,  copy the attribute value not just from attribute to the same atribute, but from one source attribute to a different attribute, 3 or 4 sets of this.


thx

ronjonp

  • Needs a day job
  • Posts: 7527
Re: copy attribute value to block's winthin a distance
« Reply #8 on: December 21, 2016, 11:39:16 PM »
Give this a try for multiple blocks. I added comments so you can try to understand what's going on.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b bases blocks d en p s ss tags x)
  2.   ;; RJP 12/21/2016
  3.   (if (and ;; Get the base block
  4.            (setq b (car (entsel "\nPick base block: ")))
  5.            ;; Check if it has attributes
  6.            (vlax-property-available-p (setq b (vlax-ename->vla-object b)) 'hasattributes)
  7.            ;; Get effectivename .. yes this can bonk if no effective name prop ( I'll let you add a check :P )
  8.            (setq en (vlax-get b 'effectivename))
  9.            ;; Get all blocks in the drawing minus the 'bases' .. effective name bonky'ness could exist here too
  10.            (setq blocks (vl-remove-if
  11.                           '(lambda (x) (= (vlax-get x 'effectivename) en))
  12.                           (setq ss (mapcar 'vlax-ename->vla-object
  13.                                            (mapcar 'cadr (ssnamex (ssget "_x" '((0 . "insert") (66 . 1)))))
  14.                                    )
  15.                           )
  16.                         )
  17.            )
  18.            ;; Duh
  19.            (setq d (getdist "\nEnter distance to search: "))
  20.            ;; Get the base blocks
  21.            (setq bases (vl-remove-if-not '(lambda (x) (= (vlax-get x 'effectivename) en)) ss))
  22.       )
  23.     (progn ;; Foreach base
  24.            (foreach base bases
  25.              ;; Get the base insertion point
  26.              (setq p (vlax-get base 'insertionpoint))
  27.              ;; Get a list of tagstrings & values from the 'base'
  28.              (setq tags
  29.                     (vl-remove 'nil
  30.                                (mapcar '(lambda (x) (cons (vla-get-tagstring x) (vla-get-textstring x)))
  31.                                        (vlax-invoke base 'getattributes)
  32.                                )
  33.                     )
  34.              )
  35.              ;; Cycle through all the other attributed blocks
  36.              (foreach block blocks
  37.                ;; If the distance is bueno ..
  38.                (if (<= (distance p (vlax-get block 'insertionpoint)) d)
  39.                  ;; Put the base block's value into the 'radius' block
  40.                  (mapcar '(lambda (x)
  41.                             (if (setq s (cdr (assoc (vla-get-tagstring x) tags)))
  42.                               (vla-put-textstring x s)
  43.                             )
  44.                           )
  45.                          (vlax-invoke block 'getattributes)
  46.                  )
  47.                )
  48.              )
  49.            )
  50.     )
  51.   )
  52.   ;; ssshhh
  53.   (princ)
  54. )
« Last Edit: December 21, 2016, 11:42:47 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #9 on: December 22, 2016, 12:18:04 PM »
Thx roy  this works perfectly for me  :-)

a special thx for the comments, this helps me a little more in understanding the code



ronjonp

  • Needs a day job
  • Posts: 7527
Re: copy attribute value to block's winthin a distance
« Reply #10 on: December 22, 2016, 12:48:33 PM »
Thx roy Ron this works perfectly for me  :)

a special thx for the comments, this helps me a little more in understanding the code
You're welcome Charlie.  ;D

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #11 on: December 22, 2016, 01:20:18 PM »
charlie bit in trouble

now I just have to try how to just copy certain attrubites  :mrgreen:

ronjonp

  • Needs a day job
  • Posts: 7527
Re: copy attribute value to block's winthin a distance
« Reply #12 on: December 22, 2016, 01:31:37 PM »
charlie bit in trouble

now I just have to try how to just copy certain attrubites  :mrgreen:
Try this version ... it will only copy the defined tags: ("GEO__ZONE" "GEO__OBJECT" "ELEC__ZONE" "ELEC__BORD_ID")
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b bases blocks d en p s ss tags x)
  2.   ;; RJP 12/21/2016
  3.   (if (and ;; Get the base block
  4.            (setq b (car (entsel "\nPick base block: ")))
  5.            ;; Check if it has attributes
  6.            (vlax-property-available-p (setq b (vlax-ename->vla-object b)) 'hasattributes)
  7.            ;; Get effectivename .. yes this can bonk if no effective name prop ( I'll let you add a check :P )
  8.            (setq en (vlax-get b 'effectivename))
  9.            ;; Get all blocks in the drawing minus the 'bases' .. effective name bonky'ness could exist here too
  10.            (setq blocks (vl-remove-if
  11.                           '(lambda (x) (= (vlax-get x 'effectivename) en))
  12.                           (setq ss (mapcar 'vlax-ename->vla-object
  13.                                            (mapcar 'cadr (ssnamex (ssget "_x" '((0 . "insert") (66 . 1)))))
  14.                                    )
  15.                           )
  16.                         )
  17.            )
  18.            ;; Duh
  19.            (setq d (getdist "\nEnter distance to search: "))
  20.            ;; Get the base blocks
  21.            (setq bases (vl-remove-if-not '(lambda (x) (= (vlax-get x 'effectivename) en)) ss))
  22.       )
  23.     (progn ;; Foreach base
  24.            (foreach base bases
  25.              ;; Get the base insertion point
  26.              (setq p (vlax-get base 'insertionpoint))
  27.              ;; Defined tag names to copy
  28.              (setq tags '("GEO__ZONE" "GEO__OBJECT" "ELEC__ZONE" "ELEC__BORD_ID"))
  29.              ;; Get a list of tagstrings & values from the 'base'
  30.              (setq tags (vl-remove 'nil
  31.                                    (mapcar '(lambda (x)
  32.                                               (if (vl-position (vla-get-tagstring x) tags)
  33.                                                 (cons (vla-get-tagstring x) (vla-get-textstring x))
  34.                                               )
  35.                                             )
  36.                                            (vlax-invoke base 'getattributes)
  37.                                    )
  38.                         )
  39.              )
  40.              ;; Cycle through all the other attributed blocks
  41.              (foreach block blocks
  42.                ;; If the distance is bueno ..
  43.                (if (<= (distance p (vlax-get block 'insertionpoint)) d)
  44.                  ;; Put the base block's value into the 'radius' block
  45.                  (mapcar '(lambda (x)
  46.                             (if (setq s (cdr (assoc (vla-get-tagstring x) tags)))
  47.                               (vla-put-textstring x s)
  48.                             )
  49.                           )
  50.                          (vlax-invoke block 'getattributes)
  51.                  )
  52.                )
  53.              )
  54.            )
  55.     )
  56.   )
  57.   ;; ssshhh
  58.   (princ)
  59. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

DraxJDM

  • Newt
  • Posts: 47
Re: copy attribute value to block's winthin a distance
« Reply #13 on: December 22, 2016, 01:45:20 PM »
thx Roy..

i get  .... ; error: bad argument type: VLA-OBJECT nil

i had a try with the same solution .. a copy of the first version you give to me


only i had the "setq tags" after the "VL-load-com" 
is there a reason why it has to be set after "foreach" ... 

ronjonp

  • Needs a day job
  • Posts: 7527
Re: copy attribute value to block's winthin a distance
« Reply #14 on: December 22, 2016, 01:54:06 PM »
Not sure what's causing the error .. I set it within the foreach because it is set on line 29 then redefined on line 31.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC