Jump to content

select blocks inside another block by lisp


shokoufeh

Recommended Posts

Hi every one

I have some blocks named for example "AAA" inside another block, named for example "B" (nested blocks).
I have this lisp which is supposed to be able to select nested blocks, but it can not. It shows "No instances found".
would you please help me with that?

 

(defun c:selectNestedBlocks ()
  (setq ss (ssget "_X" '((2 . "AAA"))))  ; Selects all instances of the block "AAA" even if nested
  (if ss
    (princ (strcat "\n" (itoa (sslength ss)) " instances of 'AAA' selected."))
    (princ "\nNo instances found.")
  )
  (princ)
)

 

Link to comment
Share on other sites

See if this works for you...

 

(setq ss (ssget "_X" '((0 . "INSERT")(2 . "AAA"))))

 

 

Link to comment
Share on other sites

I misread which was the block and which was nested.  The above code will find blocks AAA, but not nested, you need to use B as the block searched for.

 

(setq ss (ssget "_X" '((0 . "INSERT")(2 . "B"))))

 

AFAIK, ssget doesn't do nested items.

Link to comment
Share on other sites

See if this with colours can help...

 

(defun c:changeblksinsblks-nests ( / blockcomp-nest blocks ccc col doc flatten lay lac lll lss lst )

  (or (not (vl-catch-all-error-p (vl-catch-all-apply (function vlax-get-acad-object) nil))) (vl-load-com))

  (defun blockcomp-nest ( blk / ent enx lst )
    (if (setq ent (tblobjname "block" blk))
      (while (setq ent (entnext ent))
        (if (= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
          (setq lst (vl-list* (blockcomp-nest (cdr (assoc 2 enx))) ent lst))
          (setq lst (cons ent lst))
        )
      )
    )
    (reverse lst)
  )

  (defun blocks ( / blk lst )
    (while (setq blk (tblnext "block" (not blk)))
      (setq lst (cons (cdr (assoc 2 blk)) lst))
    )
    (reverse lst)
  )

  (defun flatten ( l )
    (if (atom l)
      (list l)
      (append (flatten (car l)) (if (cdr l) (flatten (cdr l))))
    )
  )

  (setq lay "BLK"     ;; Target layer
        col acbylayer ;; Target colour
        doc (vla-get-activedocument (vlax-get-acad-object))
        ccc 0
  )
  (setq lst (blocks))
  (foreach itm lst
    (setq lll (cons (cons itm (blockcomp-nest itm)) lll))
  )
  (setq lll (vl-sort lll (function (lambda ( a b ) (> (length (cdr a)) (length (cdr b)))))))
  (foreach itm lll
    (setq ccc (1+ ccc))
    (setq lac (strcat lay (itoa ccc)))
    (if (not (vl-position lac lss))
      (progn
        (setq lss (cons lac lss))
        (if (not (tblsearch "layer" lac))
          (progn
            (vl-cmdf "_.-LAYER" "_M" lac "_C" ccc)
            (while (< 0 (getvar (quote cmdactive)))
              (vl-cmdf "")
            )
          )
        )
        (foreach e (flatten (cdr itm))
          (vla-put-color (vlax-ename->vla-object e) col)
          (vla-put-layer (vlax-ename->vla-object e) lac)
        )
      )
    )
  )
  (setvar (quote clayer) "0")
  (vla-regen doc acallviewports)
  (princ)
)

 

Regards, M.R.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...