Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Selection Set by block name

59 REPLIES 59
Reply
Message 1 of 60
spm5
9702 Views, 59 Replies

Selection Set by block name

Hello, 

I am trying to select all blocks of a certian name. I am using the following:

(setq ss1bk (ssget "_X" (list (cons 2 blkname))))

 

My issue is that this will not select any dynamic or attribute blocks that are not at the default insert state. 

Does anyone know how to select all of a block by name including reguardless of current state?

 

Thanks.

59 REPLIES 59
Message 2 of 60
_Tharwat
in reply to: spm5

check this out (untested) .

 

(if (setq objs (ssadd)
          ss   (ssget "_x" '((0 . "INSERT")))
    )
  (progn
    (repeat
      (setq i (sslength ss))
       (setq blk (vla-get-effectivename
                   (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                 )
       )
       (if (eq (strcat blkname) (strcat blk))
         (ssadd blk objs)
       )
    )
    (if objs (sssetfirst nil objs))
  )
  (princ)
)

 

Message 3 of 60
spm5
in reply to: spm5

Thanks, but it is returning:

; error: bad argument type: lentityp "name of block here"

 

Message 4 of 60
_Tharwat
in reply to: spm5

add this function to the top of codes (vl-load-com)

and change the variable blkname to your needed block name and try again

 

SORRY ,,, replace function strcat with strcase

 

Tharwat

Message 5 of 60
spm5
in reply to: spm5

Still no dice. I tried this:

(setq blkname (getstring))
(if (setq objs (ssadd)
          ss   (ssget "_x" '((0 . "INSERT")))
    )
  (progn
    (repeat
      (setq i (sslength ss))
       (setq blk (vla-get-effectivename
                   (vlax-ename->vla-object (ssname ss (setq i (1- i))))
                 )
       )
       (if (eq (strcase blkname) (strcase blk))
         (ssadd blk objs)
       )
    )
    (if objs (sssetfirst nil objs))
  )
  (princ)
)

 

I also tried manually typing the name of the block insted of setting the variable but same result.

I have (vl-load-com) autoloading, but I put it in manually just for good measure as well.

 

thanks

Message 6 of 60
_Tharwat
in reply to: spm5

Try it this way my spm

untested , because I am away from my laptop . and it would work for you .

 

(defun c:Test (/ blkname found objs ss i blk name)
  (vl-load-com)
  ;;; Tharwat 03. March. 2012 ;;;
  (if (and (setq blkname (getstring T "\n Enter name block :"))
           (setq found (tblsearch "BLOCK" blkname))
           (setq objs (ssadd)
                 ss   (ssget "_x" '((0 . "INSERT")))
           )
      )
    (progn
      (repeat
        (setq i (sslength ss))
         (setq name (vla-get-effectivename
                      (vlax-ename->vla-object
                        (setq blk (ssname ss (setq i (1- i))))
                      )
                    )
         )
         (if (eq (strcase blkname) (strcase name))
           (ssadd blk objs)
         )
      )
      (if objs
        (sssetfirst nil objs)
      )
    )
    (cond ((not blkname)
           (princ "\n Missed name of block ***")
          )
          ((not found)
           (princ "\n Block not found in drawing !!!")
          )
          (t
           (princ "\n couldn't find any block !!! ")
          )
    )

  )
  (princ)
)

 

Message 7 of 60
spm5
in reply to: spm5

works like a charm.

Thanks alot!

Message 8 of 60
_Tharwat
in reply to: spm5

You're welcome .

 

Glad to hear that it worked for you Smiley Happy

 

Tharwat

 

Message 9 of 60
miquan
in reply to: _Tharwat

Dear Tharwat,

 

Thanks for your lisp, it is very usefull.

But could you possibly help me to modify?

I need add option to get block name by select specified block in drawing. It means that we have 2 options:

1. Enter block name

2. Select block

 

The next modification is limiting the zone of block from entire drawing to selected zone by window. Similiarly we also 2 options:

1.Select block in entire drawing

2.Select block in window

 

Miquan

Message 10 of 60
_Tharwat
in reply to: miquan


@miquan wrote:

Dear Tharwat,

 

Thanks for your lisp, it is very usefull.

But could you possibly help me to modify?

I need add option to get block name by select specified block in drawing. It means that we have 2 options:

1. Enter block name

2. Select block

 

The next modification is limiting the zone of block from entire drawing to selected zone by window. Similiarly we also 2 options:

1.Select block in entire drawing

2.Select block in window

 

Miquan


Try this code and let me know how did you get on with it .

 

(defun c:GetBlocks (/ *error* SelectBlock EnterBlockName Bname s objs ss i)
  ;;--- Tharwat 09. May. 2013 ---;;
  (defun *error* (x) (princ "\n*Cancel*"))
  (defun SelectBlock (/ ss Bname)
    (if (setq ss (ssget "_+.:E:S:L" '((0 . "INSERT"))))
      (setq Bname (vla-get-effectivename
                    (vlax-ename->vla-object (ssname ss 0))
                  )
      )
    )
    Bname
  )
  (defun EnterBlockName (/ st)
    (while
      (and (/= "" (setq st (getstring t "\n Enter the Block Name :")))
           (not (tblsearch "BLOCK" st))
      )
       (princ
         "\n <!> Block Name is not found in drawing <!> < Try again > "
       )
    )
    st
  )
  (initget 0 "Enter Select")
  (setq s
         (cond ((getkword
                  (strcat "\n Chose one [Enter/Select] < Select >: ")
                )
               )
               ("Select")
         )
  )
  (cond ((eq s "Enter")
         (if (not (setq Bname (EnterBlockName)))
           (progn (princ "\n Miss typed the Block Name <!>") (exit))
         )
        )
        ((eq s "Select")
         (if (not (setq Bname (SelectBlock)))
           (progn (princ "\n Missed the Block <!>") (exit))
         )
        )
  )
  (initget 0 "Global Window")
  (setq s
         (cond ((getkword
                  (strcat "\n Chose one [Global/Window] < Global >: ")
                )
               )
               ("Global")
         )
  )
  (setq objs (ssadd))
  (cond
    ((eq s "Global")
     (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 Bname))))
       (repeat (setq i (sslength ss))
         (ssadd (ssname ss (setq i (1- i))) objs)
       )
     )
    )
    ((eq s "Window")
     (if (setq ss (ssget (list '(0 . "INSERT") (cons 2 Bname))))
       (repeat (setq i (sslength ss))
         (ssadd (ssname ss (setq i (1- i))) objs)
       )
     )
    )
  )
  (if (> (sslength objs) 0)
    (sssetfirst nil objs)
  )
  (princ "\n Written by Tharwat Al Shoufi")
  (princ)
)

(vl-load-com)

 Tharwat

Message 11 of 60
pbejse
in reply to: miquan


@miquan wrote:

I need add option to get block name by select specified block in drawing. It means that we have 2 options:

1. Enter block name

2. Select block

 

The next modification is limiting the zone of block from entire drawing to selected zone by window. Similiarly we also 2 options:

1.Select block in entire drawing

2.Select block in window

 

Miquan


Another :

 

(defun c:SelBlock (/ blk s bn selection opt)
(vl-load-com)
;;;	pBe 09May2013	;;;
  (while (not
      (progn
        (initget "N")
        (setq blk (entsel "\nSelect Block/N for Block name:"))
        (cond
          ((null blk) (prompt "\n<<Null selection>>"))
          ((eq blk "N")
           (while (not
                    (and (Setq bn (getstring "\nEnter Block Name: "))
                         (tblsearch "Block" bn)
                    )
                  )
             (princ (Strcat "\n<< " bn " Invalid block name>>"))
             
           ) T
          )
          ((setq s (car blk))
           (if (eq (cdr (Assoc 0 (entget s))) "INSERT")
             (setq bn (vla-get-EffectiveName (vlax-ename->vla-object s)))
             (prompt "\n<<Invalid Object>>")
           )
          )
        )
      )
    )
  )
  (princ (Strcat "\nBlock Name Filter: " (Strcase BN)))
  (initget  "A W")
  (setq opt (cond ((getkword
                     (strcat "\n Choose option [All/Window] <All>: ")
                   )
                  )
                  ("A")
            )
  )
  (setq selection
         (ssget (if (eq opt "A") "_X" "_:L")
                (list '(0 . "insert")
                      (cons 2 (strcat bn ",`*U*"))
                                      )
         )
  )
  (sssetfirst nil selection)
  (princ)
)

 HTH

Message 12 of 60
miquan
in reply to: _Tharwat

Dear Tharwat,

 

Thansk for your quick reply.

 

I tried your lisp, but it doesn't work properly.

I tried all option but result is nothing which is select.

 

In the option select block by window, I get this result:

" ...

Chose one [Global/Window] < Global >: w
Select objects: Specify opposite corner: 0 found"

 

Miquan

Message 13 of 60
_Tharwat
in reply to: miquan

The code works very well here .

 

Global = without user selction .

Window = you should be selecting by window selection and the block name must be included within that

window selection to let the code highlight them for you , otherwise nothing would be selected .

 

Chek well the name of the block that you enter .

 

Try again and lete me know .

Message 14 of 60
miquan
in reply to: pbejse

Dear pbejse,

 

Thanks for your lisp, it almost works fine.

But it fail with these block strangely. (see my attched dwg)

 

Miquan

Message 15 of 60
miquan
in reply to: _Tharwat

Dear tharwat,

 

Please try your lisp in my attached file recently.

I tried again, but it failed.

 

Miquan

Message 16 of 60
alanjt_
in reply to: miquan

Message 17 of 60
miquan
in reply to: alanjt_

Dear alanjt,

 

I can see this thread, because I don't have account.

Message 18 of 60
_Tharwat
in reply to: miquan


@miquan wrote:

Dear tharwat,

 

Please try your lisp in my attached file recently.

I tried again, but it failed.

 

Miquan


Try it now .

 

(defun c:GetBlocks (/         *error*   SelectBlock
                    EnterBlockName      Bname     s         gw
                    objs      ss        sn        i
                   )
  ;;--- Tharwat 09. May. 2013 ---;;
  (defun *error* (x) (princ "\n*Cancel*"))
  (defun SelectBlock (/ ss Bname)
    (if (setq ss (ssget "_+.:E:S:L" '((0 . "INSERT"))))
      (setq Bname (vla-get-effectivename
                    (vlax-ename->vla-object (ssname ss 0))
                  )
      )
    )
    Bname
  )
  (defun EnterBlockName (/ st)
    (while
      (and (/= "" (setq st (getstring t "\n Enter the Block Name :")))
           (not (tblsearch "BLOCK" st))
      )
       (princ
         "\n <!> Block Name is not found in drawing <!> < Try again > "
       )
    )
    st
  )
  (initget 0 "Enter Select")
  (setq s
         (cond ((getkword
                  (strcat "\n Chose one [Enter/Select] < Select >: ")
                )
               )
               ("Select")
         )
  )
  (cond ((eq s "Enter")
         (if (not (setq Bname (EnterBlockName)))
           (progn (princ "\n Miss typed the Block Name <!>") (exit))
         )
        )
        ((eq s "Select")
         (if (not (setq Bname (SelectBlock)))
           (progn (princ "\n Missed the Block <!>") (exit))
         )
        )
  )
  (initget 0 "Global Window")
  (setq gw
         (cond ((getkword
                  (strcat "\n Chose one [Global/Window] < Global >: ")
                )
               )
               ("Global")
         )
  )
  (setq objs (ssadd))
  (cond
    ((eq gw "Global")
     (if (setq ss (ssget "_X" (list '(0 . "INSERT"))))
       (repeat (setq i (sslength ss))
         (if (eq (vla-get-EffectiveName
                   (vlax-ename->vla-object
                     (setq sn (ssname ss (setq i (1- i))))
                   )
                 )
                 Bname
             )
           (ssadd sn objs)
         )
       )
     )
    )
    ((eq gw "Window")
     (if (setq ss (ssget (list '(0 . "INSERT"))))
       (repeat (setq i (sslength ss))
         (if (eq (vla-get-EffectiveName
                   (vlax-ename->vla-object
                     (setq sn (ssname ss (setq i (1- i))))
                   )
                 )
                 Bname
             )
           (ssadd sn objs)
         )
       )
     )
    )
  )
  (if (> (sslength objs) 0)
    (sssetfirst nil objs)
  )
  (princ "\n Written by Tharwat Al Shoufi")
  (princ)
)

(vl-load-com)

 

Message 19 of 60
pbejse
in reply to: miquan


@miquan wrote:

Dear pbejse,

 

Thanks for your lisp, it almost works fine.

But it fail with these block strangely. (see my attched dwg)

 

Miquan


I was expecting you to notice that, as you are using this on Dynamic blocks

 

(defun c:SelBlock (/ blk s bn selection opt)
(vl-load-com)
;;;	pBe 09May2013	;;;
  (while (not
      (progn
        (initget "N")
        (setq blk (entsel "\nSelect Block/N for Block name:"))
        (cond
          ((null blk) (prompt "\n<<Null selection>>"))
          ((eq blk "N")
           (while (not
                    (and (Setq bn (getstring "\nEnter Block Name: "))
                         (tblsearch "Block" bn)
                    )
                  )
             (princ (Strcat "\n<< " bn " Invalid block name>>"))
             
           ) T
          )
          ((setq s (car blk))
           (if (eq (cdr (Assoc 0 (entget s))) "INSERT")
             (setq bn (vla-get-EffectiveName (vlax-ename->vla-object s)))
             (prompt "\n<<Invalid Object>>")
           )
          )
        )
      )
    )
  )
  (princ (Strcat "\nBlock Name Filter: " (Strcase BN)))
  (initget  "A W")
  (setq opt (cond ((getkword
                     (strcat "\n Choose option [All/Window] <All>: ")
                   )
                  )
                  ("A")
            )
  )
  (if (setq selection
         (ssget (if (eq opt "A") "_X" "_:L")
                (list '(0 . "insert")
                      (cons 2 (strcat bn ",`*U*"))
                                      )
         ) notname selection
      )
;;; do your thing here  ;;;    
    (repeat (setq i (sslength  notname))
      	(if (not (eq (Strcase
              (vla-get-effectivename
                (vlax-ename->vla-object (setq e (ssname notname (setq i (1- i)))))
              )
            )
          (strcase bn)))
          (ssdel e selection)
        )
      )
    )
;;; this is just to highlight the selection ;;;
  (sssetfirst nil selection)
  (princ)
)

 

Never thought you will use this for just selecting the object alone <ssx variation for Dblocks>  thats why i only include sssetfirst.. Anyhoo. try the modified code. This should work as to your intention.

 

Now if you need to do something with the blocks selected you can add "stuff" inside the repeat loop.

 

Cheers

 

 

Message 20 of 60
pbejse
in reply to: _Tharwat

@ tharwat.

 

Perhaps it would be better to include (cons 2 (strcat bname ",`*U*"))) on your ssget filter to reduce the number of  blocks for iteration on "global mode" and "Window" mode.

 

Better yet combine the two options like i had in mine.

 

Cheers Tharwat

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost