Jump to content
acad1985

Lisp to Saveas and Open the file Automatically

Recommended Posts

acad1985

Hello Guys

I'm trying to create a routine Lisp to Saveas the DWG file open it the same file again. but i don't know how to open the file through LISP.

I want to save as and open the file Automatically.

Please anyone Help me to do this.

 

 

Thanks in Advance.

Share this post


Link to post
Share on other sites
Cad64

Your request is confusing. If you do a SaveAs, you can't open the file because it's already open. You just saved it with a different name.

Share this post


Link to post
Share on other sites
acad1985

Hey Thanks.

Actually i have to save as the file with different name then i need to open.

Share this post


Link to post
Share on other sites
Cad64

This is why I'm confused. The SaveAs command, saves your current drawing to a new location and/or a new file name. Once the file has been saved, your current drawing is now the new file that you just saved. So you don't need to open it because it's already open.

 

Maybe what you need to do is save the file to a different name and then open the previous file from before the SaveAs?

Share this post


Link to post
Share on other sites
BIGAL

Like cad64 you write a new script file every time get current dwg name and put that as "open" last line, just use lisp to write script file and call as last line (command "script" "saveasy")

 

Roughly script is

saveas olddwg2 close
open olddwg 

Share this post


Link to post
Share on other sites
acad1985

Actually I'm using GIS software Which is worked on AutoCAD platform. When i do this manually. i need to saveas the file from to a folder with different name. At that time current file name will not changed. So again i need to open the file from folder.

Share this post


