creating a new/open/save/close lisp

creating a new/open/save/close lisp

Satoews
Advocate Advocate
2,711 Views
11 Replies
Message 1 of 12

creating a new/open/save/close lisp

Satoews
Advocate
Advocate

I have been trying to figure out how to creat a lisp that would let me

 

  1. Type in a file number
  2. if the file is in the a certain folder it would open it
  3. if not it would create a new file in the before mentioned folder.
  4. it would then save and close the previous file i have been working on.
  5. If it could also lastly end in editing an attribute for the new drawing that would be great, epic block lot is the block name.

I currently have it set to print to pdf when it saves the file, i don't think that would be effected but i wanted to throw that out there as well.

 

Any help or direction in where to start is greatly appriciated.

 

Thanks in advance

 

 

Shawn T
0 Likes
Accepted solutions (1)
2,712 Views
11 Replies
Replies (11)
Message 2 of 12

hmsilva
Mentor
Mentor

@Anonymous wrote:

I have been trying to figure out how to creat a lisp that would let me

 

  1. Type in a file number
  2. if the file is in the a certain folder it would open it
  3. if not it would create a new file in the before mentioned folder.
  4. it would then save and close the previous file i have been working on.
  5. If it could also lastly end in editing an attribute for the new drawing that would be great, epic block lot is the block name.

Hi shawnt,

what occurs to me is to use a '.scr' file to close/open, to the rest of the code we can use Auto/VisualLISP.

"epic block lot" is the block name?

If so, what is the TAG?

 

Henrique

 

EESignature

Message 3 of 12

Satoews
Advocate
Advocate

I have three tags for Epic Block Lot
ADD
ST1
CTY

 

thanks!!

Shawn T
0 Likes
Message 4 of 12

hmsilva
Mentor
Mentor

@Anonymous wrote:

I have three tags for Epic Block Lot
ADD
ST1
CTY

 

thanks!!


One more question, is the block in model, or in layout?

 

Henrique

EESignature

Message 5 of 12

Satoews
Advocate
Advocate

It is in a layout, LOT-HOUSE if you need the name.

 

thanks!

Shawn T
0 Likes
Message 6 of 12

hmsilva
Mentor
Mentor

@Anonymous wrote:

It is in a layout, LOT-HOUSE if you need the name.

 

thanks!


shawnt,

as a starting point

 

