cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Trying to populate string data array using API

acarter-2
1-Visitor

Trying to populate string data array using API

I want to pass a table of data into Mathcad.

Each row of data is like fluid_description (string), fluid_density (real value with units), fluid_viscosity (real value with units)

I have succeeded in sending a matrix of numerical values with and without units, and also individual string values.  But can't seem to find how to send a matrix of strings.

Also worth mentioning that whilst for now I'm using the API, in future it would be intended to migrate this to Mathcad gateway.

Any advice welcome.

ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald III
(To:acarter-2)

Mathcad Prime (3.1) or Mathcad 15?

Can you write the data to a text file (or Excel) and then use Mathcad's file I/O functions to get the data?

Stuart

View solution in original post

20 REPLIES 20

I don't have an answer for you, but as a workaround, could you just populate separate arrays and then combine them in Mathcad?

acarter-2
1-Visitor
(To:MJG)

Whilst researching this problem, I also found Mathcad Tables, which are pretty cool and present the data nicely. 

But again, I don't see any API method for setting Table rows.  However I was able to insert a matrix into a table, which kind of works ... except still no possibility that I can find to create an array of strings.

        [DispId(18)]

        int SetMatrixValue(string aliasArg, IMathcadPrimeMatrix valueArg, string unitsArg);

        [DispId(11)]

        int SetRealValue(string aliasArg, double valueArg, string unitsArg);

        [DispId(36)]

        int SetSExprValue(string aliasArg, string sexpressionArg);

        [DispId(33)]

        int SetStringValue(string aliasArg, string valueArg);

The value parameter for SetMatrixValue is data type IMathcadPrimeMatrix, which has only one value setter method, and this demands value as double:

        [DispId(3)]

        int SetMatrixElement(int rowIndexArg, int columnIndexArg, double valueArg);

Thanks for the suggestion, I am at this stage convinced there is no way to do what I need, the workaround suggested will work if I abandon the string descriptions.

Of course I could send the string description as an integer array of ASCII codes, and convert it in Mathcad... but that's just crazy!

I don't think many people have gotten much use from Prime's API, so I wouldn't expect much help on this site (though, feel free to post just in case).  I've only done a little playing with Prime's API to verify it can do what I need (simple import of a list of variables), but I've decided to stick with Mathcad 15 for now.  Additionally, my current computer only has Prime 1.0, so I can't even look much up.  Sounds like you've got a plan in mind - good luck!

RichardJ
19-Tanzanite
(To:acarter-2)

In Mathcad 15 you can edit the XML of a math region via the API. There's a recent thread here: How to define a variable on MathCad from an Excel., with some example worksheets (Mathcad 15 worksheets, of course). I believe you can do the same in Prime, using MathcadRegion2.MathInterface.XML. You could create a matrix of strings that way.

MJG
18-Opal
18-Opal
(To:MJG)

After re-reading your question, I think I originally misunderstood.  I find it strange that you were able to send a matrix of numerical values but not a matrix of strings.  Can you share what code you are using?  I have not performed this operation, but based on the information in the Developer's Reference, I would not expect there to be much trouble sending a matrix of strings.

Finally, I should have asked: what version of Mathcad are you using?

acarter-2
1-Visitor
(To:MJG)

I'm using Prime 3.1.

Is the Developer's reference available for download somewhere?  I'm just using object browser and guesswork.

Unless you want to pay the hefty fee for the SDK, using the object browser is the only option (& posting here, or course).

RichardJ
19-Tanzanite
(To:acarter-2)

Is the Developer's reference available for download somewhere?  I'm just using object browser and guesswork.

Not exactly. See this thread: NEW API for Mathcad Prime 3.1

RichardJ
19-Tanzanite
(To:MJG)

based on the information in the Developer's Reference

You have the developers reference for Prime?

MJG
18-Opal
18-Opal
(To:MJG)

Sorry for the confusion.  Despite what the time stamps show, I typed this reply prior to getting clarification that you were using Prime.  The Developer's Reference I was referring to is for Mathcad 15 (& prior).

RichardJ
19-Tanzanite
(To:MJG)

