Author Topic: Help me to fix the code with reactor  (Read 2420 times)

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Help me to fix the code with reactor
« on: March 30, 2014, 11:21:21 AM »
Hi all...

I've searched on how to make hatch-wipeouts (to "cheat" ACAD with double hatches), but I run into some kind of bug with reactor activated... Please before you load routine, use (vlr-remove-all) command so that reactor may fire correctly (twice loading, or more may produce unpredicted results)...

Code - Auto/Visual Lisp: [Select]
  1. (setq *foo_reactor* (vlr-sysvar-reactor nil '((:vlr-sysVarChanged . Foo_Callback))))
  2.  
  3. (defun foo_Callback ( rea lst / newtilemode lay )
  4.   (if (and
  5.         (= "TILEMODE" (car lst))
  6.         (= T (cadr lst))
  7.         (/= (setq newtilemode (getvar 'tilemode))
  8.             (cond (*tilemode*)
  9.                   ((setq *tilemode* (getvar 'tilemode))))))
  10.     (progn
  11.       (setq *tilemode* newtilemode)
  12.       (princ "\n\"TILEMODE\" has changed: ")
  13.       (princ newtilemode)
  14.       )
  15.       (setvar 'clayer "0")
  16.       (if (tblsearch "LAYER" "HATCH-WIPEOUT")
  17.         (progn
  18.           (setq lay (vla-item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) "HATCH-WIPEOUT"))
  19.           (if (eq newtilemode 0) (vla-put-freeze lay :vlax-true) (vla-put-freeze lay :vlax-false))
  20.         )
  21.       )
  22.       (terpri))
  23.   )
  24. )
  25.  
  26. (defun c:bhwipeout ( / adoc True->RGB orlay lay bckmodcol rgb bound )
  27.  
  28.  
  29.   (defun True->RGB ( c )
  30.     (mapcar '(lambda ( x ) (lsh (lsh (fix c) x) -24)) '(8 16 24))
  31.   )
  32.  
  33.   (setq orlay (getvar 'clayer))
  34.   (if (not (tblsearch "LAYER" "HATCH-WIPEOUT"))
  35.     (setq lay (vla-add (vla-get-layers adoc) "HATCH-WIPEOUT"))
  36.     (setq lay (vla-item (vla-get-layers adoc) "HATCH-WIPEOUT"))
  37.   )
  38.   (vla-put-plottable lay :vlax-false)
  39.   (setq rgb (reverse (true->rgb (atoi bckmodcol))))
  40.   (command "_.layer" "_S" "HATCH-WIPEOUT" "_C" "_T" (strcat (itoa (car rgb)) "," (itoa (cadr rgb)) "," (itoa (caddr rgb))) "" "")
  41.   (setvar 'clayer orlay)
  42.   (setvar 'hpcolor "255,255,255")
  43.   (setvar 'hpname "SOLID")
  44.   (prompt "\nPick boundary entity for wipeout hatch")
  45.   (setq bound (ssname (ssget "_+.:E:S:L" '((0 . "*POLYLINE,CIRCLE,ELLIPSE,SPLINE,REGION"))) 0))
  46.   (command "_.-bhatch" "_S" bound "" "")
  47.   (setvar 'clayer "HATCH-WIPEOUT")
  48.   (setvar 'hpcolor "ByLayer")
  49.   (command "_.-bhatch" "_S" bound "" "")
  50.   (command "_.draworder" bound "" "_F")
  51.   (setvar 'clayer orlay)
  52.   (princ)
  53. )
  54.  

I am pretty slim when it comes to reactors, so I've just used one from BlackBox and Lee's subfunction True->RGB and applied reactor to my situation (case), but when routine is fired and wipeout(hatches) are created and when I switch for a few times from Model to Paper space, Model Space just blanks and I can't see anything and not even cursor - it changed to mouse Windows arrow...

Please, help me to solve this problem if you know how...

M.R.
« Last Edit: March 30, 2014, 05:30:47 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help me to fix the code with reactor
« Reply #1 on: March 30, 2014, 02:20:52 PM »
I can't reproduce the problem in BricsCAD V14. But have you tried commenting out line 24?:
Code: [Select]
(vla-regen (vla-get-activedocument (vlax-get-acad-object)) AcAllViewports)
If that solves the problem and you require the regen, consider using a 'postprocess'. Line 24 would then become:
Code: [Select]
(vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) "(RegenPostProcess) ")And you would need this function:
Code: [Select]
(defun RegenPostProcess () (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acallviewports) (princ))

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Help me to fix the code with reactor
« Reply #2 on: March 30, 2014, 03:19:22 PM »
No it didn't help, again the same bug... Does it work in Brics...?

Code: [Select]
(defun regenpostprocess nil
  (vl-load-com)
  (vla-regen (vla-get-activedocument (vlax-get-acad-object)) AcAllViewports)
)

(defun reactor nil
  (vl-load-com)
  (setq *foo_reactor* (vlr-sysvar-reactor nil '((:vlr-sysVarChanged . Foo_Callback))))
)
 
(defun foo_Callback ( rea lst / newtilemode lay )
  (vl-load-com)
  (if (and
        (= "TILEMODE" (car lst))
        (= T (cadr lst))
        (/= (setq newtilemode (getvar 'tilemode))
            (cond (*tilemode*)
                  ((setq *tilemode* (getvar 'tilemode))))))
    (progn
      (setq *tilemode* newtilemode)
      (princ "\n\"TILEMODE\" has changed: ")
      (princ newtilemode)
      (if (eq (vla-get-freeze (vla-item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) "0")) :vlax-true)
        (vla-put-freeze (vla-item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) "0") :vlax-false)
      )
      (setvar 'clayer "0")
      (if (tblsearch "LAYER" "HATCH-WIPEOUT")
        (progn
          (setq lay (vla-item (vla-get-layers (vla-get-activedocument (vlax-get-acad-object))) "HATCH-WIPEOUT"))
          (if (eq newtilemode 0) (vla-put-freeze lay :vlax-true) (vla-put-freeze lay :vlax-false))
        )
      )
      (vla-sendcommand (vla-get-activedocument (vlax-get-acad-object)) "(regenpostprocess)\n")
      (terpri))
  )
)

(defun c:bhwipeout ( / adoc True->RGB orlay lay bckmodcol rgb bound )

  (vl-load-com)
  (setq adoc (vla-get-activedocument (vlax-get-acad-object)))

  (defun True->RGB ( c )
    (mapcar '(lambda ( x ) (lsh (lsh (fix c) x) -24)) '(8 16 24))
  )
 
  (setq orlay (getvar 'clayer))
  (if (not (tblsearch "LAYER" "HATCH-WIPEOUT"))
    (setq lay (vla-add (vla-get-layers adoc) "HATCH-WIPEOUT"))
    (setq lay (vla-item (vla-get-layers adoc) "HATCH-WIPEOUT"))
  )
  (vla-put-plottable lay :vlax-false)
  (setq bckmodcol (vlax-variant-value (vlax-variant-change-type (vla-get-graphicswinmodelbackgrndcolor (vla-get-display (vla-get-preferences (vlax-get-acad-object)))) vlax-vbstring)))
  (setq rgb (reverse (true->rgb (atoi bckmodcol))))
  (command "_.layer" "_S" "HATCH-WIPEOUT" "_C" "_T" (strcat (itoa (car rgb)) "," (itoa (cadr rgb)) "," (itoa (caddr rgb))) "" "")
  (setvar 'clayer orlay)
  (setvar 'hpcolor "255,255,255")
  (setvar 'hpname "SOLID")
  (prompt "\nPick boundary entity for wipeout hatch")
  (setq bound (ssname (ssget "_+.:E:S:L" '((0 . "*POLYLINE,CIRCLE,ELLIPSE,SPLINE,REGION"))) 0))
  (command "_.-bhatch" "_S" bound "" "")
  (setvar 'clayer "HATCH-WIPEOUT")
  (setvar 'hpcolor "ByLayer")
  (command "_.-bhatch" "_S" bound "" "")
  (command "_.draworder" bound "" "_F")
  (setvar 'clayer orlay)
  (vlr-remove-all)
  (reactor)
  (princ)
)

If it works, than my effort isn't total waste of time...
« Last Edit: March 30, 2014, 05:31:30 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help me to fix the code with reactor
« Reply #3 on: March 30, 2014, 05:57:39 PM »
I would try moving more callback code to the postprocess.
This line would be my next candidate:
Code: [Select]
(setvar 'clayer "0")
But you could also put everything, except the call to (vla-sendcommand), in the postprocess. This is perhaps even the wisest choice. Using (vla-sendcommand) ensures that the CAD program will execute the code when it is ready to do so. If the code is inside the callback that may not be the case.

Here is an example of the use of a postprocess:
http://www.theswamp.org/index.php?topic=42826.msg505544#msg505544

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Help me to fix the code with reactor
« Reply #4 on: March 30, 2014, 06:24:55 PM »
Thanks, Roy, this solved it... Very much THANKS!!!

Code - Auto/Visual Lisp: [Select]
  1. (defun postprocess nil
  2.   )
  3.   (setvar 'clayer "0")
  4.   (if (tblsearch "LAYER" "HATCH-WIPEOUT")
  5.     (progn
  6.       (if (eq (getvar 'tilemode) 0) (vla-put-freeze lay :vlax-true) (vla-put-freeze lay :vlax-false))
  7.     )
  8.   )
  9. )
  10.  
  11. (defun reactor nil
  12.   (setq *foo_reactor* (vlr-sysvar-reactor nil '((:vlr-sysVarChanged . Foo_Callback))))
  13. )
  14.  
  15. (defun foo_Callback ( rea lst / newtilemode lay )
  16.   (if (and
  17.         (= "TILEMODE" (car lst))
  18.         (= T (cadr lst))
  19.         (/= (setq newtilemode (getvar 'tilemode))
  20.             (cond (*tilemode*)
  21.                   ((setq *tilemode* (getvar 'tilemode))))))
  22.     (progn
  23.       (setq *tilemode* newtilemode)
  24.       (princ "\n\"TILEMODE\" has changed: ")
  25.       (princ newtilemode)
  26.       (terpri))
  27.   )
  28. )
  29.  
  30. (defun c:bhwipeout ( / adoc True->RGB ss i orlay lay bckmodcol rgb bound )
  31.  
  32.  
  33.   (defun True->RGB ( c )
  34.     (mapcar '(lambda ( x ) (lsh (lsh (fix c) x) -24)) '(8 16 24))
  35.   )
  36.  
  37.   (setq orlay (getvar 'clayer))
  38.   (if (not (tblsearch "LAYER" "HATCH-WIPEOUT"))
  39.     (setq lay (vla-add (vla-get-layers adoc) "HATCH-WIPEOUT"))
  40.     (setq lay (vla-item (vla-get-layers adoc) "HATCH-WIPEOUT"))
  41.   )
  42.   (vla-put-plottable lay :vlax-false)
  43.   (setq rgb (reverse (true->rgb (atoi bckmodcol))))
  44.   (command "_.layer" "_S" "HATCH-WIPEOUT" "_C" "_T" (strcat (itoa (car rgb)) "," (itoa (cadr rgb)) "," (itoa (caddr rgb))) "" "")
  45.   (setvar 'clayer orlay)
  46.   (setvar 'hpname "SOLID")
  47.   (prompt "\nSelect boundary entities for wipeout hatches")
  48.   (setq ss (ssget (append (list '(-4 . "<or") '(0 . "CIRCLE") '(-4 . "<and") '(0 . "*POLYLINE") '(-4 . "<not") '(-4 . "&=") '(70 . 8) '(-4 . "not>") '(-4 . "&=") '(70 . 1) '(-4 . "and>") '(-4 . "<and") '(0 . "SPLINE") '(-4 . "&=") '(70 . 1) '(-4 . "and>") '(-4 . "<and") '(0 . "ELLIPSE") '(41 . 0.0)) (list (cons 42 (* 2 pi))) (list '(-4 . "and>") '(0 . "REGION") '(-4 . "or>")))))
  49.   (setq i -1)
  50.   (while (setq bound (ssname ss (setq i (1+ i))))
  51.     (if (>= (atof (substr (getvar "acadver") 1 4)) 18.0) (setvar 'hpcolor "255,255,255") (setvar 'cecolor "RGB:255,255,255"))
  52.     (setvar 'clayer orlay)
  53.     (command "_.draworder" bound "" "_F")
  54.     (command "_.-bhatch" "_S" bound "" "")
  55.     (setvar 'clayer "HATCH-WIPEOUT")
  56.     (if (>= (atof (substr (getvar "acadver") 1 4)) 18.0) (setvar 'hpcolor "ByLayer") (setvar 'cecolor "ByLayer"))
  57.     (command "_.-bhatch" "_S" bound "" "")
  58.     (command "_.draworder" bound "" "_F")
  59.   )
  60.   (setvar 'clayer orlay)
  61.   (if *foo_reactor* (vlr-remove *foo_reactor*))
  62.   (reactor)
  63.   (princ)
  64. )
  65.  

M.R.
« Last Edit: March 31, 2014, 07:15:34 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Help me to fix the code with reactor
« Reply #5 on: March 31, 2014, 04:30:12 AM »
Working with reactors can indeed be tricky. Nice to hear that you got it working.

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Help me to fix the code with reactor
« Reply #6 on: March 31, 2014, 06:05:45 AM »
I've added multiple entity processing (setq ss (ssget ...)) and also included support for older CAD versions that don't have HPCOLOR sysvar...

Once again, thanks Roy... Hope routine is going to be of some help now - it's complete, but I personally don't want to place reactors into my Startup Suite (acaddoc.lsp), so I am using bare version for that case, and when I switch to Paper Space, I manually freeze that unplottable layer "HATCH-WIPEOUT"

Regards, thanks...
M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Re: Help me to fix the code with reactor
« Reply #7 on: March 31, 2014, 06:33:44 AM »
I've made mistake in copy-pasting pieces of codes... Now that's fixed, thanks for your patience, M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube