C ++ \ IronPython集成示例代码?

时间:2010-10-17 15:38:34

标签: c++ python visual-studio visual-c++ ironpython

我正在寻找 C ++ \ IronPython集成简单示例代码,即在C ++中嵌入python代码,或者更好的Visual C ++程序。

示例代码应包括:如何在语言之间共享对象,如何来回调用函数\方法等...

此外,明确的设置程序也会有所帮助。 (如何在Visual Studio等中包含Python运行时dll ...)

我找到了一个很好的C#\ IronPython here示例,但找不到C ++ \ IronPython示例代码。

2 个答案:

答案 0 :(得分:2)

更新 - 我写了一个更通用的例子(加上包含整个VS2008项目的zip文件的链接)作为我博客上的条目here.

很抱歉,我这么晚才开始游戏,但这里是我将IronPython集成到Visual Studio 2008中的C ++ / cli应用程序中的方法 - .net 3.5。 (实际上是带有C / C ++的混合模式应用程序)

我为地图编写附加组件,使应用程序用Assembly编写。公开API以便可以编写C / C ++加载项。我将C / C ++与C ++ / cli混合在一起。这个例子中的一些元素来自API(例如XPCALL和CmdEnd() - 请忽略它们)

///////////////////////////////////////////////////////////////////////
void XPCALL PythonCmd2(int Result, int Result1, int Result2) 
{
  if(Result==X_OK)
  {
        try
        {
              String^ filename = gcnew String(txtFileName);
              String^ path = Assembly::GetExecutingAssembly()->Location;
              ScriptEngine^ engine = Python::CreateEngine();
              ScriptScope^ scope = engine->CreateScope();
              ScriptSource^ source = engine->CreateScriptSourceFromFile(String::Concat(Path::GetDirectoryName(path), "\\scripts\\", filename + ".py"));

              scope->SetVariable("DrawingList", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingList::typeid));
              scope->SetVariable("DrawingElement", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingElement::typeid));
              scope->SetVariable("DrawingPath", DynamicHelpers::GetPythonTypeFromType(AddIn::DrawingPath::typeid));
              scope->SetVariable("Node", DynamicHelpers::GetPythonTypeFromType(AddIn::Node::typeid));

              source->Execute(scope);
        }
        catch(Exception ^e)
        {
              Console::WriteLine(e->ToString());
              CmdEnd();
        }
  }
  else
  {
        CmdEnd();
  }
}
///////////////////////////////////////////////////////////////////////////////

如您所见,我向IronPython展示了一些对象(DrawingList,DrawingElement,DrawingPath& Node)。这些对象是我创建的C ++ / cli对象,用于向IronPython公开“东西”。

当调用C ++ / cli source-> Execute(scope)行时,唯一的python行 运行是DrawingList.RequestData。

RequestData采用委托和数据类型。

当C ++ / cli代码完成后,它会调用指向的委托 功能“钻石”

在函数菱形中,它通过调用来检索所请求的数据 DrawingList.RequestedValue()对DrawingList.AddElement(dp)的调用添加了 应用程序可视化数据库的新元素。

最后,对DrawingList.EndCommand()的调用告诉FastCAD引擎 清理并结束插件的运行。

import clr
def diamond(Result1, Result2, Result3):

  if(Result1 == 0):

    dp = DrawingPath()
    dp.drawingStuff.EntityColor = 2
    dp.drawingStuff.SecondEntityColor = 2

    n = DrawingList.RequestedValue()

    dp.Nodes.Add(Node(n.X-50,n.Y+25))
    dp.Nodes.Add(Node(n.X-25,n.Y+50))
    dp.Nodes.Add(Node(n.X+25,n.Y+50))
    dp.Nodes.Add(Node(n.X+50,n.Y+25))
    dp.Nodes.Add(Node(n.X,n.Y-40))

    DrawingList.AddElement(dp)

  DrawingList.EndCommand()

DrawingList.RequestData(diamond, DrawingList.RequestType.PointType)

我希望这就是你要找的东西。

答案 1 :(得分:1)

如果您不需要.NET功能,则可以依赖嵌入Python而不是IronPython。有关详细信息和示例,请参阅Embedding Python in Another Application上的Python文档。如果您不介意依赖BOOST,则可以试用Python integration library