Author Topic: Help with lisp for automatically saving multiple paperspace viewport layerstates  (Read 3706 times)

0 Members and 1 Guest are viewing this topic.

Pad

  • Bull Frog
  • Posts: 342
Hi

I often receive drawings now where the model space has every drawing sheet layer turned on and it is very difficult to isolate the information I require.
I have to go into the particular paperspace layout which has the information I require, make the viewport active and then save the layerstate.  I can then go into model space and apply this layerstate.
Is there a lisp already or could somebody run through the steps I would need to do to automate this process please?
I would like the current model space layerstate saved and then each paperspace layout viewport layerstate saved using the layout name.   I hope that makes sense.

Thanks
Pads

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Did you check to see if the previous user is using Express Tools Layer States?
That is what I use.  8)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
This will get the layer states in a VP.
http://www.theswamp.org/index.php?topic=4002.0
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Pad

  • Bull Frog
  • Posts: 342
Hi Cab.

Yeah it looks like they must delete them before passing the drawing on.  There are not normally any layerstates saved either by cmd line 'layerstates'  or via the layer states manager.
It looks like express tools has removed LMAN, but 2009 (which I am on) will detect LMAN express tools layerstates and convert them to autocad layerstates.

LMAN
--------------------------------------------------------------------------------

Layer Properties Manager Dialog BoxLMAN, -LMAN, LMANMODE, and -LMANMODE are no longer part of the Express Tools. Use the Layer States Manager dialog box in AutoCAD to achieve the functionality of these commands. For more information about the Layer States Manager dialog box, see the AutoCAD Help system. Also see LAYERSTATE command in the Command Reference.
Note: The LMAN command now invokes the LAYERSTATE command.


