Author Topic: saveas and closing a dwg  (Read 6185 times)

0 Members and 1 Guest are viewing this topic.

Hangman

  • Guest
saveas and closing a dwg
« on: April 20, 2005, 12:07:40 AM »
Howdy all,

I'm having a bit of a pickle here with a simple routine, can someone give me a knife ???

I want to do a saveas on a 2004 dwg and save it as a 2000.
then, I want to close the dwg without any prompts.
This is what I have so far:
Code: [Select]

    (progn
      (setq fname (getvar "dwgname"))
      (setq fnamel (strlen fname))
      (setq fname (substr fname 1 (- fnamel 4)))
      (setq fend "_2000")
      (setq f-2k (strcat fname fend))
      (setq fpath (getvar "dwgprefix"))
      (setq fpf-2k (strcat fpath f-2k))
      (command ".saveas" "2000" fpf-2k)
    )

This works.  But, when I close the dwg, it asks if I want to save any changes.  If I put in a (command "close"), It asks if I really want to leave without saving the changes.
There have been no changes after the ...  wait a minute,   :roll:  setting the variables back to their original settings would be a change wouldn't it ?.!
So, saving the dwg has to be at the very end of the lisp ???

Sorry, the rantings of a madman.   :oops:
Any help would be greatly appreciated.

nivuahc

  • Guest
saveas and closing a dwg
« Reply #1 on: April 20, 2005, 07:16:59 AM »
That's because AutoCAD 2004 believes that there is no possible way you would want to save your drawing as anything but 2004. If you changed your preferences to save all drawings as 2000 you wouldn't get that prompt... but I'm thinking that's not something you want to do.... I'm gonna look into this and see if I can't come up with something but, chances are, one of the gurus here (CAB, Stig, MP, Mark, etc..) will beat me to it! :D

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
saveas and closing a dwg
« Reply #2 on: April 20, 2005, 07:39:51 AM »
I just ran this in 2005 and it worked.
Code: [Select]

(defun c:s2000 ()
  (command "qsave")
  (setq fname (getvar "dwgname"))
  (setq fnamel (strlen fname))
  (setq fname (substr fname 1 (- fnamel 4)))
  (setq fend "_2000")
  (setq f-2k (strcat fname fend))
  (setq fpath (getvar "dwgprefix"))
  (setq fpf-2k (strcat fpath f-2k))
  (command ".saveas" "2000" fpf-2k)
  (command "close")
  )
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
saveas and closing a dwg
« Reply #3 on: April 20, 2005, 08:00:51 AM »
Another version, less variables.
Code: [Select]