Link to post
Share on other sites
Grrr
(defun C:ReOpenSave_On nil
 (foreach rtr (cdar (vlr-reactors :VLR-Editor-reactor)) (if (= "ReOpenSave" (vlr-data rtr)) (vlr-remove rtr)) )
 (vlr-set-Notification (vlr-Editor-reactor "ReOpenSave" '((:VLR-SaveComplete . ReOpenSave:CB) (:VLR-BeginSave . ReOpenSave:CB))) 'all-documents)
 (princ)
)
(defun C:ReOpenSave_Off nil
 (foreach rtr (cdar (vlr-reactors :VLR-Editor-reactor)) (if (= "ReOpenSave" (vlr-data rtr)) (vlr-remove rtr)) )
 (princ)
)

(defun ReOpenSave:CB ( rtr args / rea )
 (setq rea (vlr-current-reaction-name))
 (cond 
   ( (eq :VLR-BeginSave rea)
     (if (not (zerop (getvar 'dwgtitled))) (setq *ReOpenSave* (apply 'strcat (mapcar 'getvar '(dwgprefix dwgname))) ) )
   )
   ( (eq :VLR-SaveComplete rea)
     (if (and *ReOpenSave* (not (eq (apply 'strcat (mapcar 'getvar '(dwgprefix dwgname))) *ReOpenSave*)))
       (
         (lambda ( fpath / shell )
           (if fpath
             (vl-catch-all-apply
               (function
                 (lambda nil
                   (setq shell (vlax-get-or-create-object "Shell.Application"))
                   (vlax-invoke-method shell 'Open fpath)
                 )
               )
             )
           )
           (vl-catch-all-apply 'vlax-release-object (list shell))
         ); lambda
         *ReOpenSave* 
       )  
     ); if
     (setq *ReOpenSave* nil)
   )
   ( '(67 114 101 100 105 116 115 32 116 111 32 76 101 101 32 77 97 99) )
 ); cond
); defun ReOpenSave

(vl-load-com) (princ)
(C:ReOpenSave_On)

Share this post


Link to post
Share on other sites
ronjonp

Haha ... :)

(apply 'strcat (mapcar 'chr '(67 114 101 100 105 116 115 32 116 111 32 76 101 101 32 77 97 99)))

Share this post


Link to post
Share on other sites
Grrr
Haha ... :)

(apply 'strcat (mapcar 'chr '(67 114 101 100 105 116 115 32 116 111 32 76 101 101 32 77 97 99)))

 

Could not mention my friend in a discrete way (he taught me reactors). :)

Share this post


Link to post
Share on other sites
ronjonp

Maybe you mean you need to copy a file to another directory rather than saveas? Give this a try:

(defun c:foo (/ f nf p sf sh)
 (setq sf "MySubFolderNameZippityDooDah")
 (setq p (getvar 'dwgprefix))
 (setq f (getvar 'dwgname))
 (setq nf (strcat p sf "\\" f))
 (cond	((cond ((findfile nf) (print "File exists...") nil)
       ((vl-file-directory-p (strcat p sf)) (vl-file-copy (strcat p f) nf) t)
       ((vl-mkdir (strcat p sf)) (vl-file-copy (strcat p f) nf) t)
 )
 (setq sh (vlax-get-or-create-object "Shell.Application"))
 (vlax-invoke-method sh 'open nf)
 (vlax-release-object sh)
)
 )
 (princ)
)
(vl-load-com)

Share this post


Link to post
Share on other sites
acad1985

Guys Thank you for your codes.

Both your codes working perfectly.

 

@ronjonp your code is creates a folder on same path and saveas the file on that folder. can you make small modification on this?

 

I have to saveas the files on folder called as "Plans". can we select the path manually instead of Automatically select the path.

 

Once run the lisp dialog box should be open to select the path, (All the files saved inside this folder, E:\Autocad Files\SSC\North Region\SNE ,so file

 

browser ;should open with this folder while open to select the path)

after select the path "Plans" folder will be create automatically on that folder and open automatically.

Share this post


Link to post
Share on other sites
acad1985

Hi, Anyone Please Help me.

Share this post


Link to post
Share on other sites
rkmcswain
The SaveAs command, saves your current drawing to a new location and/or a new file name. Once the file has been saved, your current drawing is now the new file that you just saved. So you don't need to open it because it's already open.

 

Correct, whereas the SAVE command writes the contents of the editor out to a new file, but keeps you in the current file. More info.

Share this post


Link to post
Share on other sites
ronjonp

Try this:

(defun c:foo (/ _dir F NF P SF SH)
 (defun _dir (msg path / sh folder out)
   (or (vl-file-directory-p path) (setq path (getvar 'dwgprefix)))
   (cond ((and	(setq sh (vlax-get-or-create-object "Shell.Application"))
	(setq folder (vlax-invoke-method sh 'browseforfolder 0 msg "&H2000" path))
   )
   (setq out (vlax-get-property (vlax-get-property folder 'self) 'path))
   (setq out (strcat (vl-string-right-trim "\\" out) "\\"))
  )
   )
   (and sh (vlax-release-object sh))
   out
 )
 (if (setq p (_dir "Pick a directory yo!" "E:\\Autocad Files\\SSC\\North Region\\SNE\\"))
   (progn
     (setq f (getvar 'dwgname))
     (setq sf "Plans")
     (setq nf (strcat p sf "\\" f))
     (cond
((cond
   ((findfile nf) (print "File exists...") nil)
   ((vl-file-directory-p (strcat p sf)) (vl-file-copy (strcat (getvar 'dwgprefix) f) nf) t)
   ((vl-mkdir (strcat p sf)) (vl-file-copy (strcat (getvar 'dwgprefix) f) nf) t)
 )
 (setq sh (vlax-get-or-create-object "Shell.Application"))
 (vlax-invoke-method sh 'open nf)
 (vlax-release-object sh)
)
     )
   )
 )
 (princ)
)
(vl-load-com)

Share this post


Link to post
Share on other sites
acad1985

This is amazing. worked perfectly.

This is worked what i want,Thank you so much ronjonp.

 

Thank you all of your effort.

 

Much Appreciated.:D

Share this post


Link to post
Share on other sites
gmmdinesh

Hey Guys, i had a same scenario here.

I have a Lisp file to do some process automatically, every time we need saveas the file and run the lisp.

instead of that, can we run the lisp automatically after open the saved file through above posted lisp?

We can't add the lisp to Acaddoc.lsp file because we need to run the lisp to the file which we opened by above code(Saveas file)

 

I think if i add the lisp on Acaddoc.lsp, it's will run all the time when we open the AutoCAD right?

 

Thanks

Share this post


Link to post
Share on other sites
ronjonp
This is amazing. worked perfectly.

This is worked what i want,Thank you so much ronjonp.

 

Thank you all of your effort.

 

Much Appreciated.:D

 

Glad to help :)

Share this post


Link to post
Share on other sites
gmmdinesh
Hey Guys, i had a same scenario here.

I have a Lisp file to do some process automatically, every time we need saveas the file and run the lisp.

instead of that, can we run the lisp automatically after open the saved file through above posted lisp?

We can't add the lisp to Acaddoc.lsp file because we need to run the lisp to the file which we opened by above code(Saveas file)

 

I think if i add the lisp on Acaddoc.lsp, it's will run all the time when we open the AutoCAD right?

 

Anyone Have an idea to do this?

Share this post


Link to post
Share on other sites
acad1985

Hello ronjonp,

The above code was working Perfectly.

but i have a another issue on this.

Can i put the file name manually when i save the file. actually this is save the file automatically.

Every time i need to do the rename of the file once process done.

so if i can put the file name could be better.

 

Sorry for delay.

Thanks.

Share this post


Link to post
Share on other sites
acad1985

Hello... Please help me anyone...:(

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

×