Program with Tcl/Tk Command Tutorials

Learn more about programming with Tcl/Tk commands with this tutorial.

  1. Make a proc:
    • A proc is a function that, when called, will run through all the commands made within it.
    • The two words within the { } are the inputs (if there are inputs needed). These inputs will be used in the function. For example, the fileName input would be the file used in the function.
      • When calling the values of these inputs within the function, use a $ symbol followed by the variable name.
    • The last { on the proc line denotes the beginning of the code within the function. There must be another } at the very end of the written code to end the proc.
    proc ShowCurves {fileName amount}  {
  2. hwi is the first parent handle, which connects every API together.
    • First, get the session handle from the hwi handle, and then you can delve deeper to find the APIs needed.
    • For the basics in understanding APIs, read Program with APIs for Beginners.
    hwi GetSessionHandle sess
  3. Get the project handle.
    sess GetProjectHandle proj
  4. Open up the data file handle of the file that is input when the function is called.
    • The $ symbol tells the program to use the value of the variable, in this case, the variable is the input fileName.
    sess GetDataFileHandle data $fileName
  5. Each of the “foreach” loops runs through the input data.
    • For example, the first "foreach" loops through each value in the datatype list of the data file.
    • The same occurs for the request list and the component list.
    • This way, every possible combination of datatype, request, and component are used.
    foreach value [data GetDataTypeList false] {
                     foreach request [data GetRequestList $value false] {
                             foreach component [data GetComponentList $value false] {
  6. Next, get the correct page, window, and client, and set the correct client type before getting its handle.
    • This adds a curve for every datatype, request, and component, and gets the handle of the curve that was just made.
    proj GetPageHandle p1 1
    p1 GetWindowHandle w1 1
    w1 SetClientType Plot
    w1 GetClientHandle plot
  7. By setting the added curve as cn, you can grab the handle each time without knowing exactly which curve number it is.
    • This code once again uses the $ symbol, as to use the value of the variable cn.
    set cn [plot AddCurve]
    plot GetCurveHandle c1 $cn
  8. Set up the curve based on the file. It uses the datatype, request, and component set in the loop.
    c1 GetVectorHandle xv x
    xv SetType File
    xv SetFilename $fileName
    c1 GetVectorHandle yv y
    yv SetType File
    yv SetFilename $fileName
    yv SetDataType $value
    yv SetRequest $request
    yv SetComponent $component
  9. Draw the cursor after recalculating and scaling it.
    plot Recalculate
    plot Autoscale
    plot Draw
  10. Find the min and max Y values and set them to the specific variables maxY and minY.
    c1 AddCursor
    c1 GetCursorHandle cur 1
    cur FindGlobalMax y
    set maxY [cur GetY]
    cur FindGlobalMin y
    set minY [cur GetY]
  11. Find the difference between the min and max y value by taking the absolute value of the maxY minus the minY.
    set diff [expr abs($maxY-$minY)]
  12. If the difference is less than the amount you input, the curve’s visibility will be turned off.
    if {$diff < $amount} {
                c1 SetVisibility false
    }
  13. Release all the handles so they can be grabbed once again when the loop begins again.
    p1 ReleaseHandle
    w1 ReleaseHandle
    plot ReleaseHandle
    c1 ReleaseHandle
    cur ReleaseHandle
    xv ReleaseHandle
    yv ReleaseHandle
  14. Once everything is done, the loops are closed and the proc is closed as well.
    }
             }
     }
    }