Jump to content

Lisp to Run a command Automatically when opened the file


acad1985

Recommended Posts

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.?

Link to comment
Share on other sites

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • acad1985

    7

  • rlx

    6

  • Tharwat

    5

  • BIGAL

    2

Top Posters In This Topic

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...