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.
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) )
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
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
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)
)
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
@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
@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
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
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 .
Dear pbejse,
Thanks for your lisp, it almost works fine.
But it fail with these block strangely. (see my attched dwg)
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)
@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
@ 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