TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Matt__W on December 07, 2012, 09:00:45 AM

Title: Send all hatch/images to background prior to printing
Post by: Matt__W on December 07, 2012, 09:00:45 AM
I'm looking for a reactor that would send all hatch/images to the background prior to printing and/or publishing from sheet set manager.
Anyone have anything they could share??  :)
Title: Re: Send all hatch/images to background prior to printing
Post by: Matt__W on December 07, 2012, 11:48:22 AM
Okay, so I've decided to go another route with this and send all hatch to the back just prior to saving.  I've been messing around with some reactor code and can't seem to figure out why it's not working.  Here's what I've got...

Code: [Select]
(defun Hatch2Back ( / ss)
   (if (setq ss (ssget "x" '((0 . "HATCH"))))
      (command "_.draworder" ss "")
   )
   (princ)
)

(defun VLR_COMMAND-IT () 
   (vl-load-com)
   (vlr-command-reactor nil '((:vlr-commandWillStart . startCommand))) 
   (vlr-command-reactor nil '((:vlr-commandEnded . endCommand)))
   (vlr-command-reactor nil '((:vlr-commandCancelled . cancelCommand)))
)

(defun startCommand (calling-reactor startcommandInfo / thecommandstart) 
   (cond
      ((= thecommandstart "SAVE")  (Hatch2Back))
      ((= thecommandstart "QSAVE") (Hatch2Back))
      ((= thecommandstart "SAVEAS")(Hatch2Back))
   )
)

(defun endCommand (calling-reactor endcommandInfo / thecommandend)
;;; Do something here....
)

(defun cancelCommand (calling-reactor cancelcommandInfo / thecommandcancel)
;;; Do something here...
)


(VLR_COMMAND-IT) ; Auto-load the reactors

(princ)

If I manually run the HATCH2BACK command, it works, but not when the command is issued in AutoCAD but I don't know where I'm going wrong with this.
Title: Re: Send all hatch/images to background prior to printing
Post by: ronjonp on December 07, 2012, 12:23:00 PM
You can't have command calls in reactors.

Perhaps this:

Code: [Select]
(defun hatch2back (/ doc exd hatches ms sorttbl)
  ;; Sends modelspace hatches to back
  (setq ms (vla-get-modelspace (setq doc (vla-get-activedocument (vlax-get-acad-object)))))
  (setq exd (vla-getextensiondictionary ms))
  (if (vl-catch-all-error-p
(setq sorttbl (vl-catch-all-apply 'vla-getobject (list exd "acad_sortents")))
      )
    (setq sorttbl (vla-addobject exd "acad_sortents" "acdbsortentstable"))
  )
  (vlax-for o ms
    (if (= "AcDbHatch" (vla-get-objectname o))
      (setq hatches (cons o hatches))
    )
  )
  (and hatches (vlax-invoke sorttbl 'movetobottom hatches))
  ;;(vla-regen doc acallviewports)
  (princ)
)
(or *startcommandreactor*
    (setq *startcommandreactor* (vlr-command-reactor nil '((:vlr-commandwillstart . strtcmd))))
)
(defun strtcmd (calling-reactor strtcmdinfo)
  (and (wcmatch (car strtcmdinfo) "SAVE,QSAVE,SAVEAS") (hatch2back))
)
Title: Re: Send all hatch/images to background prior to printing
Post by: Matt__W on December 07, 2012, 01:06:40 PM
Thanks!  I'll give it a go!
Title: Re: Send all hatch/images to background prior to printing
Post by: Lee Mac on December 07, 2012, 02:45:07 PM
These functions (http://lee-mac.com/draworderfunctions.html) may help - they should be compatible for use in Reactor callback functions.
Title: Re: Send all hatch/images to background prior to printing
Post by: Matt__W on October 30, 2013, 03:14:09 PM
So.... I'm revisiting this and have made some additions but it's not working.  I want to send all hatches to the background THEN send all images to the background so they will end up being behind the hatches.  I've copied some of the code and changed it to recognize raster images but it's not working.  It only sends the hatch to the back.  Where did I go wrong?  I get the following error message.

 error: bad argument type: VLA-OBJECT nil

Code: [Select]
(defun hatch2back (/ doc exd hatches ms sorttbl)
  ;; Sends modelspace hatches to back
  (setq ms (vla-get-modelspace (setq doc (vla-get-activedocument (vlax-get-acad-object)))))
  (setq exd (vla-getextensiondictionary ms))
  (if (vl-catch-all-error-p
(setq sorttbl (vl-catch-all-apply 'vla-getobject (list exd "acad_sortents")))
      )
    (setq sorttbl (vla-addobject exd "acad_sortents" "acdbsortentstable"))
  )
  (vlax-for o ms
    (if (= "AcDbHatch" (vla-get-objectname o))
      (setq hatches (cons o hatches))
    )
  )
  (and hatches (vlax-invoke sorttbl 'movetobottom hatches))
  ;;(vla-regen doc acallviewports)
  (princ)
)

(defun images2back (/ doc3 exd3 images ms3 sorttbl3)
  ;; Sends modelspace hatches to back
  (setq ms3 (vla-get-modelspace (setq doc3 (vla-get-activedocument (vlax-get-acad-object)))))
  (setq exd3 (vla-getextensiondictionary ms3))
  (if (vl-catch-all-error-p
(setq sorttbl3 (vl-catch-all-apply 'vla-getobject (list exd3 "acad_sortents")))
      )
    (setq sorttbl3 (vla-addobject exd3 "acad_sortents" "acdbsortentstable"))
  )
  (vlax-for o ms3
    (if (= "AcDbRasterImage" (vla-get-objectname o))
      (setq images (cons o images))
    )
  )
  (and images (vlax-invoke sorttbl2 'movetobottom images))
  ;;(vla-regen doc acallviewports)
  (princ)
)
(or *startcommandreactor*
    (setq *startcommandreactor* (vlr-command-reactor nil '((:vlr-commandwillstart . strtcmd))))
)

(defun strtcmd (calling-reactor strtcmdinfo)
   (and (wcmatch (car strtcmdinfo) "SAVE,QSAVE,SAVEAS,PLOT") (progn (hatch2back) (images2back) ) ))
)
Title: Re: Send all hatch/images to background prior to printing
Post by: jvillarreal on October 30, 2013, 04:23:01 PM
Looks like this:
Code: [Select]
(and images (vlax-invoke sorttbl2 'movetobottom images))
should actually be

Code: [Select]
(and images (vlax-invoke sorttbl3 'movetobottom images))
Title: Re: Send all hatch/images to background prior to printing
Post by: hmspe on October 30, 2013, 07:03:44 PM
I can't speak to anything related to the sheet set manager, but I wonder why a reactor would be needed for printing.  I have a routine to automatically updates the date on my engineering seal before printing.  The function is named 'c:update_seal' and is in the on_doc_load lisp file.  To call it I changed the command for the print button on the toolbar to '^C^Cupdate_seal;print'.  I intentionally left the drop-down menu entry for printing unchanged so that I have a readily available option that updates and one that does not.  For a function that ran for every plot I would probably redefine the print command to run 'c:update_seal' then call '._print'.  Seems far easier and less prone to issues than a reactor. 
Title: Re: Send all hatch/images to background prior to printing
Post by: Lee Mac on October 30, 2013, 07:32:05 PM
This might be a little more efficient Matt:
Code - Auto/Visual Lisp: [Select]
  1. (defun setdraworder ( / exd ls1 ls2 obn sor spc )
  2.               exd (vla-getextensiondictionary spc)
  3.               sor (cond ((catchapply 'vla-getobject (list exd "acad_sortents")))
  4.                         ((catchapply 'vla-addobject (list exd "acad_sortents" "acdbsortentstable")))
  5.                   )
  6.         )
  7.         (progn
  8.             (vlax-for obj spc
  9.                 (cond
  10.                     (   (= "AcDbHatch" (setq obn (vla-get-objectname obj)))
  11.                         (setq ls1 (cons obj ls1))
  12.                     )
  13.                     (   (= "AcDbRasterImage" obn)
  14.                         (setq ls2 (cons obj ls2))
  15.                     )
  16.                 )
  17.             )
  18.             (if ls1 (vlax-invoke sor 'movetobottom ls1))
  19.             (if ls2 (vlax-invoke sor 'movetobottom ls2))
  20.         )
  21.         (princ "\nUnable to retrieve Sortents Table.")
  22.     )
  23.     (princ)
  24. )
  25. (defun catchapply ( fun arg / rtn )
  26.     (if (not (vl-catch-all-error-p (setq rtn (vl-catch-all-apply fun arg)))) rtn)
  27. )
  28. (defun draworder:callback ( obj arg )
  29.     (if (wcmatch (strcase (car arg)) "SAVE,QSAVE,SAVEAS,PLOT")
  30.         (setdraworder)
  31.     )
  32.     (princ)
  33. )
  34. (if (null draworder:reactor)
  35.     (setq draworder:reactor (vlr-command-reactor nil '((:vlr-commandwillstart . draworder:callback))))
  36. )
Title: Re: Send all hatch/images to background prior to printing
Post by: Matt__W on October 31, 2013, 08:40:22 AM
That works really well, Lee.  Thanks!
Title: Re: Send all hatch/images to background prior to printing
Post by: Lee Mac on October 31, 2013, 09:00:43 AM
That works really well, Lee.  Thanks!

You're welcome Matt  8-)