Jump to content
acad1985

Lisp to Run a command Automatically when opened the file

Recommended Posts

acad1985

Hi All

I have a bunch of files in a folder, I have to do the purging process on several files.

I have a lisp file to open the file automatically,

So my idea is, after open the file, if the file name having 7 digits then the purge command should run automatically, if the file name less/greater than 7 digits then command should not run. can we do through lisp if possible.?

Share this post


Link to post
Share on other sites
Tharwat

Hi,

 

Create a acaddoc.lsp file if its not already existed into your AutoCAD support folder then add your codes into the fore-said file then within your codes you would need to have a few codes checking for the file name then do the job if your criteria matched.

Share this post


Link to post
Share on other sites
acad1985

Hi Tharwat

Thanks for your reply, If i add my code into acaddoc.lsp, this will run all the files whenever open my AutoCAD, but need if the file name having seven digits (ex. 1234_A3) then only purge command should run automatically.

Share this post


Link to post
Share on other sites
BIGAL

A simple check (getvar "dwgname") and have a look at is least 7 long does it have some pattern

 

(getvar 'dwgname)
strlen is it 11
subtract .dwg of end dont forget the 4 end characters .dwg
substr in a loop 7 times check each character is it a number
asscii 47-59 is 0-9 
Drawing1.dwg is 12 long so Drawing1 is 8 long

Share this post


Link to post
Share on other sites
acad1985

Hey Bigal

Can you please give me the code.. I'm not a expert in lisp. But I'm interested to learn. So please help me.

Share this post


Link to post
Share on other sites
rlx
Posted (edited)

this should help you on your way :

 

(defun c:tst ( / dwgn letters numbers)
 (vl-load-com)
 ; dwgname without path and extension
 (setq dwgn (vl-filename-base (getvar 'dwgname)))
 (princ (strcat "\nDrawing name is : " dwgn))
 (princ (strcat "\nDrawing name has " (itoa (strlen dwgn)) " characters"))
 ; Total number of letters in filename
 (if (> (setq letters (length (vl-remove-if '(lambda(x)(and (< x 58)(> x 47)))(vl-string->list dwgn)))) 0)
   (princ (strcat "\nTotal of letters in drawing name : " (itoa letters)))
   (princ "\nNo letters in dwgname")
 )
 ; total number of numbers in filename
 (if (> (setq numbers (length (vl-remove-if '(lambda(x)(or (< x 48)(> x 57)))(vl-string->list dwgn)))) 0)
   (princ (strcat "\nTotal of numbers in drawing name : " (itoa numbers)))
   (princ "\nNo numbers in dwgname")
 )
 (princ)
)

; a few tiny lisp functions for more general use :

; remove letters from string (ripcar "123abc456def") -> "123456"
(defun ripcar (s)
 (vl-list->string (vl-remove-if '(lambda(x)(or (< x 48)(> x 57)))(vl-string->list s))))
; remove numbers from string (ripnum "123abc456def") -> "abcdef"
(defun ripnum (s)
 (vl-list->string (vl-remove-if '(lambda(x)(and (< x 58)(> x 47)))(vl-string->list s))))

gr. Rlx

Edited by rlx

Share this post


Link to post
Share on other sites
BIGAL

Thanks rlx, nice descriptions, the question could cause other problems like purging a dwg you did not mean to. So acad1985 make sure you have a continue yes or no at start and for your info dcl supports a Ok Cancel button only, making a nice screen input rather than typing Y N etc. Which has prompted me to add to my library.

Share this post


Link to post
Share on other sites
Tharwat
if the file name having seven digits (ex. 1234_A3)

A is a letter and not a digit.

Can you explain your different file names in order to know how to check and run a program when a certain criteria meets.

Share this post


Link to post
Share on other sites
Tharwat

Hi rlx, :)

(length nil) ;; = 0

Share this post


Link to post
Share on other sites
rlx
Hi rlx, :)

(length nil) ;; = 0

 

oh darn , I realized that right after I posted but hoped nobody would notice , but there's no fooling you is there ...haha

Share this post


Link to post
Share on other sites
rlx
A is a letter and not a digit.

Can you explain your different file names in order to know how to check and run a program when a certain criteria meets.

 

realized that too , that's why I did all three possibillity's , length , numbers and letters :-)

Share this post


Link to post
Share on other sites
Tharwat

Won't this be enough? :)

(wcmatch "My drawing12345678 YAY.dwg" "*########*")

Share this post


Link to post
Share on other sites
Grrr
Won't this be enough? :)

(wcmatch "My drawing12345678 YAY.dwg" "*########*")

 

OP didn't mentioned if the digits are consecutive. ;)

and..

(wcmatch "My drawing12345678 YAY1234567890.dwg" "*########*")

 

Perhaps OP must provide sample names for the dwg files.

Share this post


Link to post
Share on other sites
acad1985

Hey Tharwat

A is a letter and not a digit.

Can you explain your different file names in order to know how to check and run a program when a certain criteria meets.

 

You are correct , there is a combination of Digits and Letters, So i will use only digits.

So that file Name will be like this 1234567.dwg,7896541.dwg,9632587.dwg

 

@ rlx, Thank you so much for your code, but your code only shows the file name, can you please tell me how to run purge command automatically using your code.

 

 

Thanks Again

Share this post


Link to post
Share on other sites
rlx

If your filename is exactly 7 numbers it would not have to be more difficult than :

 

 (if (wcmacth (vl-filename-base (getvar 'dwgname)) "#######")(vla-PurgeAll (vla-get-ActiveDocument (vlax-get-acad-object)))) 

 

(ps typed it directly so sorry if I made a typo...)

Share this post


Link to post
Share on other sites
Tharwat

Hi,

 

Add the following into your acaddoc.lsp file.

 

(defun PurgeMe (/ FileName)
 (and (setq FileName (vl-filename-base (getvar 'DWGNAME)))
      (vl-every '(lambda (u) (< 47 u 58)) (vl-string->list FileName))
      (vla-purgeall (vla-get-activedocument (vlax-get-acad-object)))
      )
 (princ)
 )
;; Auto-run on start up of drawing names that their names are consist of digits only.
(PurgeMe)

Share this post


Link to post
Share on other sites
acad1985

Hey Tharwat

Thanks for your code, This working but it's always working even file name having digits (it's working file name as 1,2,3,4,5,6,7,8,9...etc digits)

But i need it should be run only file name having 7 digits.

Also please tell me, if i want to use another command or Lisp command instead of purge, then where i have to change in this lisp.

 

Thanks again.

Share this post


Link to post
Share on other sites
Grrr

(wcmatch (strcase ...) "#######.DWG")

Share this post


Link to post
Share on other sites
rlx

think there is a little confusion about the filename structure. Either it IS 7 numbers (digits whatever) , and nothing more , or , it also can have letters but it contains 7 numbers , either next to each other of not. Anyway , if the total filename is 7 numbers in a row , wcmatch option will do. If its multi-culty the only way to be sure is to rip the characters out and measure the corpse. So :

 

 (if (= (strlen (ripcar (vl-filename-base (getvar 'dwgname)))) 7) (progn ... purge...surge...merge whatever... )) 

 

gr. Rlx

Share this post


Link to post
Share on other sites
acad1985

Hey Rlx

I have replaced your code with the code provided by Tharwat, but it's not working can you please look what i have made wrong..

(defun PurgeMe (/ FileName)
(if (= (strlen (ripcar (vl-filename-base (getvar 'dwgname)))) 7) 
(progn ... purge...surge...merge whatever... ))
      )
 (princ)
 )
;; Auto-run on start up of drawing names that their names are consist of digits only.
(PurgeMe)

 

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×