Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Restore a Layer State when opening/closing

  1. #1
    Member
    Join Date
    2015-09
    Posts
    21
    Login to Give a bone
    0

    Default Restore a Layer State when opening/closing

    I would like to restore a default layer state when opening (or closing) a drawing. This is primarily for publishing purposes. I am using ACA 2009. The intent is to publish a set of drawings without needing to first open each one individually to make sure the proper layer state is current or restored. I know this is an old, common problem but could not find a solution on this site.
    thank you

  2. #2
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    You could accomplish it through Lisp:
    Code:
    (defun c:LayerStateRestore (/ Name)
      (if (setq Name (getstring "\nWhich layer state do you want to restore? "))
        (LayerStateRestore Name)
      )
      (princ)
    )
    
    (defun LayerStateRestore (Name / )
      (if (layerstate-restore Name)
        (princ (strcat "\nThe layer state " Name " was restored."))
        (princ (strcat "\nThe layer state " Name " could not be restored."))
      )
    )
    Just create a file named ACADDOC.LSP (if it doesn't already exist) in one of your support folders. It's a normal TXT file, just with a LSP extension - you can make it using Notepad (or preferably AutoCAD's built-in VLIDE command). Copy the code above and add a call like this at the end:
    Code:
    (LayerStateRestore "MyLayerStateName")
    Change to the name you want.

    If you want something more than just setting it current (e.g. importing) look into the Developer Help about the layerstate lisp functions. This is the link towards ACA 2011's relevant page: http://docs.autodesk.com/ARCHDESK/20...b7ccc-69d0.htm

    You can for example import a state from a LAS file (using layerstate-import) or from another DWG (using layerstate-importfromdb).

  3. #3
    Member
    Join Date
    2015-09
    Posts
    21
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    I'm missing something. Bear with me as I have never written a lisp routine.
    I created the ACADDOC.lsp by copying and pasting your routine into notepad. I then placed it in my support folder. My layer state name is State1. See attached. It runs when I open CAD and says that " The layer state State1 was restored" but it doesn't restore it. What have I missed?
    Attached Images Attached Images

  4. #4
    Member
    Join Date
    2015-09
    Posts
    21
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    Ah!
    ok, it restores it model space but not in the layout it was created.

  5. #5
    Member
    Join Date
    2015-09
    Posts
    21
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    OK, now I'm replying to myself.
    I guess it's working but a regen is needed as the proper layer states do not appear in the viewport until a regen is issued or until you toggle to model space and then back to the layout. Before doing a regen, I also did a print preview and all proper layer states showed up in the preview without the regen.

  6. #6
    Certifiable AUGI Addict
    Join Date
    2015-11
    Location
    Jo'burg SA
    Posts
    4,512
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    You could add a regen just after the state has been restored. Also I've added the nil to be more "official" according to the Developer Help, doesn't do much difference:
    Code:
    (defun LayerStateRestore (Name / )
      (if (layerstate-restore Name nil)
        (progn
          (princ (strcat "\nThe layer state " Name " was restored."))
          (command "._REGENALL")
        )
        (princ (strcat "\nThe layer state " Name " could not be restored."))
      )
    )
    The nil means that the state is restored to model space (i.e. the overall drawing layer state). If you need to restore inside a viewport, it's a lot more complex, since you need to obtain the VP's entity name somehow and then pass that instead of the nil to the layerstate-restore function.

    There's some optional codes to this function as well. If you want something different from the default behaviour. E.g. adding a 1 just after the nil will turn off layers not set in the state, 2 will freeze them, 4 will set it as viewport overrides. These values can be added together to make combinations of the 3.

  7. #7
    Member
    Join Date
    2015-09
    Posts
    21
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    I will update my lisp. Thanks for your help! It is much appreciated.

  8. #8
    Member
    Join Date
    2013-01
    Posts
    2
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    I know this is an old thread but hopefully it might get some new hits. Has anybody figured out a way to update (restore) layer states once and have that update propagate throughout the entire drawing?? In other words, making an update (restore) to one viewport but then have that update restore to all other viewports that have that same layer state? (instead of having to go to each individual viewport and manually doing a restore via the LS Manager) I did find a lisp on another forum that does a restore throughout the drawing, "but" it applies the updated layer state to ALL viewports regardless of their current state. Well I don't want to apply the same LS to all viewports, just the ones that match the updated one. I would imagine this lisp can be modified to include a way to match the layer state? Just in case it's possible, here is the current lisp. It does work, but like I said it will apply one layer state to all viewports regardless.

    Code:
    (vl-load-com)
    
    (defun c:LayerStateRestoreAll (/ *error* ss layouts options lastRestored option acDoc n i found)
    
      (defun *error* (msg)
        (if acDoc
          (vla-endundomark acDoc)
        )
        (cond ((not msg))                                                   ; Normal exit
              ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
              ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
        )
        (princ)
      )
    
      (if
        (and
          (setq ss (ssget "_x" '((0 . "VIEWPORT"))))
          (< (length (setq layouts (layoutlist))) (sslength ss))
          (setq options (layerstate-getnames))
          (not (initget 
                 (setq options
                        (vl-string-right-trim
                          " "
                          (apply
                            'strcat
                            (mapcar (function (lambda (x) (strcat x " ")))
                                    options
                            )
                          )
                        )
                 )
               )
          )
          (or
            (setq option
                   (getkword
                     (strcat
                       "\nEnter layer state to restore to all viewports ["
                       (vl-string-translate " " "/" options)
                       "]"
                       (if (setq lastRestored (layerstate-getlastrestored))
                         (strcat "<" lastRestored ">:")
                         ":"
                       )
                     )
                   )
            )
            (setq option
                   (cond (lastRestored lastRestored)
                         ((= 1 (length options)) (car options))
                   )
            )
          )
        )
         (progn
           (vla-startundomark
             (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
           )
           (foreach layout layouts
             (setq found (setq ss nil))
             (if (setq ss
                        (ssget "_x" (list '(0 . "VIEWPORT") (cons 410 layout)))
                 )
               (progn
                 (repeat (setq i (setq n (sslength ss)))
                   (if found
                     (layerstate-restore
                       option
                       (ssname ss (setq i (1- i)))
                       4
                     )
                     (setq found (setq i (1- i)))
                   )
                 )
                 (prompt
                   (strcat "\nLayer state \""
                           option
                           "\" applied to "
                           (itoa (setq n (1- n)))
                                 " viewport"
                                 (if (= 1 n)
                                   ""
                                   "s"
                                 )
                                 " on \""
                                 layout
                                 "\" tab. "
                           )
                 )
               )
             )
           )
         )
         (cond (ss (prompt "\n** No layer states found ** "))
               ((prompt "\n** No viewports found ** "))
         )
      )
      (*error* nil)
    )
    Last edited by BlackBox; 2015-03-20 at 12:21 PM. Reason: Please use [CODE] Tags

  9. #9
    Certifiable AUGI Addict
    Join Date
    2001-03
    Location
    Tallahassee, FL USA
    Posts
    3,667
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    Quote Originally Posted by KIR7449 View Post
    I know this is an old thread but hopefully it might get some new hits. Has anybody figured out a way to update (restore) layer states once and have that update propagate throughout the entire drawing?? In other words, making an update (restore) to one viewport but then have that update restore to all other viewports that have that same layer state? (instead of having to go to each individual viewport and manually doing a restore via the LS Manager) I did find a lisp on another forum that does a restore throughout the drawing, "but" it applies the updated layer state to ALL viewports regardless of their current state. Well I don't want to apply the same LS to all viewports, just the ones that match the updated one. I would imagine this lisp can be modified to include a way to match the layer state? Just in case it's possible, here is the current lisp. It does work, but like I said it will apply one layer state to all viewports regardless.
    Layer States are simply a saved group of layer settings. They could be for every layer in the drawing, just one layer, or any group of layers in between. They do not have the ability to store viewport overrides, and they are not associated or linked to viewports in any way. I wrote a lisp to update current layers to match a Layer States file without adding any layers. Lisp could be written to only modify certain properties if say the only viewport overrides were viewport frozen, but it would not be possible to either save or restore setting for every viewport in every layout in a drawing.

    The lisp you found is probably almost as close as you are going to get. I found the lisp you referred to by BlackBox (coder extraordinaire!) at: http://forums.autodesk.com/t5/visual...s/td-p/4605561. It's posting etiquette to include credit to whoever wrote the code and helpful to all to provide a link to where you found it.
    Last edited by Tom Beauford; 2015-03-20 at 10:59 AM.

  10. #10
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,106
    Login to Give a bone
    0

    Default Re: Restore a Layer State when opening/closing

    Quote Originally Posted by Tom Beauford View Post
    Layer States are simply a saved group of layer settings. They could be for every layer in the drawing, just one layer, or any group of layers in between. They do not have the ability to store viewport overrides, and they are not associated or linked to viewports in any way. I wrote a lisp to update current layers to match a Layer States file without adding any layers. Lisp could be written to only modify certain properties if say the only viewport overrides were viewport frozen, but it would not be possible to either save or restore setting for every viewport in every layout in a drawing.
    The code probably can be tweaked... What am I saying? We all know it can be tweaked. ...to provide a mechanism to assign a layer state to a viewport. Then trigger a layer state restore to restore the assigned layer state per viewport. If no layer state is assigned to the viewport, request the user to specify the desired layer state.

    Quote Originally Posted by Tom Beauford View Post
    The lisp you found is probably almost as close as you are going to get. I found the lisp you referred to by BlackBox (coder extraordinaire!) at: http://forums.autodesk.com/t5/visual...s/td-p/4605561. It's posting etiquette to include credit to whoever wrote the code and helpful to all to provide a link to where you found it.
    We probably should update the forum FAQ with this etiquette, but I dislike having to spell out etiquette (and not just the word).
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

Page 1 of 2 12 LastLast

Similar Threads

  1. Layer state to restore with script
    By stg in forum AutoCAD General
    Replies: 5
    Last Post: 2009-08-11, 01:49 PM
  2. Restore Layer State
    By DW2Whittle in forum CAD Management - General
    Replies: 0
    Last Post: 2008-06-11, 04:54 PM
  3. Keep current Layer Filter current while opening and closing
    By kathy71046 in forum AutoCAD General
    Replies: 8
    Last Post: 2007-05-24, 02:35 AM
  4. Replies: 8
    Last Post: 2006-10-11, 07:36 PM

Posting Permissions

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