(defun c:demo (/ *error* dwgname name new newfile ofile scrfile)

   (defun *error* (msg)
      (if ofile
         (close ofile)
      )
      (cond ((not msg))
            ((member msg '("Function cancelled" "quit / exit abort")))
            ((princ (strcat "\n** Error: " msg " ** ")))
      )
      (princ)
   )

   (if (and (progn
               (initget 6)
               (setq new (getint "\nEnter the next dwg number: "))
            )
            (setq name (strcat (itoa new) ".dwg"))
            (setq dwgname (strcat (getvar 'dwgprefix) name))
       )
      (progn
         (if (vl-directory-files (getvar 'dwgprefix) name)
            (setq newfile (vla-open (vla-get-Documents (vlax-get-acad-object)) dwgname :vlax-false))
            (setq newfile (vla-add (vla-get-documents (vlax-get-acad-object)) (getenv "LastTemplate")))
         )
         (vlax-for layt (vla-get-layouts newfile)
            (if (= (vla-get-name layt) "LOT-HOUSE")
               (vlax-for blk (vla-get-block layt)
                  (if (and (= (vla-get-objectname blk) "AcDbBlockReference")
                           (= (vla-get-effectivename blk) "Epic Block Lot")
                      )
                     (foreach att (vlax-invoke blk 'getattributes)
                        (cond ((= (vla-get-tagstring att) "ADD")
                               (vla-put-textstring att "New address...")
                              )
                              ((= (vla-get-tagstring att) "ST1")
                               (vla-put-textstring att "New state...")
                              )
                              ((= (vla-get-tagstring att) "CTY")
                               (vla-put-textstring att "New city...")
                              )
                        )
                     )
                  )
               )
            )
         )
         (vla-saveas newfile (strcat (getvar 'dwgprefix) name))
         (vla-close newfile)
         (setq scrfile (strcat (getenv "Temp") "\\My_Open.scr")
               ofile   (open scrfile "w")
         )
         (write-line "_.qsave" ofile)
         (write-line "_.close" ofile)
         (write-line "_.open" ofile)
         (write-line (strcat "\"" dwgname "\"") ofile)
         (close ofile)
         (command "_.script" scrfile)
      )
   )
)

 

Hope this helps,
Henrique

EESignature

Message 7 of 12

Satoews
Advocate
Advocate

appologies for the late response, few issues:

  • only seems to accept 5 digits for the file number
  • the block needs to be edited every time, any way to just automaticly open enhanced attribute editor?
  • the previous drawing dosen't seem to close on its own.


I am learning a lot from your lisp thru the autodesk help and trying to figure out what this is doing.

 

thanks again!

Shawn T
0 Likes
Message 8 of 12

hmsilva
Mentor
Mentor
Accepted solution

@Anonymous wrote:

appologies for the late response, few issues:

  • only seems to accept 5 digits for the file number
  • the block needs to be edited every time, any way to just automaticly open enhanced attribute editor?
  • the previous drawing dosen't seem to close on its own.


I am learning a lot from your lisp thru the autodesk help and trying to figure out what this is doing.

 

thanks again!


Hi shawnt,

the 'demo' it was just a starting point, for your code...

I did change the function to get the 'numbers' for the new dwg, to allow a large number of digits.

The 'demo' show a way to change the attributes in the new dwg without the need to open enhanced attribute editor, if your goal is to use the values from 'Epic Block Lot' at the current dwg, and copy to yhe new dwg, it's easy to add that feature in the code.

the previous drawing should close...

 

I added some comments to the code...

(defun c:demo (/ *error* dwgname name new newfile ofile scrfile)
;; error function
   (defun *error* (msg)
       ; if the ofile is open during the error, close it
      (if ofile
         (close ofile)
      )
      (cond ((not msg))
            ((member msg '("Function cancelled" "quit / exit abort")))
            ((princ (strcat "\n** Error: " msg " ** ")))
      )
      (princ)
   )

    ;; main program
    
    ; asks the user to enter a new number (as a string to allow a large number of digits)
   (if (and (setq new (getstring "\nEnter the next dwg number: "))
            ; test if not an empty string
            (/= new "")
             ; test if the user did enter a valid dwg name
            (snvalid new)
            ; set name with file extension
            (setq name (strcat new ".dwg"))
            ; set dwgname with full path
            (setq dwgname (strcat (getvar 'dwgprefix) name))
       )
       ; if all previous functions are true:
      (progn
          ; test if dwg exists
         (if (vl-directory-files (getvar 'dwgprefix) name)
             ; if exists, open it and set newfile with it, as a vla-object
            (setq newfile (vla-open (vla-get-Documents (vlax-get-acad-object)) dwgname :vlax-false))
             ; if not exists, add it to the document collection using the lat used template, and set newfile with it, as a vla-object
            (setq newfile (vla-add (vla-get-documents (vlax-get-acad-object)) (getenv "LastTemplate")))
         )
         ; foreach layout (layt) from layouts collection from newfile
         (vlax-for layt (vla-get-layouts newfile)
             ; test if layout name is "LOT-HOUSE"
            (if (= (vla-get-name layt) "LOT-HOUSE")
                ; if true, forech block from block collection
               (vlax-for blk (vla-get-block layt)
                   ; test if is objectname property from blk is a "AcDbBlockReference"
                  (if (and (= (vla-get-objectname blk) "AcDbBlockReference")
                           ; and test if the effectivename property from blk is "Epic Block Lot"
                           (= (vla-get-effectivename blk) "Epic Block Lot")
                      )
                      ; if the previous tests are true
                      ; foreach attribute
                     (foreach att (vlax-invoke blk 'getattributes)
                         ; if tagstring is:
                        (cond ((= (vla-get-tagstring att) "ADD")
                               ; put "New address..."
                               (vla-put-textstring att "New address...")
                              )
                              ((= (vla-get-tagstring att) "ST1")
                               ; put "New state..."
                               (vla-put-textstring att "New state...")
                              )
                              ((= (vla-get-tagstring att) "CTY")
                               "New city..."
                               (vla-put-textstring att "New city...")
                              )
                        )
                     )
                  )
               )
            )
         )
         ; save the newfile
         (vla-saveas newfile (strcat (getvar 'dwgprefix) name))
         ; close the newfile
         (vla-close newfile)
         ; start to create the script file
         (setq scrfile (strcat (getenv "Temp") "\\My_Open.scr")
               ; open the script file to write "W" 
               ofile   (open scrfile "w")
         )
         ; write at ofile:
         ; qsave
         (write-line "_.qsave" ofile)
         ; close
         (write-line "_.close" ofile)
         ; open
         (write-line "_.open" ofile)
         ; the dwg name to open
         (write-line (strcat "\"" dwgname "\"") ofile)
         ; close the ofile
         (close ofile)
         ; call the script command to run the previous cript file
         (command "_.script" scrfile)
      )
   )
)

 

Hope this helps,
Henrique

EESignature

Message 9 of 12

Satoews
Advocate
Advocate

VERY NICE, THANK YOU FOR THE EXPLANATIONS IN THE LISP!

 

THANKS!

Shawn T
0 Likes
Message 10 of 12

hmsilva
Mentor
Mentor

@Anonymous wrote:

VERY NICE, THANK YOU FOR THE EXPLANATIONS IN THE LISP!

 

THANKS!


You're welcome, shawnt
Glad I could help

Henrique

EESignature

0 Likes
Message 11 of 12

Satoews
Advocate
Advocate

Sorry to bug you again with this one but I am stuck on one thing that is driving me nuts.

Changes to the code:

 

  • I added T to the getstring so it can accept spaces.
  • added getstrings to the attributes so I can edit them on the fly.

I am stuck on:

 

In the beginning of this code I need to be able to print the previous file. I can't figure out how to add in code right to not break the lisp. 

 

Thanks in advance.

 

(defun c:demo (/ *error* dwgname name new newfile ofile scrfile ADD0 ST0 CTY0)
;; error function
   (defun *error* (msg)
       ; if the ofile is open during the error, close it
      (if ofile
         (close ofile)
      )
      (cond ((not msg))
            ((member msg '("Function cancelled" "quit / exit abort")))
            ((princ (strcat "\n** Error: " msg " ** ")))
      )
      (princ)
   )

    ;; main program
    
    ; asks the user to enter a new number (as a string to allow a large number of digits)
   (if (and (setq new (getstring T "\nEnter the next dwg number: "))
            ; test if not an empty string
            (/= new "")
             ; test if the user did enter a valid dwg name
            (snvalid new)
            ; set name with file extension
            (setq name (strcat new ".dwg"))
            ; set dwgname with full path
            (setq dwgname (strcat (getvar 'dwgprefix) name))
       )
       ; if all previous functions are true:
      (progn
          ; test if dwg exists
         (if (vl-directory-files (getvar 'dwgprefix) name)
             ; if exists, open it and set newfile with it, as a vla-object
            (setq newfile (vla-open (vla-get-Documents (vlax-get-acad-object)) dwgname :vlax-false))
             ; if not exists, add it to the document collection using the lat used template, and set newfile with it, as a vla-object
            (setq newfile (vla-add (vla-get-documents (vlax-get-acad-object)) (getenv "LastTemplate")))
         )
         ; foreach layout (layt) from layouts collection from newfile
         (vlax-for layt (vla-get-layouts newfile)
             ; test if layout name is "LOT-HOUSE"
            (if (= (vla-get-name layt) "LOT-HOUSE")
                ; if true, forech block from block collection
               (vlax-for blk (vla-get-block layt)
                   ; test if is objectname property from blk is a "AcDbBlockReference"
                  (if (and (= (vla-get-objectname blk) "AcDbBlockReference")
                           ; and test if the effectivename property from blk is "Epic Block Lot"
                           (= (vla-get-effectivename blk) "Epic Block Lot")
                      )
                      ; if the previous tests are true
                      ; foreach attribute
                     (foreach att (vlax-invoke blk 'getattributes)
                         ; if tagstring is:
                        (cond ((= (vla-get-tagstring att) "ADD")
                               ; put "New address..."
                               (SETQ ADD0 (GETSTRING T "\nEnter the Address Number: " ))
                               (vla-put-textstring att ADD0)
                              )
                              ((= (vla-get-tagstring att) "ST1")
                               ; put "New street..."
                               (SETQ ST0 (GETSTRING T "\nEnter the Street Address: " ))
                               (vla-put-textstring att ST0)
                              )
                              ((= (vla-get-tagstring att) "CTY")
                               ; put "New city..."
                               (SETQ CTY0 (GETSTRING T "\nEnter the City: " ))
                               (vla-put-textstring att CTY0)
                              )
                        )
                     )
                  )
               )
            )
         )
         ; save the newfile
         (vla-saveas newfile (strcat (getvar 'dwgprefix) name))
         ; close the newfile
         (vla-close newfile)
         ; start to create the script file
         (setq scrfile (strcat (getenv "Temp") "\\My_Open.scr")
               ; open the script file to write "W" 
               ofile   (open scrfile "w")
         )
         ; write at ofile:
         ; qsave
         (write-line "_.qsave" ofile)
         ; close
         (write-line "_.close" ofile)
         ; open
         (write-line "_.open" ofile)
         ; the dwg name to open
         (write-line (strcat "\"" dwgname "\"") ofile)
         ; close the ofile
         (close ofile)
         ; call the script command to run the previous cript file
         (command "_.script" scrfile)
      )
   )
)

 

Shawn T
0 Likes
Message 12 of 12

hmsilva
Mentor
Mentor

shawnt wrote:

...

In the beginning of this code I need to be able to print the previous file. I can't figure out how to add in code right to not break the lisp. 

...


Hi shawnt,

to print the previous file, just add the print command, after the (progn

Do you need to print all layouts?

If so, try to add this code lines after the (progn

 

(setq adoc (vla-get-ActiveDocument (vlax-get-acad-object)))
(vlax-for layt (vla-get-layouts adoc)
   (if (not (eq (vla-get-name layt) "Model"))
      (vla-PlotToDevice (vla-get-plot adoc))
   )
)

 

Hope this helps,
Henrique

EESignature