Nate Holt's Blog

July 16, 2009

Propagating AutoLISP variables drawing to drawing – the ( vl-propagate…) function

Filed under: Tutorials — nateholt @ 12:52 pm

The last posting gave an example utility to do a find/replace on substrings with MText entities. When going from one drawing to another in AutoCAD, this function could “remember” the previous find and replace substrings, even when AutoCAD was running in its normal “multiple drawing interface” mode.

This was made possible by using the VisualLisp “Blackboard”. The first time the utility was executed, the user’s “find” and “replace” substrings were pushed out to the blackboard. Then, on subsequent runs of the utility, possibly on other drawings later opened in the session, the utility would check the blackboard and retrieve any saved values. These then presented as defaults to the user.

There’s another way to do this, it works well and is a tiny bit simpler. It is to use the (vl-propagate…) function to preset a given AutoLISP variable to a preset value for all open drawings.  It eliminates the “check the blackboard” step. And, even cooler, it presets this variable for any future drawings opened during the AutoCAD session (see highlighted VisualLisp help shown below)!

mtext03

Both the blackboard and propagate methods are detailed here using the last posting’s AutoLISP utility as an example.

Drawing to drawing values – the “Blackboard” method

Here is a snippet from the original version of the utility. It deals with checking for a previous “find” substring value, previously saved on the blackboard as “save_find”. This check for “save_find” on the blackboard happens on the third line down – the call to the function “vl-bb-ref”.

; Look for previous find/replace substrings stored on the
  ; "black board". If found, present these as defaults.
  (setq default_find (vl-bb-ref 'save_find))
  (if (AND default_find (/= default_find ""))
    (progn ; default "find" string found from previous run
      (setq find default_find) ; default from previous run
      (setq find (getstring (strcat "\nFind [" default_find "]=") T))
      (if (= find "")(setq find default_find)) ; use previous
    )
  ; ELSE
    (setq find (getstring "\nFind=" T))
  )
  (if (AND find (/= find ""))
    ; Save the "find" string on the "black-board" for
    ; reference next time command is invoked.
    (vl-bb-set 'save_find find) 
  ) 

And, in the last line above, the current value of the “find” AutoLISP variable is pushed out to the blackboard with the call to “vl-bb-set”. This enables the “find” substring value to be retrieved on subsequents runs of the utility (using the vl-bb-ref function above!).

Drawing to drawing values – the “Propagate” method

 Here is the same snippet but using the “vl-propagate” function. It is a bit cleaner / simpler. The key thing to remember here is to NOT list the propagated AutoLISP variables as “local” variables in the utility’s definition.

; Look for previous find/replace substrings that have been
  ; "propagated" to Lisp variables "default_find" and "default_replace"
  ; in all dwgs of current session, whether open or yet to be opened.
  (if (AND default_find (/= default_find ""))
    (progn ; default "find" string propagated from previous run
      (setq find default_find) ; default from previous run
      (setq find (getstring (strcat "\nFind [" default_find "]=") T))
      (if (= find "")(setq find default_find)) ; use previous
    )
  ; ELSE
    (setq find (getstring "\nFind=" T))
  )
  (if (AND find (/= find ""))
    (progn ; Save the "find" string out to variable "default_find" for all
           ; other open drawings or those that will be opened during this
           ; AutoCAD session.
      (setq default_find find) ; set the value into "default_find"
      (vl-propagate 'default_find) ; preset variable "default_find" everywhere 
  ) )

What happens with this one call to (vl-propagate ‘default_find) is that this AutoLISP variable and its value at the moment the vl-propagate function is executed is suddenly visible within any other drawings open on the screen… AND this variable will magically appear preset to the initial value in any future drawings you open during your AutoCAD session!

Conclusion

There it is. I wish I had stumbled across vl-propagate a bit sooner. It would have simplified setting up an AutoLISP utility’s processing of multiple-drawing in a batching operation.

About these ads

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: