DiscussionsIssue archiveDraw Harness and Samples

Archived discussion

How to access global variables in a proc function loaded with the source command?

Draw Harness and SamplesFri, 08/16/2024 - 10:262 replies

Browse this forum
QuestionAuthor

Hello, I have been experimenting with the DRAW environment and was attempting to use a proc call. The proc is stored in a file that i am importing with the source command. I think my understanding of the upvar command is somehow flawed. Thanks for any help or hints that point in the right direction.

my main script:

pload all
vinit View1

box b 8.0 10.0 15.0
explode b E


vclear

source /showEdges.tcl
display_all_edges

vfit

my /showEdges.tcl file:

proc display_all_edges {} {
    # List all variables in the current session
    set all_vars [directory]

    foreach var $all_vars {
        # Use upvar to link the local variable 'edgeVar' to the global variable named by 'var'
        upvar #0 $var edgeVar

        # Get the output of the 'whatis' command for the global variable
        set var_info [whatis $var]

        # Print the variable name and its type information
        puts "Checking variable: $var -> $var_info"

        # Check if the output contains the word "edge"
        if {[string match "*EDGE*" $var_info]} {
            # Attempt to display the edge variable and handle any errors
            if {[catch {vdisplay $var} result]} {
                puts "Error displaying $var: $result"
            } else {
                puts "Successfully displayed edge: $var"
            }
        } else {
            puts "$var is not an edge, skipping."
        }
    }

    puts "Done displaying all edge variables."
}

Errors related to variables produced with the explode command:


Error: object with name 'b_7' does not exist! Error: object with name 'b_8' does not exist! Error: object with name 'b_9' does not exist! Error: object with name 'b_10' does not exist! Error: object with name 'b_1' does not exist! Error: object with name 'b_11' does not exist! Error: object with name 'b_2' does not exist! Error: object with name 'b_12' does not exist! Error: object with name 'b_3' does not exist! Error: object with name 'b_4' does not exist! Error: object with name 'b_5' does not exist! Error: object with name 'b_6' does not exist!

Discussion

2 archived replies

Posts are displayed in their archived order.

01Commenter-1

Pay attention that you are giving shape variable names to vdisplay, so that this command called inside procedure should be able to find provided variables by name.

I don't think you need upvar here - just call vdisplay with uplevel #0 vdisplay prefix to ensure that it will work at global state. The 3d viewer in DRAW is a global object anyway.

Take a look at drawObject in Samples.

02Author

Thanks. This was a pure skill issue relating to how TCL works. Thanks for explaining the uplevel #0 prefix for commands that need to touch the global scope.