(defun c:s2000 (/ dn)

   (setq dn
(strcat
  (getvar 'dwgprefix)
  (substr
(getvar 'dwgname)
1
(- (strlen (getvar 'dwgname)) 4) ;remove '.dwg'
)
  "_2000.dwg" ; add suffix plus '.dwg'
  )
)

   (command "qsave")

   (command "_.saveas" "2000" dn)

   (command "_.close")

   )
TheSwamp.org  (serving the CAD community since 2003)

nivuahc

  • Guest
saveas and closing a dwg
« Reply #4 on: April 20, 2005, 08:18:42 AM »
More than likely, he's got some goings one when he opens the drawing... like variables being set, layers being created, something along those lines... because AutoCAD thinks that modifications have been made to the drawing.

I didn't notice the appended _2000 bit when I glanced at the code earlier... so that's my best guess

Hangman

  • Guest
saveas and closing a dwg
« Reply #5 on: April 20, 2005, 03:34:04 PM »
Quote
More than likely, he's got some goings one when he opens the drawing... like variables being set, layers being created, something along those lines... because AutoCAD thinks that modifications have been made to the drawing.

Nivuahc,  Yes, I did but I moved some things around a bit and got it to work.  It's funny how the brain works when you start explaining things to others.
I was contemplating the saveas to 2000 preferences, but I was still taking too much time in the typing.  I wanted to create a lisp that would save the dwg to another directory, purge it, bind it and then save it as a 2000 and close with a simple two key keystroke.  I've got most of it, but I have a couple questions at the bottom to finish off.

Mark, I like your code, clean, less of a chance for mistakes.  Mind if I use it ???

So, now another question that goes along with this:
First, If I save a drawing as a 2000 and there is another dwg with that name, an old copy for example that I want to over write, how can I tell it to over write the file and continue the lisp ???

Also, say I want to save this 2000 version to a different directory that doesn't exist.  How can I have it create a directory to save the drawing to ???

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
saveas and closing a dwg
« Reply #6 on: April 20, 2005, 03:52:18 PM »
Quote from: Hangman
Mark, I like your code, clean, less of a chance for mistakes.  Mind if I use it ???

Of course not. That's what it's there for. Enjoy!!

Quote from: Hangman
So, now another question that goes along with this:
First, If I save a drawing as a 2000 and there is another dwg with that name, an old copy for example that I want to over write, how can I tell it to over write the file and continue the lisp ???

Also, say I want to save this 2000 version to a different directory that doesn't exist.  How can I have it create a directory to save the drawing to ???

Two functions come to mind, vl-file-delete and vl-mkdir. If you get stuck on those let us know.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
saveas and closing a dwg
« Reply #7 on: April 20, 2005, 03:55:36 PM »
Don't forget vl-file-copy | vl-file-rename if you wanted to make a backup of a file before over writing it.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
saveas and closing a dwg
« Reply #8 on: April 20, 2005, 03:59:26 PM »
And just for fun, findfile.
:)
TheSwamp.org  (serving the CAD community since 2003)

hudster

  • Gator
  • Posts: 2848
saveas and closing a dwg
« Reply #9 on: April 20, 2005, 04:07:44 PM »
This is cool, i can think of loads of uses for this routine. :D
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Hangman

  • Guest
saveas and closing a dwg
« Reply #10 on: April 21, 2005, 09:49:25 AM »
Hudster,  Throw some ideas at us, what do you see that can be used with this routine ???

Mark, I was looking at findfile, wasn't sure if it could be used in this app though.  I don't know the VL stuff (yet), have the resources but no time to learn it yet.
If I posted the code of what I currently have, could you throw together a VL lisp for me ???

daron

  • Guest
saveas and closing a dwg
« Reply #11 on: April 21, 2005, 09:59:47 AM »
Post the code.

hudster

  • Gator
  • Posts: 2848
saveas and closing a dwg
« Reply #12 on: April 21, 2005, 10:33:54 AM »
It could be used in two scenarios in our office at least.

An issue drawing lisp, which save a copy from the Current drawing to folder to the issued folder.

And also one which moves the drawing to the mods folder when under revision.

At the moment we have to save as and then delete the original in each of these scenarios, if I could automate it, it would be so much easier.  a single click in fact
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
saveas and closing a dwg
« Reply #13 on: April 21, 2005, 11:01:17 AM »
Quote from: Hangman
Mark, I was looking at findfile, wasn't sure if it could be used in this app though.
Quote from: Hangman
First, If I save a drawing as a 2000 and there is another dwg with that name


This will delete the existing *_2000.dwg if it exists then save the new one.
Code: [Select]

(defun c:s2000 (/ dn)

   (setq dn
    (strcat
      (getvar 'dwgprefix)
      (substr
       (getvar 'dwgname)
       1
       (- (strlen (getvar 'dwgname)) 4) ;remove '.dwg'
       )
      "_2000.dwg" ; add suffix plus '.dwg'
      )
    )

   (command "qsave")

   ;
   ; this would likely make more sense using mutiple 'cond' statement
   ;
   (if (not (findfile dn)); if the file was *not* found
       (progn
          (command "_.saveas" "2000" dn)
          (command "_.close")
          )
        ; else we *assume* it exists and delete it
        (if (vl-file-delete (findfile dn))
      (progn
 (command "_.saveas" "2000" dn)
 (command "_.close")
 )
             )
        )

   )

*untested*
TheSwamp.org  (serving the CAD community since 2003)

Hangman

  • Guest
saveas and closing a dwg
« Reply #14 on: April 21, 2005, 11:13:19 AM »
Hudster,  similar to what I'm doing.  After the project is 90%, we need to send it to the client.  I need to copy it to another directory, purge it and bind it.  This routine will make life simple.

Daron, And anyone else who could help, here's the code.

Code: [Select]

;;Simple Save-as, Purge All and Bind routine
;;for preparing dwgs for clients
;;
;;By Hangman April 19, 2005
;;
;;
(defun c:SPG ()
  (command ".undo" "be")
  (setq echoprv (getvar "cmdecho")
        old_err *error*
        *error* $error
  )
  (setvar "cmdecho" 0)
  (setvar "bindtype" 1)
;
  (setq cfpath (getvar "dwgprefix"); Current File Path
        cfname (getvar "dwgname"); Current File Name
        fpend "bind\/" ; -- see note1 below --
        nfpath (strcat cfpath fpend); New File Path
        nfname (strcat nfpath cfname); New File Name
  )
  (command ".saveas" "2004" nfname)
;
  (command "qsave")
  (setq space (getvar "tilemode"))
;
  (if (= space 1)
    (progn
      (command "zoom" "E")
      (command "-layer" "S" "0" "")
      (command "tilemode" 0)
      (command ".pspace")
    )
  )
  (if (= space 0)
    (progn
      (command ".pspace")
      (command "zoom" "E")
      (command "-layer" "S" "0" "")
    )
  )
  (command "purge" "A" "" "N")
  (command "purge" "A" "" "N")
  (command "purge" "A" "" "N"); Just mak'n sure it's all gone
  (command "qsave")
  (command "-xref" "R" "*")
  (command "-xref" "B" "*")
;
  (setq fname (getvar "dwgname"); File Name
        fnamel (strlen fname)
        fname (substr fname 1 (- fnamel 4)); Taking off .dwg
        fnend "_2000" ; Adding to end of File name
        f-2k (strcat fname fnend) ; --see note2 below --
        fpath (getvar "dwgprefix")
        fpf-2k (strcat fpath f-2k) ; New path and file name
  )
  (command ".undo" "e")
  (setq *error* old_err
        old_err nil)
  (setvar "cmdecho" echoprv)
  (command ".saveas" "2000" fpf-2k)
  (princ)
  (command "close")
)


Note 1 --  I need to check for the directory BIND, if it's there, great, continue the code.  If not, then make the directory to save the dwg to.

Note 2 --  I need to check for an existing dwg w/ same name.  If it exists, over write.  If not, great, continue code.

I usually run the purge two or three times because there are some plans that I have that are created with a base plan in mind.  Having several layers with different objects for different sheets.  Think of a five story building, each floor having different objects at each floor.  The objects I have all on the base plan under different layer names designated for each floor.  If I don't purge all before I bind, all those layers for the different floors show up on the dwgs making a real mess.

This code is working, but I need to manually check for the directory and any existing dwgs before I run it.  If I forget to do that, it screws up my original dwg - Don't want that.

Also, could you give me some suggestions on my $error input, I don't think this is working very well.

Thanks guys.