开发者

Python interface in C++ Windows Form Application

开发者 https://www.devze.com 2023-03-22 17:16 出处:网络
I am an mechanical engineer with some programing skills and I need some advice from much more experience people from programing. I am writing an optimization platform that will involve (at least at th

I am an mechanical engineer with some programing skills and I need some advice from much more experience people from programing. I am writing an optimization platform that will involve (at least at the beginning) one commercial optimization tool, one commercial design program and one my program written in C++. The platform should prepare case for optimization tool which will run all necessary programs in right order. This part with setting optimization tool from my platform is not a problem because I had some training and they have very good instructions. Also communication with this commercial design program is quite easy because it use OLE interface and has a large number of examples of it. The part with which I have a great problem is providing of a large flexibility to my program. The calculations are mainly based on empirical correlations and I do not want to put them inside a code. I need to take them out to provide easy access in case of new data and new correlations. I am not writing only about changing of some coefficients but about changing of whole equations. For example: let say that something is described with equation X = A*Y^3 + B*Y^2+C*Y+D. ABCD are coefficients and Y is value taken from object in program and X is of course solution. But there can be another correlation for this which can take into account another value from object from program. X = A*Y开发者_如何学Python^2 + B*Z^2 +Y/Z. So I need a way to take out all correlations from my code put them in separete files and be able to create new ones so they will have access to object properties in my code. I was thinking about using Python macros (because in this commercial design program it works weary well, exactly in the way I have described) but the more I reed about this the more I do not know if it will work. I am writing Windows Form application and I use VS 2010. First problem is that is unmanaged and whole program is managed. So I used something like this:

#pragma unmanaged
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#endif
#pragma managed

At least program compile but I have problem with casting unmanaged types of variables from Pyton to managed in C++.

So my first question:

  1. Is it possible to use Python as macro scripting tool in Windows Form application written in VS?

  2. Where can I get more information about this? I read Python docs, and some tutorials in the web but there are all in Win32 console application.

  3. Maybe I should try to use boost? It will help me?

  4. Is there some easy and simple way to do this? ( If someone would show me a simple form application with one button, one textBox and one class with two properties A and B and macro in Python (in separate file) that would return X = A + B * 2 to this text box he would be my hero:)

  5. If answer to first four questions is negative pleas at least give me some hints how I can do this in other way. Thanks!


  1. Yes
  2. The application type in the examples shouldn't really matter. For the most part the Python bindings and supporting code will be the same, it will just differ in how you source the python code that you want to evaluate (i.e config file, or text box inside the application).
  3. Boost has a python module which supports writing objects which are then "automatically" available in python. It is not as powerful as writing it by hand of course but it should be enough for what you need to do. This might be a good place to start, see : http://www.boost.org/doc/libs/1_47_0/libs/python/doc/tutorial/doc/html/index.html
0

精彩评论

暂无评论...
验证码 换一张
取 消