Jump to content

Change plotstyles display on all currently open docs?


Recommended Posts

Posted

Hi all,

 

Here's a function that doesn't work to set the plot styles for all open drawings.

 

Is there something I'm missing?

 

(defun set-pstyles (showBool / app docs orgDoc) 
  (setq showBool (if showBool :vlax-true :vlax-false))
  (setq app (vlax-get-acad-object))
  (setq docs (vla-get-Documents app))
  (setq orgDoc (vla-get-ActiveDocument app)) ; store the original
  (vlax-for d docs 
    (vla-Activate d)
    (vla-put-ShowPlotStyles (vla-get-ActiveLayout d) showBool)
    (vla-Regen d acActiveViewport)
  )
  (vla-Activate orgDoc)
)

(set-pstyles T)
; (set-pstyles nil)

 

Posted

So where is it going wrong? Is it switching between drawings or just switching to one drawing and then stopping?

Posted
3 hours ago, mhupp said:

 

AutoCAD doesn't like multiple drawings.  you will either have to do scripts or maybe vl-propagate ?

Thanks @mhupp, I'll try that once I finish work tomorrow. 

 

@Steven P, when I replaced the vla-put-ShowPlotStyles with just a (princ "test') I think it only done the current doc. Will test tomorrow. 

  • Like 1
Posted

I've tried this and it's still the same. 

 

;; Define a safe toggle function
(defun-q my-pstyle-toggle (doc state / layout) 
  (setq layout (vla-get-ActiveLayout doc))
  (vla-put-ShowPlotStyles layout (if state :vlax-true :vlax-false))
  (vla-Regen doc acActiveViewport)
)

;; Make sure all drawings know about it
(vl-propagate 'my-pstyle-toggle)

;; Apply it to all open documents
(defun c:AllDocsPstyleOn (/ app docs) 
  (setq app  (vlax-get-acad-object)
        docs (vla-get-Documents app)
  )
  (vlax-for d docs 
    (my-pstyle-toggle d T) ;; pass :vlax-true
  )
  (princ "\nPlot styles ON for all open drawings.")
  (princ)
)

(defun c:AllDocsPstyleOff (/ app docs) 
  (setq app  (vlax-get-acad-object)
        docs (vla-get-Documents app)
  )
  (vlax-for d docs 
    (my-pstyle-toggle d nil) ;; pass :vlax-false
  )
  (princ "\nPlot styles OFF for all open drawings.")
  (princ)
)

 

@Steven P, it's switching drawings alright. Just not toggling the plotstyles.

Posted

Even this simple example doesn't work.

 

; TILEMODE_MODEL
(defun-q RUNTILEMODE_MODEL () 
  (command ".TILEMODE" "1")
  (princ)
)
(vl-propagate 'RUNTILEMODE_MODEL)

(defun c:test (/ app docs) 
  (setq app  (vlax-get-acad-object)
        docs (vla-get-Documents app)
  )
  (vlax-for d docs 
    (RUNTILEMODE_MODEL)
  )
  (princ "\nAll drawings set to model space.")
  (princ)
)

 

Posted
1 hour ago, 3dwannab said:

@Steven P, it's switching drawings alright. Just not toggling the plotstyles.

 

Yup, that sounds about right... LISP generally cannot perform a function on another open drawing.

 

In MHUPPs link Lee Mac explains it in there. The only way I've found you can do it is to make a script for example in Lee Macs Script Write or in Script Writer Pro and use that - the drawings have to be closed though

  • Agree 1
Posted

You can get a list of currently open documents and change to any of those documents. But your lisp will stop. Not sure and have mot tested using a script with this method. The one thing you must do is re do the current document list each time you change dwg. 

 

(defun openblk (blkname / adocs)
(setq acDocs (vla-get-documents (vlax-get-acad-object)))
(vla-open acDocs blkname)
(vla-activate (vla-item acdocs 1))
)

You could load the lisp and run for each dwg name. have a look at 1st line each item inside acdocs for dwg names. Then write a script.

script maybe
(load "openblk")
(openblk "dwg1")
; do your thing
Save
Close
(load "openblk")
(openblk "dwg2")
; do your thing
Save
Close
and so on

 

Example (vlax-get (vla-item acdocs 1) 'name) = "section.dwg" the acdoc has property "Count" so know how many dwgs are open.

Posted (edited)

That's not how vl-propagate works. it only passes variable and its value between all open drawings. 

here is how i used it passing VC and SZ  between all open drawings.  once ZAD is run in one drawing any other drawing you run ZAD in will zoom to the same xy location. it was good to look at old revisions. 

 

;;----------------------------------------------------------------------------;;
;; Zoom Area Across Multiple Drawings
(defun C:ZAD (/ a)
  (initget "Yes No")
  (setq a
    (cond
      ((getkword "\nRedefine Zoom Area? [Yes/No]: ")) ("No")
    )
  )
  (if (= "Yes" a)
    (progn
      (vl-cmdf "_.Zoom" "W" Pause Pause)
      (setq vc (getvar 'viewctr))
      (setq SZ (getvar 'viewsize))
      (vl-propagate 'vc)
      (vl-propagate 'sz)
    )
    (if (or (= vc nil) (= sz nil))
      (prompt "\nPlease Define Zoom Area")
      (vl-cmdf "_.Zoom" "C" VC SZ)
    )
  )
  (princ)
)

 

like i said AutoCAD doesn't like to run one lisp across multiple open drawings. but its been awhile since iv used it. Maybe this would work?

 

--

edit oops didn't hit post

 

Edited by mhupp
  • Like 1
Posted (edited)
8 minutes ago, mhupp said:

That's not how vl-propagate works

 

This thread for RunAll by MP on the swamp wrote this.

 

Quote

Did you know ...

AutoCAD will not let you use vl-propagate on functions or commands defined via defun?

For example:

    (defun foo ( ) (princ "\nHi from foo.") (princ))

    >> FOO

    (vl-propagate 'foo)

    >> (error "Visual LISP: Illegal inter-doc import/export object" #<SUBR @000000002c4dbc00 FOO>)

However you can side step this limitation if you define the function via setq or defun-q (merely a wrapper for setq):

    (setq foo '(( ) (princ "\nHi from foo.") (princ)))

    >> (nil (PRINC "\nHi from foo.") (PRINC))

    or

    (defun-q foo ( ) (princ "\nHi from foo.") (princ))

    >> FOO

    (vl-propagate 'foo)

    >> (nil (PRINC "\nHi from foo.") (PRINC))

Which you could then pass to _RunAll:

    (_RunAll "(foo)")

    >> RunAll: CMD string "(FOO)" attempted in 2 docs: 1.44 secs (0.12 secs/doc).

 

@BIGAL, that RunAll utility will process all open drawings without having to write a script.

Edited by 3dwannab
Posted (edited)


double post deleted.

Edited by 3dwannab
Posted (edited)
20 minutes ago, mhupp said:

 

 shurg. 

Why shurg? Sorry if you're MP over on there.

 

It can pass defun if they're done like so with defun-q:

(defun-q foo ( ) (command "tilemode" 1) (princ))
(vl-propagate 'foo)


The running (foo) in the other open drawings will set them to model space. So this is where I was hoping that my code earlier would work.

Edited by 3dwannab
Posted (edited)

Here's the program I have now.

 

It requires the RunAll Utility found here: https://www.theswamp.org/index.php?topic=53912
 

;;
;; ToggleDisplayColour.lsp
;;
;; Author: 3dwannab + others
;;
;; Version History:
;;   v1.0 - 2024-06-11: Initial version. Updated to change the grid colours depending on if the background is black or white.
;;   v1.1 - 2025.06.16: Updated to change the grid colours depending on if the background is black or white.
;;   v1.2 - 2025.09.04: Updated to set the plot styles display on all open documents with the help of RunAll Utility found here: https://www.theswamp.org/index.php?topic=53912
;;
;; What this does:
;; - Toggles the AutoCAD background colour between black and white in the current space (model or layout).
;; - If the background is white the plot styles display, if black they don't.
;; - Updates grid colours to suit the background and toggles plot style display.
;; - No effect on the block editor. Refreshes DWG to display correctly.
;;
;; NOTES:
;; - Toggling doesn't work when running the command inside the block editor. Not much I think I can do about
;;   apart from perhaps implementing this https://forums.autodesk.com/t5/net-forum/change-the-block-editor-background-color-at-runtime/td-p/9831561.
;;
;; TO DO:
;; – NA
;;

(vl-load-com)

;; Converts RGB values to AutoCAD decimal color value
(defun rgb-to-dec (r g b) 
  (+ r (* g 256) (* b 65536))
)

;; Helper to extract RGB from decimal color
(defun dec-to-rgb (dec / r g b) 
  (setq r (rem dec 256))
  (setq g (rem (/ dec 256) 256))
  (setq b (rem (/ dec 65536) 256))
  (list r g b)
)

;; Helper to compute brightness (perceived luminance)
(defun color-brightness (dec / rgb) 
  (setq rgb (dec-to-rgb dec))
  (+ (* 0.299 (float (car rgb))) 
     (* 0.587 (float (cadr rgb)))
     (* 0.114 (float (caddr rgb)))
  )
)

;-------------------------------------------------------
; rm:displayplotstyles
; 04/03/10 ruul at ctr.co.at
; nomen est omen - toggles display of plot styles...
;-------------------------------------------------------
; Updated 110208 by Mike Sweeney
;
(defun rm:displayplotstyles (bshow ball / acdoc bvshow layout layouts) 
  (princ "\nDisplay plot styles ")
  (vl-load-com)
  (setq acdoc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (setq layouts (vla-get-layouts acdoc))
  (setq bvshow (if bshow :vlax-true :vlax-false))
  (princ (if bshow "ON" "OFF"))
  (cond 
    (ball
     (vlax-for layout layouts 
       (if (/= (vla-get-name layout) "Model") 
         (vla-put-ShowPlotStyles layout bvshow)
       )
     )
     (princ " in all layouts\n")
    )
    (T
     (setq layout (vla-get-ActiveLayout acdoc))
     (vla-put-ShowPlotStyles layout bvshow)
     (princ " in current layout\n")
    )
  )
  (vla-regen acdoc acactiveviewport)
  (princ)
)

;; Returns T if the background is white
(defun isWhiteBackground (/ bgColorDark bkColorLight cur inBlockEditor pref previousColLight previousColor tilemde) 
  ;; Assign cur depending on tilemode
  (setq pref (vla-get-display (vla-get-Preferences (vlax-get-acad-object))))
  (setq tilemde (getvar "tilemode"))
  (if (= tilemde 1) 
    (setq cur (vla-get-GraphicsWinModelBackgrndColor pref))
    (setq cur (vla-get-GraphicsWinLayoutBackgrndColor pref))
  )
  ;; Set up the dark and light colours to toggle
  (setq bgColorDark (rgb-to-dec 0 0 0))
  (setq bkColorLight (rgb-to-dec 255 255 255))
  (setq previousColLight bkColorLight) ; for compatibility with existing code
  ;; Get current background color
  (setq inBlockEditor (= (getvar "BLOCKEDITOR") 1))
  (if inBlockEditor 
    (setq previousColor (atoi (getenv "BEditBackground")))
    (setq previousColor (vlax-variant-value (vlax-variant-change-type cur vlax-vblong)))
  )
  ;; Decide if we need to switch to light or dark based on current colour
  (cond 
    ((= previousColor bgColorDark) T) ; If currently dark, switch to light
    ((= previousColor bkColorLight) nil) ; If currently light, switch to dark
    (T ; Fallback: use brightness as before
     (> (color-brightness previousColor) 
        (/ (+ (color-brightness bgColorDark) (color-brightness bkColorLight)) 2.0)
     )
    )
  )
)

;; Toggles the display from black to white
;; If black the plot styles are not shown
;; If white the plot styles are shown
(defun c:TG (/ bgColorDark bkColorLight doc gridcol-major gridcol-minor gridcolDarkMajor gridcolDarkMinor gridcolLightMajor gridcolLightMinor inBlockEditor pref previousColLight tilemde whiteBackground) 

  (setq doc (vla-get-activedocument (vlax-get-acad-object)))

  ;; Define grid color lists
  (setq gridcolLightMajor (rgb-to-dec 100 200 200))
  (setq gridcolLightMinor (rgb-to-dec 240 240 240))
  (setq gridcolDarkMajor (rgb-to-dec 10 60 60))
  (setq gridcolDarkMinor (rgb-to-dec 35 35 35))

  ;; Assign cur depending on tilemode
  (setq pref (vla-get-display (vla-get-Preferences (vlax-get-acad-object))))
  (setq tilemde (getvar "tilemode"))

  ;; Set up the dark and light colours to toggle
  (setq bgColorDark (rgb-to-dec 0 0 0))
  (setq bkColorLight (rgb-to-dec 255 255 255))
  ; (setq previousColLight bkColorLight) ; for compatibility with existing code

  ;; Get current background color
  (setq inBlockEditor (= (getvar "BLOCKEDITOR") 1))

  (setq whiteBackground (isWhiteBackground))

  ;; Set grid colors based on background
  (if whiteBackground 
    ;; Was light or lighter background, so toggle to dark background
    (progn 
      (setq gridcol-major gridcolLightMajor)
      (setq gridcol-minor gridcolLightMinor)
    )
    ;; Was dark or darker background, so toggle to light background
    (progn 
      (setq gridcol-major gridcolDarkMajor)
      (setq gridcol-minor gridcolDarkMinor)
    )
  )

  ;; Model space and Block Editor
  (if (not (zerop tilemde))  ; Model space

    (progn 
      ;; Toggle background color for model space
      (if (not inBlockEditor) 
        (progn 
          (vla-put-GraphicsWinModelBackgrndColor pref (vlax-make-variant (if whiteBackground bkColorLight bgColorDark) vlax-vblong))
          (setenv "2D Model grid major lines color" (itoa gridcol-major))
          (setenv "2D Model grid minor lines color" (itoa gridcol-minor))
        )
      )

      ;; Toggle Block Editor background color in registry
      (if inBlockEditor 
        (progn 
          ; (setenv "BEditBackground" (itoa (if whiteBackground bgColorDark previousColLight)))
          ; (setenv "BEdit grid major lines color" (itoa gridcol-major))
          ; (setenv "BEdit grid minor lines color" (itoa gridcol-minor))
          (alert "\nNote:\nBackground cannot be updated inside the block editor\n")
        )
      )
    ) ;; progn - Model space

    ;; Paper space (Layout)
    (progn 
      (vla-put-GraphicsWinLayoutBackgrndColor pref (vlax-make-variant (if whiteBackground bkColorLight bgColorDark) vlax-vblong))
      (setenv "Layout grid major lines color" (itoa gridcol-major))
      (setenv "Layout grid minor lines color" (itoa gridcol-minor))
    ) ;; progn - Paper space
  )

  ;; Toggle plot styles in paper space
  (if (not inBlockEditor) 
    (progn 
      (if whiteBackground 

        ;; Toggles all open docs to display correctly
        (_RunAll "(rm:displayplotstyles t nil)") ;; Turn on display of plot styles with external RunAll Ultity found here: https://www.theswamp.org/index.php?topic=53912
        (_RunAll "(rm:displayplotstyles nil nil)") ;; Turn off display of plot styles with external RunAll Ultity found here: https://www.theswamp.org/index.php?topic=53912

        ;; Old code that doesn't toggle the plot styles on all open drawings
        ; (rm:displayplotstyles t nil) ;; Turn on display of plot styles
        ; (rm:displayplotstyles nil nil) ;; Turn off display of plot styles
      )
    )
  )

  (vlax-release-object pref)

  (princ)
)

; (c:TG) ;; Unblock for testing

 

Edited by 3dwannab

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...