How disappointing . I thought you must have won the lottery, and could now afford the SDK

StuartBruff
23-Emerald III
(To:acarter-2)

Mathcad Prime (3.1) or Mathcad 15?

Can you write the data to a text file (or Excel) and then use Mathcad's file I/O functions to get the data?

Stuart

That's something I have not tried yet.  It sounds like it could work.

I use a scriptable button in mathcad 14/15 - each input e.g. tabDesign is a single string variable that contains around 10 to 16k of text (HTML formated results)

button.JPG

Sub PushBtnEvent_Exec(Inputs, Outputs)

   

Dim objFSO, objTextFile, objFile

Const ForWriting = 2

fstr = Array("_Design.htm","_ManufactSpec.htm","_Customer.htm")

tstr = Inputs(4).Value & ") Generated " & Now &"</table></div></body></html>"

If state=1 Then

userFileName = Inputs(0).Value

Set objFSO = CreateObject("Scripting.FileSystemObject")

For i = 0 to 2

  data = Inputs(i+1).Value

  Filename = userFileName & fstr(i)

  Set objFile = objFSO.CreateTextFile(Filename)

  Set objFile = Nothing

  Set objTextFile = objFSO.OpenTextFile(Filename, ForWriting)

  ObjTextFile.WriteLine(data)

  ObjTextFile.WriteLine(tstr)

  ObjTextFile.Close

  Set ObjTextFile = Nothing

Next

Outputs(0).Value = "Output Files Generated " & Now

Set objFSO = Nothing

End If

state = 0

End Sub

In Prime 3 i have had to resort to an excel component - but it has problems - anything with a macro just hangs up as it tries to return to mathcad - the files are written but then the whole shebang crashes

button 2.JPG

here's the screenshot of the excel component with the button that has a macro attached to it

here's the macro that writes the files then returns a timestamp to a cell in excel and that should be passed back to mathcad through an output variable assigned to that cell

Sub OutputButton()

Dim objFSO, objTextFile, objFile

Const ForWriting = 2

fstr = Array("_Design.htm", "_ManufactSpec.htm", "_Customer.htm")

tstr = Cells(1, 5).Value & ") Generated " & Now & "</table></div></body></html>"

userFileName = Cells(1, 1)

Set objFSO = CreateObject("Scripting.FileSystemObject")

For i = 0 To 2

    Data = Cells(1, 2 + i)

    Filename = userFileName & fstr(i)

    Set objFile = objFSO.CreateTextFile(Filename)

    Set objFile = Nothing

    Set objTextFile = objFSO.OpenTextFile(Filename, ForWriting)

    objTextFile.WriteLine (Data)

    objTextFile.WriteLine (tstr)

    objTextFile.Close

    Set objTextFile = Nothing

Next

Set objFSO = Nothing

Cells(1, 6) = "Output generated on " & Now

End Sub

Did you find a solution ao far and do you like to share it?

As far as API calls, I did not find any solution.  The suggestion from Stuart to use text file is good alternative.

RichardJ
19-Tanzanite
(To:acarter-2)

Why does modifying the XML of a region, as I suggested above, not work? You should be able to create any valid math region that way.

I have used that method in previous version of Mathcad, but as far as I can tell the xml of a region is not exposed in Prime API.

RichardJ
19-Tanzanite
(To:acarter-2)

I don't have Prime 3.1 so I can't check, but I believe there is a MathcadRegion2.MathInterface.XML.

tslewis
12-Amethyst
(To:jwehrstedt)

‌there is no solution to pass string values to a matrix as an input value via the mathcad prime API.

I was trying to do the same thing. The mathcad prime 3.1 api only exposes double arguments to matrices .

you can do this via the mathcad 15 API but not prime.

the prime API is much much less feature rich than the mathcad 15 Api.

Another limitation is that output values returned via the prime API always defer  to the default units of the worksheet which is pointless. if I want to return mm as specified in my output value in prime say,  if the sheet default is us it will always return as ft even if I specify any other type of unit such as mm m in km mile etc. Same for any type of unit.

Announcements

Top Tags