Author Topic: How do I pass variables between documents?  (Read 1430 times)

0 Members and 1 Guest are viewing this topic.

gswang

  • Newt
  • Posts: 114
How do I pass variables between documents?
« on: August 26, 2018, 06:08:22 AM »
How do I pass variables between documents?
thanks!

Lee Mac

  • Seagull
  • Posts: 12424
  • London, England
Re: How do I pass variables between documents?
« Reply #1 on: August 26, 2018, 06:34:22 AM »
There are various ways -

1. Use the Blackboard Namespace

When you define functions or assign values to global variables, these are defined within the Document Namespace and are hence only accessible from within the active document. However, you can store variables in a shared namespace known as the 'Blackboard Namespace', which is accessible from within all documents.

For example:

To store data in the Blackboard Namespace, use the vl-bb-set function, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-bb-set 'myvar 123)
  2.  
  3. 123

To retrieve the stored data from the Blackboard Namespace, use the vl-bb-ref function, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-bb-ref 'myvar)
  2. 123

2. Use the vl-propagate function

The vl-propagate function will copy the value of a given variable from the active document namespace into all other document namespaces (including those subsequently opened after the function has been evaluated). However, unlike the use of a shared namespace, since the variables are copied to other namespaces, if you change the value of the variable in one namespace, the value of the variable will not change in the namespaces to which the variable has been copied.

For example:

Drawing1.dwg
Code - Auto/Visual Lisp: [Select]
  1. Command: (setq myvar 123)
  2. 123
  3. 123

Drawing2.dwg
Code - Auto/Visual Lisp: [Select]
  1. Command: !myvar
  2. 123

On changing the variable in Drawing1.dwg:
Code - Auto/Visual Lisp: [Select]
  1. Command: (setq myvar 456)
  2. 456

The value is not changed in Drawing2.dwg (until vl-propagate is called again):
Code - Auto/Visual Lisp: [Select]
  1. Command: !myvar
  2. 123

3. Other Methods

Other methods of transferring data between documents include:
  • Writing data to file within one document and reading the file data from within another document
  • Writing data to the registry within one document and reading the registry data from within another document
« Last Edit: August 26, 2018, 06:43:18 AM by Lee Mac »

gswang

  • Newt
  • Posts: 114
Re: How do I pass variables between documents?
« Reply #2 on: August 27, 2018, 01:43:54 AM »
I'll try it. Thank you very much,Lee Mac!

Mark

  • Custom Title
  • Seagull
  • Posts: 28693
Re: How do I pass variables between documents?
« Reply #3 on: August 27, 2018, 07:59:04 AM »
There are various ways -
Wow! Where is that 'like' button when you need one.

Nice post Lee.

Thanks
TheSwamp.org  (serving the CAD community since 2003)

Lee Mac

  • Seagull
  • Posts: 12424
  • London, England
Re: How do I pass variables between documents?
« Reply #4 on: August 27, 2018, 08:07:35 AM »
Thanks Mark!  :-)

ahsattarian

  • Newt
  • Posts: 50
Re: How do I pass variables between documents?
« Reply #5 on: January 01, 2021, 11:15:45 PM »
Also check this  :



Code - Auto/Visual Lisp: [Select]
  1. (defun c:setq1 () (vl-doc-set a 1))
  2. (defun c:setq2 () (print (vl-doc-ref a)))