Macro Shared Variable

Started by Admin, December 08, 2010, 06:09:34 AM

Previous topic - Next topic

Admin

I am having trouble with Shared Variables and could use some help.  I am trying to use shared variables to remember the previous entry of a macro so the next time the macro is ran, the previously entered value is default value in the input field.

For instance, lets say I wanted to make a macro to set the range on the error display after a comparison.  Here is how the sequence would go:

1. Run macro (first time)
2. An input window pops up asking for display range (single value) and the default is 0 in the input box.
3. User enters 0.05 and presses enter
4. Display range is set to ?0.05"
5. Run macro (second time)
6. Input box appears and the default value in the box is 0.05

This is the basic functionality of a shared variable I think, but just can't seem to get it to work.  I don't know when I need to create it and if I need to create an if then statement to not recreate the shared variable if it exists already.  Any advice is great.  Thanks.

jrayself

#1
In the scenario you proposed, the value entered for the max distance is remembered in IMInspect already.  <<I misread your post.

Shared variables are for cross-module sharing, i.e. a value entered in IMInspect can be assigned to a shared variable that can be used in IMAlign and IMEdit without re-declaring the variable.
Jason R. Self
Dimensional Engineering, Inc.

jrayself

And once a shared variable is created, it always exists until you run the command to delete it.
Jason R. Self
Dimensional Engineering, Inc.

Admin

I might just read and write that value to a text file.

jrayself

I'd be curious to see the results of that.  Keep me posted.
Jason R. Self
Dimensional Engineering, Inc.

Admin

Here is the functionality of what I thought Shared Variables were.  It turns out the code is pretty simple:

Quoteversion "4.0"

#To show how to store temporary default variable values for macros ran more than once

DECLARE default_var 0

#------------------- START ----------------------------------------------------------------


#Read text file to get values - if this fails because there is no text file yet, it will just keep variable 0
DATA_FILE READ LINE_FIELD ("$_pwk_files_path\user-data\DefaultVariable.txt", 1, 1, default_var)

#Input for new variable
INPUT DOUBLE (default_var, "INPUT", "How Many Now", $default_var)

#Create (override a new blank text file and write the new value
DATA_FILE CREATE ("$_pwk_files_path\user-data\DefaultVariable.txt", "ascii", "yes" )
DATA_FILE APPEND LINE ("$_pwk_files_path\user-data\DefaultVariable.txt", {$default_var, "placeholder to show you can export more than one"})

#------------------- END ----------------------------------------------------------------
MACRO PAUSE ("Macro Complete", "Macro Complete")

tobiwan

You could also use shared variable for that application. But besides not using external files, the solution is not so beautiful  ;)

@jrayself
theres no restriction to use shared variables in only one project.  And closing the WM will delete them also.


@PW User
There should be a way to find out IF there is a shared variable at all. "GET_EXISTING_NAMES" just throws an error if there are no names and, what's worse, creates entry with value "0" in the output, so that it's not possible to find out if there is just one shared variable or none using the SIZE command on the output array variable. 



Here's my suggestion:




version "4.0"

#Local variable to store the value
DECLARE localVar

#Variables to check for existing shared variables
DECLARE arraySize
DECLARE arrayOfNames ""


# stuff
DECLARE count 1
DECLARE input
DECLARE error "No Error"
DECLARE varExists 0


# Check if SharedVariable already exists
MACRO SHARED_VARIABLE GET_EXISTING_NAMES (arrayOfNames)
MACRO GET_ERROR_STATUS (error)

# create your variable if no variable exists
If $error == "Error"
    MACRO SHARED_VARIABLE CREATE("mySharedVariable",-10000.0)
ENDIF

set arraySize SIZE(arrayOfNames)

# or look for your variable among the existing ones ....
while $count <= $arraySize
    IF $arrayOfNames[$count] == "mySharedVariable"
        MACRO SHARED_VARIABLE GET_VALUE ("mySharedVariable",localVar)
        set varExists 1
    endif
    ++ count
ENDWHILE

# or create it if your variable does not exist yet
If $varExists == 0
    MACRO SHARED_VARIABLE CREATE("mySharedVariable",-10000.0)
ENDIF

#query the user
INPUT DOUBLE (input,"Title","Message",$localVar)

#rember the input for next macro run
set localVar $input
MACRO SHARED_VARIABLE SET_VALUE ("mySharedVariable",$localVar)

#do anything else with the variable
#....