Thanks for the link, I've given them a go, but havent been able to figure anything out!
Cheers
P

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I threw this together quickly and borrowed some code from Kerry & Ronjonp.
It should work, did with my short test. Let me know.
Code - Auto/Visual Lisp: [Select]
  1. ;;  CAB 07.02.2014
  2. ;;  Create layer states from each viewport
  3. ;;  Ignore VP with no frozen layers
  4.  
  5. (defun c:vp2ls() (c:vp2layerStates))
  6. (defun c:vp2layerStates (/ a b c tab ll ln ss layerlist layerdict ls_manager
  7.                          clayname obj newlsn lsn nam)
  8.  
  9.   ;;  unlock and thaw all layers
  10.   (defun layerprep (doc / lays lay)
  11.     (setq lays (vla-get-layers doc))
  12.     (vlax-for lay lays
  13.       (if (eq (vla-get-lock lay) :vlax-true)
  14.         (vla-put-lock lay :vlax-false) )
  15.       (if (eq (vla-get-freeze lay) :vlax-true)
  16.         (vla-put-freeze lay :vlax-false))
  17.     )
  18.   )
  19.  
  20.   ;;  freeze a list of layers
  21.   (defun freezelayers (ll doc / lays lay)
  22.     (setq lays (vla-get-layers doc))
  23.     (vlax-for lay lays
  24.       (if (vl-position (vla-get-name lay) ll)
  25.         (vla-put-freeze lay :vlax-true)))
  26.   )
  27.  
  28.  
  29.   ;;  save a layer state from current layers
  30.   (defun ls_save (name lsm)
  31.     (vl-catch-all-error-p (vl-catch-all-apply 'vla-delete (list lsm name))) ; delete the LayerState if it exist
  32.     ;; Save the Named LayerState
  33.     (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-save (list lsm name 511))) ; << acLsAll replaced with 511 kwb
  34.       (strcat "Error:" name)
  35.       name
  36.     )
  37.   )
  38.  
  39.   (defun LayerMake (LName LColor LType)
  40.     (if (not(tblsearch "LAYER" LName))
  41.       (entmakex (list
  42.                  '(0 . "LAYER")
  43.                  '(100 . "AcDbSymbolTableRecord")
  44.                  '(100 . "AcDbLayerTableRecord")
  45.                  (cons 2  LName) ;layer name
  46.                  (cons 6  (if (tblobjname "ltype" ltype) ltype  "Continuous")) ;linetype
  47.                  (cons 62  LColor) ;layer color
  48.                  '(70 . 0) ; on, unlocked, thawed
  49.                 )
  50.       )
  51.     )
  52.   )
  53.  
  54.  
  55.   (defun getstatenames ( doc / nams dict obj)
  56.             (setq obj (vl-catch-all-apply
  57.                         (function (lambda () (vla-item dict "ACAD_LAYERSTATES")))))))
  58.       (vlax-for n obj
  59.         (setq nams (cons (strcase (vla-get-name n)) nams)))
  60.     )
  61.     (setq dict nil)
  62.     nams
  63.   )
  64.  
  65.  
  66.   (setq *acadapp   (vlax-get-acad-object)
  67.         *activedoc (vla-get-activedocument *acadapp)
  68.         layerdict  (vla-getextensiondictionary (vla-get-layers *activedoc))
  69.         ls_manager (vla-getinterfaceobject *acadapp
  70.                      (strcat "AutoCAD.AcadLayerStateManager." (substr (getvar "acadver") 1 2)))
  71.   )
  72.   (vla-setdatabase ls_manager (vla-get-database *activedoc))
  73.  
  74.  
  75.   (setq clayname  (getvar "clayer"))
  76.   (LayerMake "Tmp9z1x" 1 "x") ; temp layer to make current
  77.   (setvar "clayer" "Tmp9z1x")
  78.  
  79.   (setvar "ctab" "Model")
  80.   (ls_save "CurrentStateFound" ls_manager) ; save current state
  81.  
  82.   (if (setq ss (ssget "x" '((0 . "VIEWPORT"))))
  83.     (foreach tab (mapcar 'cadr (ssnamex ss))
  84.       (setq a         (cdr (assoc 330 (entget tab)))
  85.             b         (cdr (assoc 340 (entget a)))
  86.             c         (cdr (assoc 331 (entget b)))
  87.             ln        (cdr (assoc 410 (entget tab))) ; Layout Name
  88.             layerlist nil
  89.             lsn       (strcat ln "-vp" (itoa (cdr (assoc 69 (entget tab)))))
  90.       )
  91.  
  92.       (if (and (not (equal c tab)) (not (vl-position lsn newlsn)))
  93.         (progn
  94.           (layerprep *activedoc) ; thaw all
  95.           (foreach i (entget tab)
  96.             (if (and (= (type (setq ll (cdr i))) 'ename)
  97.                      (setq ll (entget ll))
  98.                      (= (cdr (assoc 0 ll)) "LAYER")
  99.                 )
  100.               (setq layerlist (cons (cdr (assoc 2 ll)) layerlist))
  101.             )
  102.           )
  103.           (if layerlist
  104.             (progn
  105.               (freezelayers layerlist *activedoc)
  106.               (setq newlsn
  107.                      (cons (strcat (ls_save lsn ls_manager) "  " (itoa (length layerList)))
  108.                            newlsn))
  109.             )
  110.           )
  111.         )
  112.       )
  113.  
  114.     ) ; end foreach
  115.   )
  116.  
  117.  
  118.   (if (vl-position (setq nam (strcase "CurrentStateFound")) (getstatenames *activedoc))
  119.     (progn
  120.       (vl-catch-all-apply 'vla-restore (list ls_manager nam))
  121.       ;; (vl-catch-all-apply 'vla-delete (list ls_manager nam)) ; in case you want to delete the temp state
  122.       (vl-cmdf "_.regenall")
  123.     )
  124.   )
  125.   (setvar "clayer" clayname)
  126.   (if (setq obj (vla-item (vla-get-layers *activedoc) "Tmp9z1x"))
  127.     (vla-delete obj)
  128.   )
  129.   (princ "\n Layer States")
  130.   (if newlsn (mapcar 'print newlsn))
  131.   (princ)
  132. )
« Last Edit: July 02, 2014, 03:02:10 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Pad

  • Bull Frog
  • Posts: 342
That works brilliantly, thank you very much.

Not sure why this is an error though:
Command: VP2LS
no function definition: VP2LAYERSTATES
as the command 'vp2layerStates' works fine, it's a bit strange.

I have added:
(Command "tilemode" "1")
(COMMAND "-LAYER" "A" "S" "metrixmodel" "" "" "")
to the top of the lisp to save the layerstate of the modelspace tab.  It seems to do the trick.

This is great and will save me a lot of messing around, especially with the clients never ending revisions..

Cheers
P
« Last Edit: July 01, 2014, 05:53:06 AM by Pad »

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
If you remove the "C:" from line 6 vp2layerStates should be a function.  On the command line you could use either vp2ls or (vp2layerStates).  (vp2layerStates) could also be called from another lisp.
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

Pad

  • Bull Frog
  • Posts: 342
of course, thanks!

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Oops should read
Code: [Select]
(defun c:vp2ls() (c:vp2layerStates))
(defun c:vp2layerStates (/ a b c tab ll ln ss layerlist layerdict ls_manager
                         clayname obj newlsn lsn)
So you can call it from the command line with both names.

Thanks

I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Pad

  • Bull Frog
  • Posts: 342
Thats it.

Thanks CAB.

Do you think there is any improvement I should make to this bit I added:
(Command "tilemode" "1")
(COMMAND "-LAYER" "A" "S" "00-metrixmodel" "" "" "")
Should I add a check to make sure the layerstate doesn't already exist or anything like that?
Thanks again.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
In the code line 90 saves the current model space layer state as
"TEMPstate"
but line 131 restores it and 132 deletes it

You can use your own name and comment out lines 129-135
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Pad

  • Bull Frog
  • Posts: 342
Thanks CAB.  I'll look into that.

P

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Updated the code. It saves the current layer state & restores it but does not delete the new state.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Pad

  • Bull Frog
  • Posts: 342
Ah nice one! Thank you.