Connect with polyline blocks with equal attribute names.

Connect with polyline blocks with equal attribute names.

Dan-Rod
Advocate Advocate
765 Views
9 Replies
Message 1 of 10

Connect with polyline blocks with equal attribute names.

Dan-Rod
Advocate
Advocate

hi, I have a drawing with blocks with the same name but with different attributes and I need to join with a line or polyline the blocks in which the attribute is the same annex file as an example I hope you can help me.

0 Likes
Accepted solutions (1)
766 Views
9 Replies
Replies (9)
Message 2 of 10

marko_ribar
Advisor
Advisor

Quickly written...

 

(defun c:connsameblkpairs ( / midpt ss i bl atts bll x a b lil )

  (vl-load-com)

  (defun midpt ( e )
    (vla-getboundingbox (vlax-ename->vla-object e) 'll 'ur)
    (mapcar 'set '(ll ur) (mapcar 'safearray-value (list ll ur)))
    (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) ll ur)
  )

  (prompt "\nSelect blocks with attributes...")
  (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
    (progn
      (repeat (setq i (sslength ss))
        (setq bl (ssname ss (setq i (1- i))))
        (setq atts (vlax-invoke (vlax-ename->vla-object bl) 'getattributes))
        (foreach att atts
          (if (= (vla-get-tagstring att) "ID/MFG")
            (setq bll (cons (list bl (vla-get-textstring att)) bll))
          )
        )
      )
      (foreach bl bll
        (setq x (vl-remove-if-not '(lambda ( x ) (equal (cadr x) (cadr bl))) (vl-remove bl bll)))
        (foreach q x
          (setq a (midpt (car bl)))
          (setq b (midpt (car q)))
          (if (not (or (vl-position (list a b) lil) (vl-position (list b a) lil)))
            (setq lil (cons (list a b) lil))
          )
        )
      )
      (foreach li lil
        (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
      )
    )
  )
  (princ)
)

HTH. M.R.

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 10

Dan-Rod
Advocate
Advocate

is what I'm looking for, but would there be a way that it would work for any block? In other words, it could work for blocks with other names, and attributes I see that the lisp is specified with the name of the attribute, which could adapt to any group of blocks that have the same attributes. I attach another example

0 Likes
Message 4 of 10

marko_ribar
Advisor
Advisor
Accepted solution

Sure...

 

(defun c:connsameblkpairs ( / midpt match ss i bl atts attl bll x a b lil )

  (vl-load-com)

  (defun midpt ( e )
    (vla-getboundingbox (vlax-ename->vla-object e) 'll 'ur)
    (mapcar 'set '(ll ur) (mapcar 'safearray-value (list ll ur)))
    (mapcar '(lambda ( a b ) (/ (+ a b) 2.0)) ll ur)
  )

  (defun match ( attl1 attl2 )
    (vl-every '(lambda ( x ) (vl-some '(lambda ( y ) (equal x y)) attl2)) attl1)
  )

  (prompt "\nSelect blocks with attributes...")
  (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
    (progn
      (repeat (setq i (sslength ss))
        (setq bl (ssname ss (setq i (1- i))))
        (setq atts (vlax-invoke (vlax-ename->vla-object bl) 'getattributes))
        (foreach att atts
          (setq attl (cons (list (vla-get-tagstring att) (vla-get-textstring att)) attl))
        )
        (setq bll (cons (list bl attl) bll))
        (setq attl nil)
      )
      (foreach bl bll
        (setq x (vl-remove-if-not '(lambda ( x ) (match (cadr x) (cadr bl))) (vl-remove bl bll)))
        (foreach q x
          (setq a (midpt (car bl)))
          (setq b (midpt (car q)))
          (if (not (or (vl-position (list a b) lil) (vl-position (list b a) lil)))
            (setq lil (cons (list a b) lil))
          )
        )
      )
      (foreach li lil
        (entmake (list '(0 . "LINE") (cons 10 (car li)) (cons 11 (cadr li))))
      )
    )
  )
  (princ)
)

HTH. M.R.

 

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 5 of 10

Dan-Rod
Advocate
Advocate

thank you very much, is what i needed

0 Likes
Message 6 of 10

Dan-Rod
Advocate
Advocate

Finally, when I run it, I see that the code takes into account all the attributes, it is possible that when running the command it will ask which attribute is the one that you want to link, and if it could also work for blocks that are nested within other blocks since I have to explode when nested.

 

I thank you again for the support and the prompt response

0 Likes
Message 7 of 10

pbejse
Mentor
Mentor

@Dan-Rod wrote:

... and if it could also work for blocks that are nested within other blocks since I have to explode when nested.


Not clear on what you mean by that ?  exploding the nested blocks eventually?   or explode only because there's a match found inside the block?

 

 

0 Likes
Message 8 of 10

Dan-Rod
Advocate
Advocate

sorry, I'm still practicing my English.

what I mean is that the command does not work with blocks that are nested, they have to be exploited until the block that I need to bind is in model space and not inside the block editor.

I hope I have made myself understood.

0 Likes
Message 9 of 10

Leo_Gambini
Contributor
Contributor

Hi Guys

 

I come to ask for an adaptation in a table, I will post the images and attachment to better understand,

I'm grateful for some help.

As this is important to me, I will make a donation of 20 U$ for those who can, as thanks for the help.

Seeyha

0 Likes
Message 10 of 10

Sea-Haven
Mentor
Mentor

Leo You should have started a new post for this request. Dont know how to ask Admin to move to new post.

0 Likes