example using C++ API of FreeCAD in external application

This commit is contained in:
wmayer
2018-08-09 22:12:18 +02:00
parent e532f3fd88
commit 2d2aaac130
4 changed files with 245 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
project(MyPlugin)
cmake_minimum_required(VERSION 3.5)
# Qt5
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
# Boost
find_package(Boost COMPONENTS signals REQUIRED)
# Coin3d
#find_package(Coin3D REQUIRED)
# Python
find_package(PythonInterp REQUIRED)
set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR})
IF (NOT DEFINED PYTHON_VERSION_STRING)
find_package(PythonLibs REQUIRED)
ELSE (NOT DEFINED PYTHON_VERSION_STRING)
find_package(PythonLibs ${PYTHON_VERSION_STRING} EXACT)
ENDIF(NOT DEFINED PYTHON_VERSION_STRING)
# FreeCAD
set(FREECAD_SRC_DIR ${CMAKE_SOURCE_DIR}/../../../.. CACHE PATH "FreeCAD sources")
set(FREECAD_BIN_DIR ${CMAKE_SOURCE_DIR}/../../../.. CACHE PATH "FreeCAD binaries")
set(FREECAD_LIBPACK_DIR ${CMAKE_SOURCE_DIR} CACHE PATH "Directory of the FreeCAD LibPack")
# -----------------------------------------------------------------------------
add_definitions(-DBOOST_ALL_DYN_LINK)
include_directories(
${Boost_INCLUDE_DIRS}
#${COIN3D_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
${Qt5Core_INCLUDE_DIRS}
${Qt5Gui_INCLUDE_DIRS}
${Qt5Widgets_INCLUDE_DIRS}
${FREECAD_SRC_DIR}
"${FREECAD_LIBPACK_DIR}/include"
)
link_directories(${FREECAD_LIBPACK_DIR}/lib)
link_directories(${FREECAD_BIN_DIR})
# -----------------------------------------------------------------------------
set(MyPlugin_SRCS
main.cpp
)
set(MyPlugin_LIBS
${Boost_LIBRARIES}
#${COIN3D_LIBRARIES}
${PYTHON_LIBRARIES}
${Qt5Core_LIBRARIES}
${Qt5Gui_LIBRARIES}
${Qt5Widgets_LIBRARIES}
)
add_executable(MyPlugin WIN32 ${MyPlugin_SRCS})
target_link_libraries(MyPlugin ${MyPlugin_LIBS})
# -----------------------------------------------------------------------------
set(FreeCADPlugin_SRCS
plugin.cpp
)
set(FreeCADPlugin_LIBS
${Boost_LIBRARIES}
#${COIN3D_LIBRARIES}
${PYTHON_LIBRARIES}
${Qt5Core_LIBRARIES}
${Qt5Gui_LIBRARIES}
${Qt5Widgets_LIBRARIES}
)
if (MSVC)
set(FreeCADPlugin_LIBS
${FreeCADPlugin_LIBS}
debug FreeCADApp_d
debug FreeCADBase_d
debug FreeCADGui_d
#debug Mesh_d
optimized FreeCADApp
optimized FreeCADBase
optimized FreeCADGui
#optimized Mesh
)
else()
set(FreeCADPlugin_LIBS
${FreeCADPlugin_LIBS}
FreeCADApp
FreeCADBase
FreeCADGui
#Mesh
)
endif()
add_library(FreeCADPlugin SHARED ${FreeCADPlugin_SRCS})
target_link_libraries(FreeCADPlugin ${FreeCADPlugin_LIBS})

View File

@@ -0,0 +1,43 @@
#ifndef __PRECOMPILED__
#define __PRECOMPILED__
#ifdef _MSC_VER
# pragma warning( disable : 4251 )
#endif
#if defined(signals) && defined(QOBJECTDEFS_H) && \
!defined(QT_MOC_CPP)
# undef signals
# define signals signals
#endif
#include <boost/signal.hpp>
namespace boost
{
namespace signalslib = signals;
}
#if defined(signals) && defined(QOBJECTDEFS_H) && \
!defined(QT_MOC_CPP)
# undef signals
// Restore the macro definition of "signals", as it was
// defined by Qt's <qobjectdefs.h>.
# define signals public
#endif
#ifdef FC_OS_WIN32
// cmake generates this define
# if defined (FreeCADPlugin_EXPORTS)
# define FC_PLUGIN_EXPORT __declspec(dllexport)
# else
# define FC_PLUGIN_EXPORT __declspec(dllimport)
# endif
# define MeshExport __declspec(dllimport)
#else // for Linux
# define FC_PLUGIN_EXPORT
# define MeshExport
#endif
#endif

View File

@@ -0,0 +1,47 @@
#include <QApplication>
#include <QDialog>
#include <QPushButton>
#include <QLibrary>
#include <QFileDialog>
QLibrary* freecadPlugin = nullptr;
void loadFreeCAD()
{
if (!freecadPlugin) {
freecadPlugin = new QLibrary("FreeCADPlugin", qApp);
}
if (!freecadPlugin->isLoaded()) {
if (freecadPlugin->load()) {
QFunctionPointer ptr = freecadPlugin->resolve("FreeCAD_init");
if (ptr) {
ptr();
}
}
}
// Load a test file
if (freecadPlugin->isLoaded()) {
typedef void (*TestFunction)(const char*);
TestFunction test = (TestFunction)freecadPlugin->resolve("FreeCAD_test");
if (test) {
QString file = QFileDialog::getOpenFileName();
if (!file.isEmpty())
test(file.toUtf8());
}
}
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QDialog dlg;
QPushButton* button = new QPushButton(&dlg);
button->setGeometry(QRect(140, 110, 90, 23));
button->setText("Load FreeCAD");
QObject::connect(button, &QPushButton::clicked, &loadFreeCAD);
dlg.show();
return app.exec();
}

View File

@@ -0,0 +1,53 @@
#include "PreCompiled.h"
#include <QApplication>
#include <QMainWindow>
#include <QMessageBox>
#include <QVector>
#include <Base/Factory.h>
#include <Base/Interpreter.h>
#include <App/Application.h>
#include <Gui/Application.h>
#include <Gui/MainWindow.h>
PyMODINIT_FUNC FreeCAD_init()
{
static QVector<char *> argv;
#if defined(_DEBUG)
argv << "FreeCADApp_d.dll" << 0;
#else
argv << "FreeCADApp.dll" << 0;
#endif
App::Application::Config()["RunMode"] = "Gui";
App::Application::Config()["Console"] = "0";
App::Application::Config()["ExeVendor"] = "FreeCAD";
App::Application::Config()["SplashScreen"] = "freecadsplash";
App::Application::init(1, argv.data());
Gui::Application::initApplication();
Gui::Application* app = new Gui::Application(true);
Gui::MainWindow* mw = new Gui::MainWindow(qApp->activeWindow());
mw->show();
app->initOpenInventor();
app->runInitGuiScript();
}
/* A test function for the plugin to load a mesh and call "getVal()" */
PyMODINIT_FUNC FreeCAD_test(const char* path)
{
try
{ // Use FreeCADGui here, not Gui
Base::Interpreter().runString("FreeCADGui.activateWorkbench(\"MeshWorkbench\")");
Base::Interpreter().runString("import Mesh");
Base::Interpreter().runStringArg("Mesh.insert(u\"%s\", \"%s\")", path, "Document");
}
catch (const Base::Exception& e)
{
QMessageBox::warning(0, "Exception", e.what());
}
}