lisp run command only on specified filenames

lisp run command only on specified filenames

giampaolo.schembri
Participant Participant
739 Views
7 Replies
Message 1 of 8

lisp run command only on specified filenames

giampaolo.schembri
Participant
Participant

hello,

i'm tryng to make a lisp that do a command only if the file name start with specified caracter

but i can't find the right way
any suggestion?

 

here's my code

(defun c:test1()
	(if ((wcmatch (getvar "DWGNAME") "CH*.dwg" "C1*.dwg" "C2*.dwg" "LI*.dwg" "MN*.dwg" "ON*.dwg" "NO*.DWG" "RJ*.dwg" "SH*.dwg" "WT*.dwg" ))  	;;if file name start with
																																				;; change blcok A / CW
	(command "_-insert" "C:\\dati\\block_update\\blocks\\lin_acquatorre.dwg" "-50,0" "" "" "" "" "" "" "" "" "")								;;insert block
	(entdel (entlast))

	(if (setq ss (ssget "_X" '((2 . "riduzione")))) 																							;block to be replaced
		(repeat (setq n (sslength ss))
		  (setq edata (entget (ssname ss (setq n (1- n)))))
		  (entmod (subst '(2 . "lin_acquatorre") '(2 . "lin_cooling-water") edata)) 
		)

	)
)
0 Likes
Accepted solutions (1)
740 Views
7 Replies
  • Lisp
Replies (7)
Message 2 of 8

pbejse
Mentor
Mentor
Accepted solution

@giampaolo.schembri wrote:

hello,

i'm tryng to make a lisp that do a command only if the file name start with specified caracter

but i can't find the right way
any suggestion?


(defun c:test1 ( / fn ss edata)
  (if
    (and
    	(Setq fn (findfile  "C:\\dati\\block_update\\blocks\\lin_acquatorre.dwg"))  
	    (Vl-some '(lambda (nme)				
			(if (wcmatch (strcase (getvar "DWGNAME"))
				     (strcat nme "*.DWG"))			;<--  if file name start with
			  nme
			)
		      )
		     '("CH" "C1" "C2" "LI" "MN"	"ON" "NO" "RJ" "SH" "WT")
	    )
	)
	(progn									;<-- change blcok A / CW
	  (command "_-insert" fn "-50,0" "" "" "" "" "" "" "" "" "")		;<-- insert block
	  (entdel (entlast))
		(if (setq ss (ssget "_X" '((2 . "riduzione"))))			;<-- block to be replaced
			(repeat (setq n (sslength ss))
			  (setq edata (entget (ssname ss (setq n (1- n)))))
			  (entmod (subst '(2 . "lin_acquatorre") '(2 . "lin_cooling-water") edata)) 
			)
		)
	)
    )
  )

 

HTH

0 Likes
Message 3 of 8

giampaolo.schembri
Participant
Participant

thanks!

it's fro 2 days that i'm going crazy with that problem

0 Likes
Message 4 of 8

pbejse
Mentor
Mentor

@giampaolo.schembri wrote:

thanks!

it's fro 2 days that i'm going crazy with that problem


Glad it works for you. And that is just one of many.

Another way is

(member (strcase (substr (getvar "DWGNAME") 1 2))
		     '("CH" "C1" "C2" "LI" "MN" "ON" "NO" "RJ" "SH" "WT")
	     )

HTH

 

0 Likes
Message 5 of 8

giampaolo.schembri
Participant
Participant

in this way i have to add my command after the file name definition, right?

(member (strcase (substr (getvar "DWGNAME") 1 2))
		     '("CH" "C1" "C2" "LI" "MN" "ON" "NO" "RJ" "SH" "WT")
command: xxxxx	     )

 1 2 check the first 2 caracters? if i write "1 2 3" i can match 3 caracters

i correctly understand?

0 Likes
Message 6 of 8

pbejse
Mentor
Mentor

@giampaolo.schembri wrote:

in this way i have to add my command after the file name definition, right?..


Yes, after the IF statement

 

	(if
	  (member (strcase (substr (getvar "DWGNAME") 1 2))
		  '("CH" "C1" "C2" "LI" "MN" "ON" "NO" "RJ" "SH" "WT")
	  		) ;_ end of member
	   (princ "do all this")		;<-- Then
	   (princ "Found no match in filename")	;<-- Else
			) ;_ end of if

 

 


@giampaolo.schembri wrote:

.. 1 2 check the first 2 caracters? if i write "1 2 3" i can match 3 caracters

i correctly understand?


Not quite

--> (substr <String argument> <start postion> <length>)

 

(substr "Chemical" 1 2) will give "Ch", start at position 1 "C" and give 2 characters "Ch"

If you want 3 characters

(substr "Chemical" 1 3) will give "Ch", start at position 1 "C" and give 3 characters "Che"

Another example

(substr "01234-Chemical" 7 2) will give "Ch", start at position 7 "C" and give 2 characters "Ch"

 

As test for you:

So if i have "This day is All good" and i want just "All good", what is the correct syntax?

 

 

0 Likes
Message 7 of 8

giampaolo.schembri
Participant
Participant

ok I understand

(substr "This day is All good" 13 14 15 16 17 18 19 20)

in this way check "all good", right?

 

0 Likes
Message 8 of 8

pbejse
Mentor
Mentor

@giampaolo.schembri wrote:

ok I understand

(substr "This day is All good" 13 14 15 16 17 18 19 20)

in this way check "all good", right?

 


Not quite there yet, did you click on the link my previous post? 

-- If length is not specified, the substring continues to the end of str. ---

(substr "This day is All good" 13)

starts at position 13 and gives out the remaining string regardless how long it is.

 

Good try though. 🙂

 

Now how do it test for this

"This day is All good" --- > "ALL"

 

Also keep in mind that the these functions  member | eq | equal  | wmatch | vl-string-search are all case sensitive.

 

0 Likes