Jump to content

Open drawing before closing current one and run lisp on the newly opened


Recommended Posts

Posted

Hello everyone,

 

I'm trying to find a way that will allow me to open a .dwg from a folder, while editing a .dwg file, close the current file and run a lisp on the file. 

 

Think of this simple scenario: I'm editing A.dwg, I want to open B.dwg from a folder through lisp, then run a lisp on the B.dwg file and finally save and exit both files (changes will be made only B.dwg file).

 

Any direction would be very much appreciated.

 

Thanks

Posted

Plain LISP will work only on the window that starts it. When you move the focus to another drawing, then that LISP doesn't move with you, so open drawing 2, you can't keep running LISP from drawing 1 in it.

 

You can however look at script processes, Lee Mac and others have script editors, and you can look at that. So you start a script via drawing 1, but it is then the script that is running and not a LISP in drawing 1. This lets you shift between drawings, open, save and close them as required.

 

So have a look at Lee Mac script editor or scriptpro, see if they will get you what you want

  • Agree 2
Posted
4 minutes ago, mhupp said:

AutoCAD doesn't play nice with multiple drawings and lisp. look into scripts

 

Whoever was asking tat question is a genius! (with a bad memory, didn't remember that one)

 

 

That will give the basis of a script process, needed a few tweaks to make it work - what that eventually grew into was never quite finished, it kept growing with new ideas, so never quite happy to post it online.

 

For a LISP it is a decent start to update though

 

  • Funny 1
Posted

I think this is the very basics of above,

 

if you look through it change

"(c:za) ;;This is just a LISP for example, "ZA", see above"

to suit your LISPS (it is near the end). Like everything , the nice stuff that goes around it all take up the space and time, here I have copied Lee Macs getfiled to allow you to select files which is most of this.

 

The version I took on from this start includes some error checking and so on (if you select a folder from getfiled this will cause problems for example), a nicer interface to let you enter commands easier and other nice stuff, but this should do what you want.

 

 

(defun c:za() (command "zoom" "a" "zoom" ".95x")(princ));;Zoom all, then zooms out 0.95x

(vl-load-com)
;;------------------------=={ Get Files Dialog }==----------------------;;
;;                                                                      ;;
;;  An analog of the 'getfiled' function for multiple file selection.   ;;
;;----------------------------------------------------------------------;;
;;  Author:  Lee Mac, Copyright © 2012  -  www.lee-mac.com              ;;
;;----------------------------------------------------------------------;;
;;  Arguments:                                                          ;;
;;  msg - [str/nil] Dialog box label; 'Select Files' if nil or "".      ;;
;;  def - [str/nil] Default directory; dwgprefix if nil or "".          ;;
;;  ext - [str/nil] File extension filter (e.g. "dwg;lsp"); "*" if nil  ;;
;;----------------------------------------------------------------------;;
;;  Returns:  List of selected files, else nil                          ;;
;;----------------------------------------------------------------------;;
;;  Version 1.6    -    2016-03-21                                      ;;
;;----------------------------------------------------------------------;;

(defun LM:getfiles ( msg def ext / *error* dch dcl des dir dirdata lst rtn )

    (defun *error* ( msg )
        (if (= 'file (type des))
            (close des)
        )
        (if (and (= 'int (type dch)) (< 0 dch))
            (unload_dialog dch)
        )
        (if (and (= 'str (type dcl)) (findfile dcl))
            (vl-file-delete dcl)
        )
        (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )    
    
    (if
        (and
            (setq dcl (vl-filename-mktemp nil nil ".dcl"))
            (setq des (open dcl "w"))
            (progn
                (foreach x
                   '(
                        "lst : list_box"
                        "{"
                        "    width = 40.0;"
                        "    height = 20.0;"
                        "    fixed_width = true;"
                        "    fixed_height = true;"
                        "    alignment = centered;"
                        "    multiple_select = true;"
                        "}"
                        "but : button"
                        "{"
                        "    width = 20.0;"
                        "    height = 1.8;"
                        "    fixed_width = true;"
                        "    fixed_height = true;"
                        "    alignment = centered;"
                        "}"
                        "getfiles : dialog"
                        "{"
                        "    key = \"title\"; spacer;"
                        "    : row"
                        "    {"
                        "        alignment = centered;"
                        "        : edit_box { key = \"dir\"; label = \"Folder:\"; }"
                        "        : button"
                        "        {"
                        "            key = \"brw\";"
                        "            label = \"Browse\";"
                        "            fixed_width = true;"
                        "        }"
                        "    }"
                        "    spacer;"
                        "    : row"
                        "    {"
                        "        : column"
                        "        {"
                        "            : lst { key = \"box1\"; }"
                        "            : but { key = \"add\" ; label = \"Add Files\"; }"
                        "        }"
                        "        : column {"
                        "            : lst { key = \"box2\"; }"
                        "            : but { key = \"del\" ; label = \"Remove Files\"; }"
                        "        }"
                        "    }"
                        "    spacer; ok_cancel;"
                        "}"
                    )
                    (write-line x des)
                )
                (setq des (close des))
                (< 0 (setq dch (load_dialog dcl)))
            )
            (new_dialog "getfiles" dch)
        )
        (progn
            (setq ext (if (= 'str (type ext)) (LM:getfiles:str->lst (strcase ext) ";") '("*")))
            (set_tile "title" (if (member msg '(nil "")) "Select Files" msg))
            (set_tile "dir"
                (setq dir
                    (LM:getfiles:fixdir
                        (if (or (member def '(nil "")) (not (vl-file-directory-p (LM:getfiles:fixdir def))))
                            (getvar 'dwgprefix)
                            def
                        )
                    )
                )
            )
            (setq lst (LM:getfiles:updatefilelist dir ext nil))
            (mode_tile "add" 1)
            (mode_tile "del" 1)

            (action_tile "brw"
                (vl-prin1-to-string
                   '(if (setq tmp (LM:getfiles:browseforfolder "" nil 512))
                        (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir tmp)) ext rtn)
                              rtn (LM:getfiles:updateselected dir rtn)
                        )                              
                    )
                )
            )

            (action_tile "dir"
                (vl-prin1-to-string
                   '(if (= 1 $reason)
                        (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir (LM:getfiles:fixdir $value))) ext rtn)
                              rtn (LM:getfiles:updateselected dir rtn)
                        )
                    )
                )
            )

            (action_tile "box1"
                (vl-prin1-to-string
                   '(
                        (lambda ( / itm tmp )
                            (if (setq itm (mapcar '(lambda ( n ) (nth n lst)) (read (strcat "(" $value ")"))))
                                (if (= 4 $reason)
                                    (cond
                                        (   (equal '("..") itm)
                                            (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir (LM:getfiles:updir dir))) ext rtn)
                                                  rtn (LM:getfiles:updateselected dir rtn)
                                            )
                                        )
                                        (   (vl-file-directory-p (setq tmp (LM:getfiles:checkredirect (strcat dir "\\" (car itm)))))
                                            (setq lst (LM:getfiles:updatefilelist (set_tile "dir" (setq dir tmp)) ext rtn)
                                                  rtn (LM:getfiles:updateselected dir rtn)
                                            )
                                        )
                                        (   (setq rtn (LM:getfiles:sort (append rtn (mapcar '(lambda ( x ) (strcat dir "\\" x)) itm)))
                                                  rtn (LM:getfiles:updateselected dir rtn)
                                                  lst (LM:getfiles:updatefilelist dir ext rtn)
                                            )
                                        )
                                    )
                                    (if (vl-every '(lambda ( x ) (vl-file-directory-p (strcat dir "\\" x))) itm)
                                        (mode_tile "add" 1)
                                        (mode_tile "add" 0)
                                    )
                                )
                            )
                        )
                    )
                )
            )

            (action_tile "box2"
                (vl-prin1-to-string
                   '(
                        (lambda ( / itm )
                            (if (setq itm (mapcar '(lambda ( n ) (nth n rtn)) (read (strcat "(" $value ")"))))
                                (if (= 4 $reason)
                                    (setq rtn (LM:getfiles:updateselected dir (vl-remove (car itm) rtn))
                                          lst (LM:getfiles:updatefilelist dir ext rtn)
                                    )
                                    (mode_tile "del" 0)
                                )
                            )
                        )
                    )
                )
            )

            (action_tile "add"
                (vl-prin1-to-string
                   '(
                        (lambda ( / itm )
                            (if
                                (setq itm
                                    (vl-remove-if 'vl-file-directory-p
                                        (mapcar '(lambda ( n ) (nth n lst)) (read (strcat "(" (get_tile "box1") ")")))
                                    )
                                )
                                (setq rtn (LM:getfiles:sort (append rtn (mapcar '(lambda ( x ) (strcat dir "\\" x)) itm)))
                                      rtn (LM:getfiles:updateselected dir rtn)
                                      lst (LM:getfiles:updatefilelist dir ext rtn)
                                )
                            )
                            (mode_tile "add" 1)
                            (mode_tile "del" 1)
                        )
                    )
                )
            )

            (action_tile "del"
                (vl-prin1-to-string
                   '(
                        (lambda ( / itm )
                            (if (setq itm (read (strcat "(" (get_tile "box2") ")")))
                                (setq rtn (LM:getfiles:updateselected dir (LM:getfiles:removeitems itm rtn))
                                      lst (LM:getfiles:updatefilelist dir ext rtn)
                                )
                            )
                            (mode_tile "add" 1)
                            (mode_tile "del" 1)
                        )
                    )
                )
            )
         
            (if (zerop (start_dialog))
                (setq rtn nil)
            )
        )
    )
    (*error* nil)
    rtn
)

(defun LM:getfiles:listbox ( key lst )
    (start_list key)
    (foreach x lst (add_list x))
    (end_list)
    lst
)

(defun LM:getfiles:listfiles ( dir ext lst )
    (vl-remove-if '(lambda ( x ) (member (strcat dir "\\" x) lst))
        (cond
            (   (cdr (assoc dir dirdata)))
            (   (cdar
                    (setq dirdata
                        (cons
                            (cons dir
                                (append
                                    (LM:getfiles:sortlist (vl-remove "." (vl-directory-files dir nil -1)))
                                    (LM:getfiles:sort
                                        (if (member ext '(("") ("*")))
                                            (vl-directory-files dir nil 1)
                                            (vl-remove-if-not
                                                (function
                                                    (lambda ( x / e )
                                                        (and
                                                            (setq e (vl-filename-extension x))
                                                            (setq e (strcase (substr e 2)))
                                                            (vl-some '(lambda ( w ) (wcmatch e w)) ext)
                                                        )
                                                    )
                                                )
                                                (vl-directory-files dir nil 1)
                                            )
                                        )
                                    )
                                )
                            )
                            dirdata
                        )
                    )
                )
            )
        )
    )
)

(defun LM:getfiles:checkredirect ( dir / itm pos )
    (cond
        (   (vl-directory-files dir) dir)
        (   (and
                (=  (strcase (getenv "UserProfile"))
                    (strcase (substr dir 1 (setq pos (vl-string-position 92 dir nil t))))
                )
                (setq itm
                    (cdr
                        (assoc (substr (strcase dir t) (+ pos 2))
                           '(
                                ("my documents" . "Documents")
                                ("my pictures"  . "Pictures")
                                ("my videos"    . "Videos")
                                ("my music"     . "Music")
                            )
                        )
                    )
                )
                (vl-file-directory-p (setq itm (strcat (substr dir 1 pos) "\\" itm)))
            )
            itm
        )
        (   dir   )
    )
)

(defun LM:getfiles:sort ( lst )
    (apply 'append
        (mapcar 'LM:getfiles:sortlist
            (vl-sort
                (LM:getfiles:groupbyfunction lst
                    (lambda ( a b / x y )
                        (and
                            (setq x (vl-filename-extension a))
                            (setq y (vl-filename-extension b))
                            (= (strcase x) (strcase y))
                        )
                    )
                )
                (function
                    (lambda ( a b / x y )
                        (and
                            (setq x (vl-filename-extension (car a)))
                            (setq y (vl-filename-extension (car b)))
                            (< (strcase x) (strcase y))
                        )
                    )
                )
            )
        )
    )
)

(defun LM:getfiles:sortlist ( lst )
    (mapcar (function (lambda ( n ) (nth n lst)))
        (vl-sort-i (mapcar 'LM:getfiles:splitstring lst)
            (function
                (lambda ( a b / x y )
                    (while
                        (and
                            (setq x (car a))
                            (setq y (car b))
                            (= x y)
                        )
                        (setq a (cdr a)
                              b (cdr b)
                        )
                    )
                    (cond
                        (   (null x) b)
                        (   (null y) nil)
                        (   (and (numberp x) (numberp y)) (< x y))
                        (   (numberp x))
                        (   (numberp y) nil)
                        (   (< x y))
                    )
                )
            )
        )
    )
)

(defun LM:getfiles:groupbyfunction ( lst fun / tmp1 tmp2 x1 )
    (if (setq x1 (car lst))
        (progn
            (foreach x2 (cdr lst)
                (if (fun x1 x2)
                    (setq tmp1 (cons x2 tmp1))
                    (setq tmp2 (cons x2 tmp2))
                )
            )
            (cons (cons x1 (reverse tmp1)) (LM:getfiles:groupbyfunction (reverse tmp2) fun))
        )
    )
)

(defun LM:getfiles:splitstring ( str )
    (
        (lambda ( l )
            (read
                (strcat "("
                    (vl-list->string
                        (apply 'append
                            (mapcar
                                (function
                                    (lambda ( a b c )
                                        (cond
                                            (   (member b '(45 46 92))
                                                (list 32)
                                            )
                                            (   (< 47 b 58)
                                                (list b)
                                            )
                                            (   (list 32 34 b 34 32))
                                        )
                                    )
                                )
                                (cons nil l) l (append (cdr l) '(( )))
                            )
                        )
                    )
                    ")"
                )
            )
        )
        (vl-string->list (strcase str))
    )
)

(defun LM:getfiles:browseforfolder ( msg dir flg / err fld pth shl slf )
    (setq err
        (vl-catch-all-apply
            (function
                (lambda ( / app hwd )
                    (if (setq app (vlax-get-acad-object)
                              shl (vla-getinterfaceobject app "shell.application")
                              hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
                              fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg flg dir)
                        )
                        (setq slf (vlax-get-property fld 'self)
                              pth (LM:getfiles:fixdir (vlax-get-property slf 'path))
                        )
                    )
                )
            )
        )
    )
    (if slf (vlax-release-object slf))
    (if fld (vlax-release-object fld))
    (if shl (vlax-release-object shl))
    (if (vl-catch-all-error-p err)
        (prompt (vl-catch-all-error-message err))
        pth
    )
)

(defun LM:getfiles:full->relative ( dir path / p q )
    (setq dir (vl-string-right-trim "\\" dir))
    (cond
        (   (and
                (setq p (vl-string-position 58  dir))
                (setq q (vl-string-position 58 path))
                (/= (strcase (substr dir 1 p)) (strcase (substr path 1 q)))
            )
            path
        )
        (   (and
                (setq p (vl-string-position 92  dir))
                (setq q (vl-string-position 92 path))
                (= (strcase (substr dir 1 p)) (strcase (substr path 1 q)))
            )
            (LM:getfiles:full->relative (substr dir (+ 2 p)) (substr path (+ 2 q)))
        )
        (   (and
                (setq q (vl-string-position 92 path))
                (= (strcase dir) (strcase (substr path 1 q)))
            )
            (strcat ".\\" (substr path (+ 2 q)))
        )
        (   (= "" dir)
            path
        )
        (   (setq p (vl-string-position 92 dir))
            (LM:getfiles:full->relative (substr dir (+ 2 p)) (strcat "..\\" path))
        )
        (   (LM:getfiles:full->relative "" (strcat "..\\" path)))
    )
)

(defun LM:getfiles:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (cons (substr str 1 pos) (LM:getfiles:str->lst (substr str (+ pos 1 (strlen del))) del))
        (list str)
    )
)

(defun LM:getfiles:updatefilelist ( dir ext lst )
    (LM:getfiles:listbox "box1" (LM:getfiles:listfiles dir ext lst))
)

(defun LM:getfiles:updateselected ( dir lst )
    (LM:getfiles:listbox "box2" (mapcar '(lambda ( x ) (LM:getfiles:full->relative dir x)) lst))
    lst
)

(defun LM:getfiles:updir ( dir )
    (substr dir 1 (vl-string-position 92 dir nil t))
)

(defun LM:getfiles:fixdir ( dir )
    (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir))
)

(defun LM:getfiles:removeitems ( itm lst / idx )
    (setq idx -1)
    (vl-remove-if '(lambda ( x ) (member (setq idx (1+ idx)) itm)) lst)
)





(defun c:BasicBatchLisp ( / batchdirectory batchfileslist l)
;;get files list from folder
  (setq batchfileslist (LM:getfiles "Choose Files" "" "dwg"))

;;Create and open script file
 (setq acount 0) ;; a counter
 (setq tempscript (strcat (getvar "TEMPPREFIX") "tempbatchscrpt.scr")) ;;temp script file
 (setq f (open tempscript "w")) ;;open file

 (foreach n batchfileslist
   (progn
     (write-line (strcat "_.OPEN \"" (nth acount batchfileslist) "\"") f) ;;open the drawing


(c:za) ;;This is just a LISP for example, "ZA", see above


     (write-line "_.CLOSE" f) ;;close the drawing
     (write-line "n" f) ;;'n' to save the drawing (change this about later)
   )
   (setq acount (+ 1 acount))
 )
 (close f)

 (command "_.SCRIPT" tempscript) ;;run script
)

 

Posted

If you have the lisp already saved on hard disk then you can load inside the script, avoids the problem of if not autoloaded.

 

(c:za) ;;This is just a LISP for example, "ZA", see above

 

(if (not za)(load "myZA.lsp"))

(c:za)

  • Like 1
Posted (edited)
8 hours ago, BIGAL said:

If you have the lisp already saved on hard disk then you can load inside the script, avoids the problem of if not autoloaded.

 

(c:za) ;;This is just a LISP for example, "ZA", see above

 

(if (not za)(load "myZA.lsp"))

(c:za)

 

Yup, I should have put that bit in too, for the processing time it takes I often just load them when I want to use them to do the same thing.

 

 

 

I did this small change last night, added a "getstring" to let you type in the LISP name... but like BigAls post above, these need to be loaded at drawing start-up rather than on the fly, updated code is just the 'basicbatchlisp' part and not Lee Macs part, that works great (as expected). Wanting to keep this fairly basic - there are better more comprehensive ones out there. Note that this will only work to run a LISP that doesn't ask for any other inputs (for example, if my ZA LISP above then asked "Enter Zoom Factor" it won't let you do that)

 

 

 

(defun c:BasicBatchLisp ( / batchdirectory batchfileslist l CommandsList NextCommand)

;;Commands List
  (setq CommandsList (list) )
  (while (/= (setq NextCommand (getstring "\nEnter Next Command or ENTER to exit" T)) "")
    (setq CommandsList (append CommandsList (list NextCommand)))
  )

;;get files list from folder
  (setq batchfileslist (LM:getfiles "Choose Files" "" "dwg"))

;;Create and open script file
 (setq acount 0) ;; a counter
 (setq tempscript (strcat (getvar "TEMPPREFIX") "tempbatchscrpt.scr")) ;;make temp script file
 (setq f (open tempscript "w")) ;;open file

 (foreach n batchfileslist
   (progn
     (write-line (strcat "_.OPEN \"" (nth acount batchfileslist) "\"") f) ;;open the drawing

       (setq acounter 0)
       (while (< acounter (length CommandsList)) ;;write commands
         (progn
           (write-line (strcat "(" (nth acounter CommandsList) ")" ) f)
           (setq acounter (+ acounter 1))
         ) ;;end progn
       ) ;;end while
                         
     (write-line "_.CLOSE" f) ;;close the drawing
     (write-line "n" f) ;;'n' to save the drawing (change this about later)
   ) ;;end progn
   (setq acount (+ 1 acount))
 )  ;;end for each n
 (close f)

 (command "_.SCRIPT" tempscript) ;;run script
)

 

 

-EDIT-

and just checking this, if you type in AutoCAD commands as if they were in a LISP that will also work, for example

(command "line" "0,0" "10,10" "")

 

 

Edited by Steven P
Posted

If I have understood correctly, I should create a script file which will run a lisp program on drawing A, then open drawing B (which is essentially a copy of recently saved drawing A, in a different folder) and through this script file run a different lisp program on drawing B and finally closes them both, after saving each file. 

 

Is this big picture?

 

Thank everyone for your time.

Posted

Not quite, but near.

 

Most of the script routines will use AutoCAD and a LISP as a tool to create the script (you can go 'old school' and create it from scratch using a text editor but that sounds like a lot of work). In the script you will have a line to open and a line to close other drawings, there shouldn't be any need to open them yourself, something like this (created using my example above to draw a line 0,0 to 10,10 on these 3 drawings):

 

_.OPEN "C:\Users\Me123\OneDrive\Desktop\AutoCAD\AutoCAD LISPS\New folder\A3 BORDER.dwg" ;;;OPEN A DREAWING
((command "line" "0,0" "10,10" ""))                                                        ;;;DO SOMETHING, CAN DO MANY THINGS JUST KEEP ADDING THEM
_.CLOSE                                                                                    ;;;CLOSE FILE
n                                                                                          ;;;n to save file
_.OPEN "C:\Users\Me123\OneDrive\Desktop\AutoCAD\AutoCAD LISPS\New folder\A3 Title.dwg"  ;;;OPEN ANOTHER DRAWING
((command "line" "0,0" "10,10" ""))
_.CLOSE
n
_.OPEN "C:\Users\Me123\OneDrive\Desktop\AutoCAD\AutoCAD LISPS\New folder\Cable Ruler.dwg"
((command "line" "0,0" "10,10" ""))
_.CLOSE
n

 

You can save this where you want with the extension '.scr'

 

So that is the script file created - open drawing, do stuff, close drawing open next drawing, do stuff, close drawing - and so on

 

To run this you need to have AutoCAD open and use the command:

 

(command "_.SCRIPT" -ScriptFileNameAndPath-) ;;run script

 

which will then run through the script you tell it to. Note that in my script example, it is telling the script to open, do stuff and close files, it doesn't have anything to tell it to do anything to the current file (in this example and in my routine above), all you need to do is put in the 'do stuff' details outside of open and close a file (if that makes sense)(I might go and edit that next, make it work on the open file as well)

 

The LISP routines typically create the script file and do the run script command al in one go

 

 

 

 

 

So, if you look at example script files you can do a lot with them, mostly it is doing the same thing on lots of files. One more confusion before anyone says this is the core console which is even faster but has some limitations - one for another question another day. If you can work out how to get scripts to work you can do a lot of automation on a range of drawings very quickly

Posted (edited)

Thanks again.

 

I'm trying to get the grasp of scripts right now but I'm failing to save the changes.

I've saved the simple function (c:za) as a .lsp file and have it loaded for each drawing opened (through Startup Suite).

In a new lsp file a have this simple code, copied from you

 

(setq batchfileslist (list (strcat "C:\\Users\\RecPC\\Desktop\\transmit\\"(getvar "dwgname")))) ;;i only need one single file that will be saved to a ;;standard directory each time
;;Create and open script file
;;I've left foreach loop untouched, even if I only need one iteration
 (setq acount 0) ;; a counter
 (setq tempscript (strcat (getvar "TEMPPREFIX") "tempbatchscrpt.scr")) ;;temp script file
 (setq f (open tempscript "w")) ;;open file

 (foreach n batchfileslist
   (progn
     (write-line (strcat "_.OPEN \"" (nth acount batchfileslist) "\"") f) ;;open the drawing


;;(c:za) ;;This is just a LISP for example, "ZA", see above
(write-line "(c:za)" f) ;;shouldn't this be like it? within write-line command?

     (write-line "_.CLOSE" f) ;;close the drawing
     (write-line "n" f) ;;'n' to save the drawing (change this about later)
   )
   ;(setq acount (+ 1 acount))
 )
 (close f)

 (command "_.SCRIPT" tempscript) ;;run script

 

After running the last command, the drawing B opens, function (c:za) is being executed but the effect isn't saved. I try to manually open drawing B after all that, and it seems that no change was ever made.

Am I missing something here? Probably yes :p.

 

EDIT: Found my mistake. I wasn't writing "(c:za)" to script file.

Edited by meinfilel
  • Like 1
Posted
4 minutes ago, meinfilel said:

After running the last command, the drawing B opens, function (c:za) is being executed but the effect isn't saved. I try to manually open drawing B after all that, and it seems that no change was ever made.

Am I missing something here? Probably yes :p.

 

need to run the save command either in ZA or the script

 

"_.qsave"
"_.close"

  • Like 1
  • Agree 1
Posted
30 minutes ago, mhupp said:

 

need to run the save command either in ZA or the script

 

"_.qsave"
"_.close"

 

 

I though the (write-line "_.CLOSE" f) and (write-line "n" f) should do that, however a good point to note, always worth double checking and no harm in a double save!

 

(in my defence we have a 'wonderful' file management system that doesn't like qsave, I have a lisp 'asave' - does the same thing - which I'd taken out above)

  • Like 1
Posted
43 minutes ago, meinfilel said:

;;(c:za) ;;This is just a LISP for example, "ZA", see above
(write-line "(c:za)" f) ;;shouldn't this be like it? within write-line command?

 

 

 

 

Whoops - Not doing so well here!! Yes, it should be like that

  • Like 1
Posted

Following Steven P's code, I've achieved what I wanted, maybe it can be more efficient but won't deal with that right now.

 

Thank you all so much.

 

 

  • Like 1
  • Thanks 1
Posted
8 minutes ago, meinfilel said:

Following Steven P's code, I've achieved what I wanted, maybe it can be more efficient but won't deal with that right now.

 

Thank you all so much.

 

 

 

 

Yes, it can be better... and am open to suggestions to make it so too!!

 

Glad it works

Posted

Here is mine. uses getfile to allow user to select the folder they want to process.

 

;;----------------------------------------------------------------------------;;
;; Make script file for drawings in a folder
(defun c:MakeScript (/ Path UserFile blkLst F FileName)
  (vl-load-com)
  (setq UserFile (getfiled "Choose a Folder to Process" (getvar 'DWGPREFIX) "*" 16)
        Path (vl-filename-directory UserFile)
        blkLst (vl-directory-files Path "*.dwg" 1) ;add all dwg files to list
        blkLst (append blkLst (vl-directory-files Path "*.dxf" 1)) ;add all dxf files to list
        blkLst (vl-remove (getvar 'DWGNAME) blkLst) ;if current file is in folder remove from list
        blkLst (vl-sort blkLst '<)
        F (open (strcat (getenv "userprofile") "\\Documents\\Script.scr") "w")
  )
  (if blkLst
    (progn
      (foreach FileName blkLst
        (write-line (strcat "_.Open \"" Path "\\" FileName "\"") F)
        (write-line " commands here " F)
        (write-line "_.qsave" F)
        (write-line "_.close" F)
      )
    )
    (prompt "\nNo Drawings Found in Folder")
  )
  (close F)
  (prompt (strcat "\nScript File for " (itoa (length BlkLst)) " File(s) Created"))
  ;(vl-cmdf "_.Script" (strcat (getenv "userprofile") "\\Documents\\Script.scr"))  ;uncomment if you want to run  right away
  (princ)
)

 

  • Like 1
Posted

Maybe for future when you do close it can ask save Y or N so (write-line "n" f) ;;'n' to save the drawing (change this about later) I am pretty sure will discard changes. need "Y"

 

image.png.7dc7e946eb4fe4028e975f4beed8bde7.png

  • Like 1
Posted
9 hours ago, BIGAL said:

Maybe for future when you do close it can ask save Y or N so (write-line "n" f) ;;'n' to save the drawing (change this about later) I am pretty sure will discard changes. need "Y"

 

image.png.7dc7e946eb4fe4028e975f4beed8bde7.png

 

Actually it works with "n" because opening the dwg using the script file the prompt is "do you want to discard the changes"?

image.png.a106e902505aa3d1082b18bf9216c7f8.png

  • Like 1
Posted (edited)

However I am going to go against what I did earlier and suggest for avoidance of doubt to put in a separate save line just to be sure. Note the below though.

 

 

 

I mentioned above that this was the start of something else, and in that the close at the end I use is close without saving, the user has an option to add a save in the script writing part I made up. I think that in the unusual case of a drawing opening and then closing with no changes this 'Y', 'N' with close causes an issue (but then.... why run a script if you are doing nothing?).

 

If BricsCAD does things slightly differently then maybe it is best to change it to have a qsave and then close (with no save)

 

The close without saving I use is below checks is drawing is modified (dbmod isn't 0) or drawing hasn't been modified (dbmod is 0) (for AutoCAD Bricscad might be different?)

 

(defun c:closer( / )
  (if (= dbmod 0)(command ".close"))
  (if (/= dbmod 0)(command ".close" "y"))
)

 

 

If I get chance later today I will update this according to these comments... running a batch is a recurring theme, so worth taking some time to improve this as a basic tool.

 

 

-edit-

Forgot to mention.. I like just 1 save when running a batch like this unless the user specifically puts it a save in there. AutoCAD creates a backup file which if you get the script wrong you can restore from there. Double saves and of course this backup is to the previous save. That is a reason I did my own thing this way rather than some of the others (and the satisfaction of making something up too)

Edited by Steven P

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