Author Topic: Lisp to toggle layer only works for lines inside a block, unless I undo/redo it  (Read 2040 times)

0 Members and 1 Guest are viewing this topic.

jhadams82

  • Mosquito
  • Posts: 18
Ok, this is a weird one.  I can't imagine why this would be happening. 

I work for a medical device supplier in site planning.  We have a bunch of layers that all start with a number and we have several layer states based on what should show up on plan sheets and a lisp to switch between them.  I just wrote a lisp (based on this thread: http://www.cadtutor.net/forum/showthread.php?68095-LISP-for-If-layer-is-frozen-thaw-it-and-vice-versa) that I want to use to toggle a layer on and off so, for instance, if I'm looking at the s1 layer state I can toggle on/off the 740 layer.

What's weird is that, for most layer states, my lisp only works for entities on the toggled layer that are inside a block, but not for standalone polylines.  What's even weirder is that if I draw a line, then undo it, then undo the lisp, then hit redo, everything shows up.  Now why would it work when redone, but not when called normally?!?  Also, if I start from the "FIN" layer state, the lisp works as expected.

Now if somebody wants to try and replicate the error, I've made non-confidential files that you can use.  So you'll open the "toggle test" drawing and load "toggle layer.lsp" and "Layer States - DXR.lsp".  If you type "fin" to get to the final layer state, you use the command "tl" to toggle layers 740 or 720 or 799.  But if you type "s1" to get to that layer state and try to toggle those layers, you won't see all the linework that you should.

I know this one's really convoluted so if anybody actually makes an attempt at it I will be very very grateful because this is driving me crazy and I have no clue where to even start.  TIA

kpblc

  • Bull Frog
  • Posts: 396
Maybe something like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:tl (/ adoc layers found target en dx) ;  initialize variables
  2.           ;  layers = list of all layers in current drawing
  3.   (if (= (type
  4.            (setq target (vl-catch-all-apply (function (lambda () (getstring "\nEnter layer number <Cancel> : ")))))
  5.            ) ;_ end of type
  6.          'str
  7.          ) ;_ end of =
  8.            (vlax-for layer (vla-get-layers adoc)
  9.              (if (and (wcmatch (strcase (vla-get-name layer)) (strcat "*" target "*"))
  10.                       (not (wcmatch (strcase (vla-get-name layer)) "*XREF*"))
  11.                       ) ;_ end of and
  12.                (setq found (cons (vla-get-name layer) found))
  13.                ) ;_ end of if
  14.              ) ;_ end of vlax-for
  15.            ;;  if "found" is anything but nil, a layer name was found
  16.            ;;  then use XOR function (boole 6) to toggle "frozen" bit flag of that layer
  17.            ;;  else alert user no matching layer was found
  18.            (foreach item found
  19.              (setq en (tblobjname "LAYER" item)
  20.                    en (entget en)
  21.                    dx (assoc 70 en)
  22.                    en (entmod (subst (cons 70 (boole 6 1 (cdr dx))) dx en))
  23.                    ) ;_ end of setq
  24.              ) ;_ end of foreach
  25.            (vla-endundomark adoc)
  26.            ) ;_ end of progn
  27.     ) ;_ end of if
  28.   (if (not found)
  29.     (princ (strcat "\nLayer(s) with mask *" target "* nou found"))
  30.     ) ;_ end of if
  31.   (princ)
  32.   ) ;_ end of defun
  33.  
  34. (defun c:fin ()
  35.   (layerstate-restore "1b - ALL-Final" (acet-currentviewport-ename))
  36.   (command "regen")
  37.   (princ)
  38.   ) ;_ end of defun
  39.  
  40. (defun c:fins ()
  41.   (layerstate-restore "1c - ALL-Final-Seismic" (acet-currentviewport-ename))
  42.   (command "regen")
  43.   (princ)
  44.   ) ;_ end of defun
  45.  
  46. (defun c:pre ()
  47.   (layerstate-restore "1a - ALL-Prelim" (acet-currentviewport-ename))
  48.   (command "regen")
  49.   (princ)
  50.   ) ;_ end of defun
  51.  
  52. (defun c:a1 ()
  53.   (layerstate-restore "2a - A1-Layout" (acet-currentviewport-ename))
  54.   (command "regen")
  55.   (princ)
  56.   ) ;_ end of defun
  57.  
  58. (defun c:hid ()
  59.   (layerstate-restore "2b - A1-Layout plus 799" (acet-currentviewport-ename))
  60.   (command "regen")
  61.   (princ)
  62.   ) ;_ end of defun
  63.  
  64. (defun c:s1 ()
  65.   (layerstate-restore "3 - S1-Layout" (acet-currentviewport-ename))
  66.   (command "regen")
  67.   (princ)
  68.   ) ;_ end of defun
  69.  
  70. (defun c:s1s ()
  71.   (layerstate-restore "4 - S1-Layout-Seismic" (acet-currentviewport-ename))
  72.   (command "regen")
  73.   (princ)
  74.   ) ;_ end of defun
  75.  
  76. (defun c:s2 ()
  77.   (layerstate-restore "5 - S2-Layout" (acet-currentviewport-ename))
  78.   (command "regen")
  79.   (princ)
  80.   ) ;_ end of defun
  81.  
  82. (defun c:e1 ()
  83.   (layerstate-restore "6 - E1-Layout" (acet-currentviewport-ename))
  84.   (command "regen")
  85.   (princ)
  86.   ) ;_ end of defun
  87.  
  88. (defun c:ceil ()
  89.   (layerstate-restore "7a - Ceiling" (acet-currentviewport-ename))
  90.   (command "regen")
  91.   (princ)
  92.   ) ;_ end of defun
  93.  
  94. (defun c:flr ()
  95.   (layerstate-restore "7b - Floor" (acet-currentviewport-ename))
  96.   (command "regen")
  97.   (princ)
  98.   ) ;_ end of defun
Sorry for my English.

jhadams82

  • Mosquito
  • Posts: 18
Wow, thanks.  That looks like much more professional code (I should probably learn about error handling at some point, lol).  But I'm still getting the same behavior.  If I change to any layer state (besides fin & fins), any lines that are not in a block and frozen in that layer state do not show up when that layer is thawed by my lisp.  But if I change to a layer state where the non-block lines are thawed by default, the toggling works.

The fact that if I undo the lisp then redo and THEN it works as expected makes me think this is a bug.  It's very weird.

jhadams82

  • Mosquito
  • Posts: 18
How do I tag Lee Mac?  "Help me Obi-wan Lee Mac.  You're my only hope."  lol

jhadams82

  • Mosquito
  • Posts: 18
 :embarrassed:  :embarrassed:  :embarrassed:

well, I figured it out.  The solution was already in the original thread I linked to (doh!).  OP had the same problem I was running into apparently, and Lee Mac replied with the solution.  Somehow the entities aren't getting updated which strikes me as very weird.  But maybe the boole command is too low level and needs to be accompanied by an (entupd)?  I'm just glad it's working now, but if somebody could enlighten me as to what's going on here, I'd love to know more about it.   :-)