Search for Blocks with special signs in name

Search for Blocks with special signs in name

info2ERHX
Contributor Contributor
902 Views
5 Replies
Message 1 of 6

Search for Blocks with special signs in name

info2ERHX
Contributor
Contributor

Hello everyone,

i'm new in this group, either, very new in AutoLisp 🙂

For you for sure simple thing - this simple (i guess:) lisp have to search for blocks in drawing and change Layer according to block name.

Worked fine till blocks with underbars and minus came. I needed to change block name and Layer in code.

Can you please help me modify lisp, to make search for stuff like 'simple_block' and 'simple-block' possible ?

Block's name have to stay unchanged !

Thanks !

Martin.

 

Code :

(defun c:demo (/ cly ent i lay ss)

  (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "simple_block") '(66 . 1))))

    (progn

      (setq cly (getvar 'CLAYER))

      (command "_.layer" "_U" "BS" "_T" "BS" "_M" "BS" "")

      (repeat (setq i (sslength sS))

        (setq ent (entget (ssname ss (setq i (1- i))))

              lay (cdr (assoc 8 ent))

        )

        (if (/= lay "BS")

          (entmod (subst (cons 8 "BS") (assoc 8 ent) ent))

        )

      )

      (setvar 'CLAYER cly)

    )

    (prompt "\n \"simple_block\" block not found! ")

  )

  (princ)

)

 

 

0 Likes
903 Views
5 Replies
Replies (5)
Message 2 of 6

cadffm
Consultant
Consultant
Accepted solution

First i wanted to write:

 

Read the help about WILDCARDS [F1]

http://help.autodesk.com/view/ACD/2021/ENU/?guid=GUID-E8328DF6-43FF-47AA-8524-72B962A9D552

 

then i wanted to write about SNVALID [F1]

http://help.autodesk.com/view/ACD/2021/ENU/?guid=GUID-2EFBE198-9860-456B-A090-206C743ACB90

 

at last i read your code..

 

"_" and "-" is not a problem. Whatever you problem is, it's not a Blockname with "_" or "-"

 

Are you sure your block references have Attribures?

You filtering blockreference with Attributes ONLY.

 

WITH '(66 . 1)

WITHOUT '(66 . 0)

ALL '(66 . 1)

 

 

Try your code again with

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

 

or better:

 

(setq ss (ssget "_X" '((0 . "INSERT") (2 . "simple_block")(8 . "~BS"))))

 

Then you don't need to check the Layer: (if (/= lay "BS")

 

and one hint more:

(setq cly (getvar 'CLAYER))

(setvar 'CLAYER cly)

 

both lines are useless because you doesnt change the CLAYER beteen (in your Demo)

Sebastian

Message 3 of 6

pbejse
Mentor
Mentor
Accepted solution

@info2ERHX wrote:

 

Worked fine till blocks with underbars and minus came. I needed to change block name and Layer in code.

Can you please help me modify lisp, to make search for stuff like 'simple_block' and 'simple-block' possible ?


First of all, Welcome to the forum

 

Ssget filter accepts wild card matching

simple?block will match simple-block and simple_block or  simple block, it matches one character between simple and block.

 

 

(setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "simple?block") '(66 . 1))))

 

 

Try it

 

BTW: start fixing your block names 🙂

Message 4 of 6

braudpat
Mentor
Mentor
Accepted solution

Hello

 

I am using often this routine "SelBlocks" from BeekeeCZ

 

To select many different block with the beginning of the name ...

 

Maybe "SelBlocks" could help you !?

 

THE HEALTH, Regards, Patrice (The Retired Old French EE Froggy)

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


Message 5 of 6

cadffm
Consultant
Consultant
Accepted solution

After reading it again, my first thought was the right one, but part of the rest are also useful

but it needed some changes <sorry>

 


 

Read the help about WILDCARDS [F1]

http://help.autodesk.com/view/ACD/2021/ENU/?guid=GUID-E8328DF6-43FF-47AA-8524-72B962A9D552

 

Or better on this page: http://help.autodesk.com/view/ACD/2021/ENU/?guid=GUID-EC257AF7-72D4-4B38-99B6-9B09952A53AD

Try your code again with

 

(setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "simple[_-]block")'(66 . 1))))

 

or better:

 

(setq ss (ssget "_X" '((0 . "INSERT") (2 . "simple?block")(8 . "~BS")(66 . 1))))

 

Then you don't need to check the Layer: (if (/= lay "BS")

 

and one hint more:

(setq cly (getvar 'CLAYER))

(setvar 'CLAYER cly)

 

both lines are useless because you won't change the CLAYER between (in your Demo)

Use LAYER command option NEW instead of MAKE !


 

 

 

(defun c:demoA (/ cly ent i lay ss)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") '(2 . "simple[_-]block")'(8 . "~BS")'(66 . 0))))
    (progn
      ;;;(setq cly (getvar 'CLAYER))
      (command "_.layer" "_U" "BS" "_T" "BS" "_NEW" "BS" "") ;<-
      (repeat (setq i (sslength sS))
        (setq ent (entget (ssname ss (setq i (1- i))))
              lay (cdr (assoc 8 ent))
        )
        ;;;(if (/= lay "BS")
          (entmod (subst (cons 8 "BS") (assoc 8 ent) ent))
        ;;;)
           (princ "\nDone")
      )
      ;;;(setvar 'CLAYER cly)
    )
    (prompt "\n \"No simple_block&simple-block\" block not found on wrong layers! ")
  )
 (princ)
)



(defun c:demoB (/ cly ent i lay ss)
  (if (setq ss (ssget "_X" '((0 . "INSERT")(2 . "simple[_-]block")(8 . "~BS")(66 . 1))))
    (progn
      (command "_.layer" "_U" "BS" "_T" "BS" "_N" "BS" "") ;<-
      (command "_.CHPROP" ss "" "_LAYER" "BS" "")
      (princ "\nDone")
    )
    (prompt "\n \"No simple_block&simple-block\" block not found on wrong layers! ")
  )
 (princ)
)

 

 

 

R

Sebastian

0 Likes
Message 6 of 6

pbejse
Mentor
Mentor
Accepted solution

Another

The variable blockAndLayer are your data. that way you can use this for any block name and target layer. 

In this example, any of the blockname found under that list, the program will assing the corresponding layer

(("SIMPLE_BLOCK" "SIMPLE-BLOCK" "SIMPLE BLOCK") "ThisLayer" )
(("ANOTHER_BLOCK" "ANOTHER BLOCK" "EVEN A DIFFRENT ONE") "ThisOtherLayer")

You can add as many block names and layer you need.

sor of a catch all thingy.

(defun c:demo (/ blockAndLayer ent bn ThisLAyer)
  (setq	blockAndLayer
	 '((("SIMPLE_BLOCK" "SIMPLE-BLOCK" "SIMPLE BLOCK") "ThisLayer" )
	   (("ANOTHER_BLOCK" "ANOTHER BLOCK" "EVEN A DIFFRENT ONE") "ThisOtherLayer")
	  )
  )
  (if (setq ss (ssget "X" '((0 . "INSERT") (66 . 1))))
    (repeat (setq i (sslength sS))
      (setq ent	(entget (ssname ss (setq i (1- i))))
	    bn	(strcase (cdr (assoc 2 ent)))
      )
      (if (setq	Thislayer
		 (vl-some '(lambda (bm)
			     (if (member bn (car bm))
			       (cadr bm)
			     )
			   )
			  blockAndLayer
		 )
	  )
	(entmod (subst (cons 8 Thislayer) (assoc 8 ent) ent))
      )
    )

  )
  (princ)
)

 

HTH

 

0 Likes