+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Senior Member
    Discipline
    Architectural
    svorgodne's Discipline Details
    Occupation
    lisp developer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    May 2007
    Location
    Vienna, austria
    Posts
    134

    Default Script. Save, Open, Close, Switch back to original DWG

    Registered forum members do not see this ad.

    I am creating a copy from a drawing with Autolisp and creating a script which will be dettaching all xrefs on the copy. After saving the copy, the last command of the script is "close", then I would like to go back to the original drawing from where the copy was made from. This is not happening if there are some other drawings opened, then it makes active the last opened drawing. I need an automatic process since I will not be the final user and they might get confused.

    Here it is the code:

    Code:
    (defun run
      (/ mc_scr)
      (setq switch_dwg
        (strcat
          (getvar 'dwgprefix)
          (getvar 'dwgname)
        )
      )
      (setq mc_scr (open (strcat (getenv "temp") "\\mc.scr") "w"))
      (foreach mem0
        (list
          "open"
          (strcat "\"" new_c "\"")
          "(setq opn_dwg_lst nil)"
          "(vlax-for"
          "  x"
          "  (vla-get-documents"
          "    (vlax-get-acad-object)"
          "  )"
          "  (setq opn_dwg_lst (cons x opn_dwg_lst))"
          ")"
          "-xref"
          "d"
          "*"
          "-purge"
          "a"
          "*"
          "n"
          "_ucs"
          "_w"
          "_plan"
          "_w"        
          "_zoom"
          "_e"
          "(foreach SYM_MEM opn_dwg_lst"
          "  (if "
          "    (="
          "      (strcat"
          "        \"A\""
          "        (substr"
          "          (getvar \"dwgname\")"
          "          2"
          "        )"
          "      )"
          "      (vla-get-name SYM_MEM)"
          "    )"
          "    (setq SYM_A SYM_MEM)"
          "  )"
          ")"
          "qsave"
          "close"
          "(vla-activate SYM_A)"
        )
        (write-line mem0 mc_scr)
      )
      (close mc_scr)
    )
    (defun mc
        (/ new_c mc)
        (setvar 'CMDECHO 0)
      (if 
        (=
          (substr
            (getvar 'dwgname)
            1 2
          )
          "A_"
        )
        (progn
          (command "qsave")
          (setq new_c 
            (strcat
              (getvar 'dwgprefix)
              (vl-string-subst "B_" "A_" (getvar 'dwgname))
            )
          )
          (cond
            (
              (/= (findfile new_c) nil)
              (command "save" new_c "y")
              (run)
            )
            (T
              (command "save" new_c)
              (run)
            )
          )
          (command "script" (strcat (getenv "temp") "\\mc.scr"))
        )
      )
      (setvar 'CMDECHO 1)
      (princ)
    )
    (mc)
    Any clue?

    thanks in advance

    Svorgodne

  2. #2
    Super Moderator Cad64's Avatar
    Computer Details
    Cad64's Computer Details
    Operating System:
    Windows 7 Profesional x64
    Computer:
    XI Computers
    Motherboard:
    Asus P8Z77-V Pro
    CPU:
    Intel Core I7 3770K 4.1 Ghz Quad Core 3rd Gen
    RAM:
    24GB DDR3 @ 1333MHz
    Graphics:
    Nvidia GeForce GTX 570 1280MB GDDR5
    Primary Storage:
    SATAII 500GB 7200 RPM
    Secondary Storage:
    Western Digital My Book Studio Edition II - 2TB capacity
    Monitor:
    HPf2105 21" LCD Widescreen & Cintiq 21ux
    Discipline
    Multi-disciplinary
    Cad64's Discipline Details
    Occupation
    Freelance Digital Artist
    Discipline
    Multi-disciplinary
    Details
    3D model building, texturing and rendering. Photoshop Color Graphics.
    Using
    AutoCAD 2014
    Join Date
    Feb 2006
    Location
    Southern California
    Posts
    10,176

    Default

    Your question has been moved to the Autolisp section. Please post your lisp related questions here: http://www.cadtutor.net/forum/forumd...l-LISP-amp-DCL
    CGSociety Portfolio: http://rdeweese.cgsociety.org/
    Facebook:
    https://www.facebook.com/rdeweese67/


    "Work Smart, Not Hard"

  3. #3
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    630

    Default

    I dont really see how your routine would work but maybe I could recommend Lee's script writer program?

    http://lee-mac.com/scriptwriter.html

    I'm not sure if you could go back to the last drawing. If Lee's program doesn't work for you I might have a plan b but it's part of a larger program I wrote for scripts so I would first have to isolate the part that would be of interest to you but I'm kinda busy right now with another program I'm working on. Can tell you what it does , first make a list of all open drawings and save all open drawings , then start a new drawing and close all open drawings , after that open all drawings you want to process and when that's done open all drawings that were previously open. But all that information must be written to the scriptfile before you execute it.

    Gr. Rlx

  4. #4
    Luminous Being
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    9,882

    Default

    One way is to use two scripts upon starting overwrite a script but has only one line
    Open originaldrawing

    Just add script originaldwg as last line.
    A man who never made mistakes never made anything

  5. #5
    Senior Member
    Discipline
    Architectural
    svorgodne's Discipline Details
    Occupation
    lisp developer
    Discipline
    Architectural
    Using
    AutoCAD 2013
    Join Date
    May 2007
    Location
    Vienna, austria
    Posts
    134

    Default

    So far what I have achieved is this
    I have drawing
    "A.dwg"

    Save it adding a prefix
    "PREFIX-A.dwg"

    Open
    "PREFIX-A.dwg"
    via vla-open or script

    Delete some entity (layer) in
    "PREFIX-A.dwg"

    Go back to Drawing
    "A.dwg"
    Using vla-activate

    But not being able to close drawing
    "PREFIX-A.dwg"
    (vla-close maybe?)

    or viceversa

    Close drawing
    "PREFIX-A.dwg"
    (vla-close maybe?)

    But not being able to go back to Drawing
    "A.dwg"
    Using vla-activate


    Of course in any case the problem remains if there are more drawings opened at the same time. gone through that already.

    Thanks again in advance
    Svorgodne

  6. #6
    Senior Member
    Computer Details
    Happy Hobbit's Computer Details
    Operating System:
    Win 7
    Discipline
    Electrical
    Using
    AutoCAD 2014
    Join Date
    Jun 2012
    Location
    Dorset, England
    Posts
    175

    Default

    Why actually close A.dwg ?

    I suggest using vl-file-copy to make a copy, a suffix can easily be coded in, the file opened (within the lisp), the layer deleted the file saved then closed

  7. #7
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    630

    Default

    Registered forum members do not see this ad.

    May this is what your looking 4?


    Code:
    
    ; RlxScript made by Rlx 16-feb-2016
    ; CadTutor example to run a script when in mdi mode
    (defun c:RlxScript ( / RlxScript-MasterScript fp script-lines open-drawing-list restore-list script-cmd)
      (setq RlxScript-MasterScript (strcat (getvar "savefilepath") "RlxScript.scr")
     fp (open RlxScript-MasterScript "w")
     script-lines (RlxScript_SelectScript)
     open-drawing-list (ListAllOpenDrawings)
     restore-list (mapcar '(lambda(x)(if (= (vla-get-ReadOnly (cdr x)) :vlax-true)
           (cons (car x) " y")(cons (car x) ""))) open-drawing-list))
      ; Here you could also pass your own list of drawings. As it is now it will run the script on all open
      ; drawings and moste things could therefore also be done using vla- commands
      ;
      ; Suppose you place a select folder here , something like (setq dwgs2bscripted (getdwgsfromfolder)) , then
      ; replace 'open-drawing-list' with 'dwgs2bscripted'.
      ;
      ; Script will now start new dwg (for script to be able to run)
      ; it will then close all the open drawings , saving their names in the restore-list
      ; Then the script will run on all dwg's in the folder you selected en finally restore all previously open drawings
      ; This routine is basicly ment for working in mdi mode , usually I prefer to run my scripts in sdi mode.
      (if (and fp script-lines open-drawing-list)
        (progn
          (write-line (strcat "(setvar \"filedia\" 0)\n.new\n\n(close_all_but_current)") fp)
          (mapcar '(lambda (open-dwg)
       (write-line (Open_Cmd (car open-dwg)) fp)
         (mapcar '(lambda (script-cmd) (write-line script-cmd fp)) script-lines)
           (write-line (Close_Cmd) fp))
           open-drawing-list)
          (mapcar '(lambda (x)(write-line (strcat ".open " (car x)(cdr x)) fp)) restore-list)))
      (if fp
        (progn (close fp)(gc)(command "._script" RlxScript-MasterScript))
        (alert"Couldn't make script file")))
    (defun ListAllOpenDrawings (/ lst each dwg doclist)
      (setq doclist (vla-get-documents (vlax-get-acad-object)))
      (vlax-for each doclist
        (setq dwg (strcase
      (vl-string-translate "\\" "/"
        (strcat (vla-get-path each) "/" (vla-get-name each))) t))
        (if (not (wcmatch (strcase dwg) "*DRAWING*"))
          (setq lst (append lst (list (cons dwg each))))
        )
      )
      lst
    )
    (defun Open_Cmd (fn / open-cmd fp-tmp)
      (cond ((void fn)(setq open-cmd ""))
     ((not (findfile fn))
      (setq open-cmd
      (strcat ".new\n\n(setvar \"texteval\" 1)\n"
       "text m (getvar \"viewctr\") (/ (getvar\"viewsize\") 25) 0 "
       "\"Not Found " fn "\"")))
     ((assoc fn restore-list)
      (setq open-cmd (strcat ".open " fn (cdr (assoc fn restore-list)))))
     ((IsRO fn)
      (setq open-cmd (strcat ".open " fn " Y")))
     (t (setq open-cmd (strcat ".open " fn)))) open-cmd)
    (defun Close_Cmd ()
      (strcat "(if (= (getvar \"writestat\") 1)(command \".qsave\" \".close\")"
       "(if (> (getvar \"dbmod\") 0)(command \"close\" \"Y\")(command \"close\")))"))
    (defun IsRO (fn / fp)(cond ((setq fp (open fn "a"))(close fp)))(not fp))
    (defun RlxScript_SelectScript ( / script-name script-fp input script-data)
      (if (and (setq script-name (findfile (getfiled "Select a script file" "" "scr" 8)))
        (setq script-fp (open script-name "r"))
        (setq input (read-line script-fp)))
        (while input (setq script-data (cons input script-data) input (read-line script-fp))))
      (if script-fp (close script-fp))
      (reverse script-data)
    )
    Gr. Rlx


    b.t.w. if you want to be able to select a folder you can find an example here : http://www.cadtutor.net/forum/showth...ock-attributte
    Last edited by rlx; 18th Feb 2016 at 11:01 am.

Similar Threads

  1. Open and Save As Pop up disengaged How can I turn this back on.
    By ostsha17 in forum AutoCAD Beginners' Area
    Replies: 3
    Last Post: 20th Jul 2013, 01:43 pm
  2. Open save close routine
    By sakinen in forum AutoLISP, Visual LISP & DCL
    Replies: 6
    Last Post: 17th Jan 2011, 07:00 am
  3. Open, Audit, Purge All, Zoom Extents, Save, and Close the file
    By sachindkini in forum AutoLISP, Visual LISP & DCL
    Replies: 2
    Last Post: 12th Nov 2009, 01:01 pm
  4. Open Close Save Hotfix for AutoCAD 2009
    By ReMark in forum Useful Links
    Replies: 0
    Last Post: 2nd Dec 2008, 12:47 pm
  5. need help with a script - OPEN, INSERT BLOCK, SAVE
    By walmartjanitor in forum AutoLISP, Visual LISP & DCL
    Replies: 3
    Last Post: 20th Sep 2008, 08:34 am

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts