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

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Call Mathcad from Fortran

PaulFrieze
1-Newbie

Call Mathcad from Fortran

Dear all,

does anyone know if it is possible to call a Mathcad worksheet from a Fortran subroutine?

Any example or tip will be really appreciated.

Thank you very much for your help.

Mirella

(Paul's colleague)

14 REPLIES 14
MikeArmstrong
5-Regular Member
(To:PaulFrieze)

Mike,

thank you very much for your answer, but this is not exactly what I was looking for.

Probably I didn't give enough details in my previous message.

I have prepared a Fortran subroutine that calculates some input values for a Mathcad spreadsheet. The Mathcad spreasheet use these input values and give an output value that must be used in the Fortran subroutine for further calculations. This is the reason why I need to "call" somehow my Mathcad spreadsheet within my Fortran subroutine; but I am not sure if this is possible.

The other simple solution is to write again my Mathcad spreadsheet in Fortran (as you suggested) but this is what I am trying to avoid.

Thank you

Best regards

Mirella

MikeArmstrong
5-Regular Member
(To:PaulFrieze)

I have prepared a Fortran subroutine that calculates some input values for a Mathcad spreadsheet. The Mathcad spreasheet use these input values and give an output value that must be used in the Fortran subroutine for further calculations. This is the reason why I need to "call" somehow my Mathcad spreadsheet within my Fortran subroutine; but I am not sure if this is possible.

It seems to me that this is more of a question for the Fortran software.

You can export out of Mathcad in a number of formats. What format does Fortran accept?

Mike

PhilipOakley
5-Regular Member
(To:PaulFrieze)

You will need to look up, within fortran, how to call a windows API. It shouldn't be hard.

The MathCAD API is listed under "Authors / Developers Help", not the regular F1 help!

Once you have that sussed, you can easily send and recieve data from the mathcad sheet, and hide mathcad while it is happening.

The only point of note is that things sent to mathcad arrive at the very top of the sheet, and things fetched are obtained from the very bottom, so make sure the sheet doesn't 'set/overwrite' the variables you send - i.e. disable the test assignment statements.

I googled "calling windows API from Fortran"

and got

http://www.xlsoft.com/jp/products/intel/cvf/docs/vf-html_e/pg/pgr32api.htm and

http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/win/compiler_f/bldaps_for/win/bldaps_call_winapi.htm

Philip

MikeArmstrong
5-Regular Member
(To:PhilipOakley)

Seems a little long winded.

Does Fortran accept *.txt format?

Mike

Hi Mike,

I can read .txt file from Fortran, have you got any suggestions using text files?

Thank you

Kind regards

Mirella

MikeArmstrong
5-Regular Member
(To:PaulFrieze)

Hi Mike,

I can read .txt file from Fortran, have you got any suggestions using text files?

Maybe I'm getting a little confused. You have just said you have Mathcad accept a *.txt file and out a *.txt file for Fortran, is not your original request?

It sounds like you want this done automatically.

Mike

Hi Philip,

thank you for your reply.

I had a look at the "Author/Developers Help" as you suggested but I could not understand how this API interface can be generated.

My understanding is that I have to write a simple routine in VBA to give to the Mathcad worksheet the location of the input file but I am confused about this as I do not have any experience in VBA. At the moment my Mathcad file is just reading data in a text file at the beginning and writing the results back in another file at the end. How can I write a VBA routine in order to read imput and write output automatically in a simple text file? Have you done this already in the past? After that, should I create then an executable file (.exe) to be run in Fortran or this API will make the trick?

Thank you for your help.

Mirella

RichardJ
19-Tanzanite
(To:PaulFrieze)

VBA is the version of VB that is embedded in MS Office products, so that is not what you want to do.

You need to figure out how to access a COM object in Fortran. For Intel Fortran here's an example that launches Word, and prints the document:

 program COMExample
 use ifwin
 use ifcom
 use ifauto
 implicit none
 ! Variables
 integer(4) word_app
 integer(4) status
 integer(INT_PTR_KIND()) invoke_args
 call COMInitialize(status)
 ! Call GetActiveObject to get a reference to a running MS WORD application
 call COMGetActiveObjectByProgID("Word.Application", word_app, status)
 if (status >= 0) then
 ! Print the active document
 invoke_args = AutoAllocateInvokeArgs()
 call AutoAddArg(invoke_args, "Copies", 2)
 status = AutoInvoke(word_app, "PrintOut", invoke_args)
 call AutoDeallocateInvokeArgs(invoke_args)
 ! Release the reference
 status = COMReleaseObject(word_app)
 end if
 call COMUninitialize()
 end program

I go it from here:

http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/fortran/win/index.htm

You need to either get or create an instance of the Mathcad application, then access (or load, if it is not already loaded) the worksheet, then use the SetValue and GetValue methods to set and get the vaues in the worksheet. Exactly how you do that will depend on your Fortran compiler.

PhilipOakley
5-Regular Member
(To:PaulFrieze)

Mirella,

I hope I have understood your request correctly. I am thinking that you have a main programme in Fortran that does a lot a good stuff, and then part way through you want some of your data to be used in a calculation that has been written in MathCAD, and would be too hard to rewrite in Fortran. The MathcAD result is then used in more calculations inside your Fortran.

Doing this in C is quite easy because there are lots of examples of how to call Windows (that is ask windows to read/write some data to/from some peripheral or application) from C. The task is to get your fortran to ask Windows to do the same.

Windows has a very standardised call structure. You ask to be connected to windows, then ask for a particular program then ask it for the data you want.

The API (application's peripheral interface) for MathCAD details what you can ask for and when. So you ask Windows for a 'handle' to MathCAD, then you ask the mathcad handle(r) to load your worksheet and it gives you back another handle (also known as: an object in the documentation), and you ask that handle(r) [ask the object] to get the next level data, and so on.

The main task is to find a web page that shows you the "easy way" of laying out your fortran. In C the layout is quite easy because its compiler 'understands' the structure of these object calls.

Have a look in C:\Program Files\Mathcad\Mathcad 15\userefi\microsft\sources for examples of some of the style of code structure. These examples are foir adding subroutines to MathCAD, which is not the way you want!.

The section of the Developer's reference you need is the Automation interface section. I think you may already have read this. THe example for the VBA interface is the best one for thinking about how to do it. You will see that it opens up a "compiler" (in this case Visual Basic) and sets up some calls to the external interface (see the links I gave previously for hints for more searching and reading your own fortran compiler manual, especially if it has a .Net library). Essentially the code is compiled (it may look as if it is a fancy interpreter but it reads the text and converts it to computer code!), and you want to do similar things.

I hope I have answered the right question, and described what you might want to do.

If you have access to Matlab, it is very easy to explore the mathcad interface from matlab

In all cases it is worth setting Application=Visible during any development so you can see things happen!!

Philip

The API (application's peripheral interface) for MathCAD details what you can ask for and when. So you ask Windows for a 'handle' to MathCAD, then you ask the mathcad handle(r) to load your worksheet and it gives you back another handle (also known as: an object in the documentation), and you ask that handle(r) [ask the object] to get the next level data, and so on.

Not trying to be pedantic here, but an object is not the same thing as a handle.

The API (application's peripheral interface)

Application Programming Interface

Now I am being pedantic

PhilipOakley
5-Regular Member
(To:RichardJ)

True. But for the un-initiated it is a useful start.

The hidden question is how to get from the "handle" viewpoint to the object viewpoint (which is how to turn the handle to open the right door of the object), i.e. where is the real code that goes off, using the header data that the handle pointed to, to get the specific property value, or access the method etc.

In the discussion, we haven't identified where that 'go fetch' code is located.

e.g. "add the USE DFWIN line" (from one of the web pages).

Philip

The hidden question is how to get from the "handle" viewpoint to the object viewpoint (which is how to turn the handle to open the right door of the object), i.e. where is the real code that goes off, using the header data that the handle pointed to, to get the specific property value, or access the method etc.

The problem is that although the syntax for handling objects is similar in most object oriented languages, Fortran is not an object oriented language. So to access and work with objects you have ugly looking function calls like the ones in the code example I posted. And that is only assuming that the compiler provides functions to access COM objects. If such functions are not provided, then there is no way to call Mathcad and set or get values of worksheet variables.

Personally, I hate Fortran. The last thing I wrote in Fortran was over 25 years ago, and I have never looked back!

PhilipOakley
5-Regular Member
(To:PhilipOakley)

Just googled "calling windows api from fortran". The two links I gave were #1 & #2. There were some links to creating fortran dlls then...

I did find http://compgroups.net/comp.lang.fortran/using-gfortran-to-call-windows-api-functions at #6 just above this thread at #7 !!

Then there was http://www.tek-tips.com/viewthread.cfm?qid=1324708&page=28

Both give examples of the calling structure. I hope this helps.(though I expect that there are still gaps!)

P

Top Tags