2. Designating Inputs and Outputs in Mathcad - MattWoodhead/MathcadPy GitHub Wiki

The instructions on this page assume you have successfully completed the steps in Installing Mathcadpy, including verifying the API connection.

Create a Mathcad file

Instructions for the use of Mathcad are outside the scope of this Python Module, but we must first create a Mathcad file which contains some calculations with which we want to interact. You can use documentation/Examples/simple_example.mcdx as a starting point (Note: This file was created in Mathcad Prime 5 so will not open in earlier versions). This document has two input values, and an output value calculated from the two inputs. We want to be able to set the two input values from python, and then read the result of the output value using python once it is calculated.

alt text


Designating an input

The first step to prepare the file for interaction with Python is to designate the appropriate variables as inputs. This can be accomplished as follows:

  • Select the "Input/Output" tab in the Mathcad Ribbon.
  • Select the variables you wish to designate as inputs to interact with via Python.
  • Press the "Assign Inputs" button in the ribbon.
  • The variables should now be contained within a blue box and have "In" marking the top left corner

Note: Once a variable is designated as an input, you can no longer manually edit its value in Mathcad.


Designating an Output

Designating an output is almost identical to the method used for designating an input.

  • Select the "Input/Output" tab in the Mathcad Ribbon.
  • Select the variables you wish to designate as Outputs to interact with via Python.
  • Press the "Assign Outputs" button in the ribbon.
  • The variables should now be contained within a blue box and have "Out" marking the top left corner

Creating Input/Output Aliases

The Python script needs to have some means of referencing particular variables within the Mathcad document in order to interact with them correctly. Mathcad provides the input variables with aliases that are close to their worksheet name, but all output variables are automatically designated as "out", "out1", "out2" etc.

It is best to set all variable alises to a sensible name that is easy to type in plain text (i.e. uses ASCII characters without subscript, superscript etc.), and use names that are easy to recognise as matching the associated worksheet variable. This will make your python script and the links to the Mathcad document simpler to understand.

  • Select the "Input/Output" tab in the Mathcad Ribbon.
  • Press the "Show as List" button
  • Select the values in the "Alias" column and edit them as appropriate. The associated variable in the Mathcad document will be highlighted.