API Guide > Code Examples > Example 6: C++ Code Generator
Example 6: C++ Code Generator
This example is a VB.NET application that uses PTC Mathcad Prime to generate C++ code. The application is comprised of two parts:
The first part launches PTC Mathcad Prime, loads a worksheet, and pushes data values to the running worksheet. This occurs in the callbacks for Form1.vb.
The second part takes a PTC Mathcad Prime equation in the underlying syntax via the clipboard, parses it with the parseme() function, and generates a C++ equation. This latter half is unrelated to the PTC Mathcad Prime API. The callback for Button2 then uses .NET StreamWriters to generate an entire C++ program.
To run this application:
1. Download the example here: VB.NET_code_generation.
2. to run the example, execute GenCode.exe. The GenCode dialog window opens:
3. Click File > Open to open the curve sheet.mcdx worksheet.
4. In the open worksheet, note the three input regions that contain the definitions of coeff1, coeff2, and coeff3:
5. In the GenCode dialog window, change the values of coeff1, coeff2, and coeff3.
6. Click Update Mathcad Prime with new coefficients to send the new data to the worksheet.
7. In the worksheet:
a. Watch the three input areas, coeff1, coeff2, and coeff3 take on the new values you specified in the GenCode dialog window.
b. Watch the change in the graph as the coefficients of function f(x) update.
c. Scroll down to locate the fourth Input region that contains the definition of myfunc(x), then copy the expression to the right of the symbolic evaluation operator.
8. In the GenCode dialog window, paste the copied expression into the STEP 3 text box. The Ruby syntax representation is displayed.
9. Click Convert the equation above from Ruby syntax to C++. A Notepad window entitled ConsoleApplication1.cpp opens showing the equivalent C++ code. Save or close the file.
10. Repeat steps 5 through 9 and ensure the newly specified coefficients are visible in the three input regions, and that the definition of myfunc(x) changes accordingly.
11. In PTC Mathcad Prime, click File > Close, and then in GenCode dialog window click File > Exit.
Was this helpful?