Prototype first working version

This commit is contained in:
Magne Sjaastad 2025-01-18 15:27:25 +01:00
parent e951ce9788
commit a1a9812d24
5 changed files with 283 additions and 0 deletions

View File

@ -1094,6 +1094,8 @@ install(TARGETS extract-projectfile-versions
DESTINATION ${RESINSIGHT_INSTALL_FOLDER}
)
add_subdirectory(native-python)
# ##############################################################################
# Visual Studio : Create the ruleset file to be used by Static Code Analysis
# https://stackoverflow.com/questions/75031903/how-to-enable-static-analysis-with-custom-ruleset-in-msvc-via-cmakelists-txt

View File

@ -0,0 +1,77 @@
cmake_minimum_required(VERSION 3.15)
project(example_project)
# Find pybind11
find_package(pybind11 REQUIRED)
# Add the module
pybind11_add_module(msjmodule example.cpp)
set(LINK_LIBRARIES
${THIRD_PARTY_LIBRARIES}
${OPENGL_LIBRARIES}
${QT_LIBRARIES}
${OPM_LIBRARIES}
${APP_FWK_LIBRARIES}
${VIZ_FWK_LIBRARIES}
ApplicationLibCode
Commands
RigGeoMechDataModel
RifGeoMechFileInterface
)
#if(RESINSIGHT_ENABLE_GRPC)
# list(APPEND LINK_LIBRARIES GrpcInterface)
#endif()
if(RESINSIGHT_USE_ODB_API)
list(APPEND LINK_LIBRARIES RifOdbReader)
endif()
target_link_libraries(
msjmodule PUBLIC ${LINK_LIBRARIES} ${EXTERNAL_LINK_LIBRARIES}
)
if(MSVC)
add_custom_command(
TARGET msjmodule
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<TARGET_RUNTIME_DLLS:msjmodule>
$<TARGET_FILE_DIR:msjmodule>
COMMAND_EXPAND_LISTS
)
endif(MSVC)
# ##############################################################################
# Copy required Dll/.so to output folder
# ##############################################################################
# create an empty library target that will be used to copy files to the build
# folder
add_library(ResInsightDummyTestTarget2 EXCLUDE_FROM_ALL empty.cpp)
set_property(
TARGET ResInsightDummyTestTarget2 PROPERTY FOLDER "FileCopyTargetsTest"
)
# create a custom target that copies the files to the build folder
foreach(riFileName ${RI_FILENAMES})
list(
APPEND
copyCommands
COMMAND
${CMAKE_COMMAND}
-E
copy_if_different
${riFileName}
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/native-python
)
endforeach()
add_custom_target(PreBuildFileCopyTest2 ${copyCommands})
set_property(TARGET PreBuildFileCopyTest2 PROPERTY FOLDER "FileCopyTargetsTest")
add_dependencies(msjmodule PreBuildFileCopyTest2)
install(TARGETS msjmodule DESTINATION .)

21
native-python/empty.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <pybind11/pybind11.h>
#include "Riaapplication.h"
int add(int a, int b) {
auto app = RiaApplication::instance();
app->closeProject();
return a + b;
}
// Create a Python module named `example` and bind the `add` function.
PYBIND11_MODULE(example, m) {
m.doc() = "Example module created using Pybind11"; // Module docstring
m.def("add", &add, "A function that adds two numbers");
}

155
native-python/example.cpp Normal file
View File

@ -0,0 +1,155 @@
#include <pybind11/pybind11.h>
#include <QCoreApplication>
#include "RiaApplication.h"
#include "../../ApplicationExeCode/RiaMainTools.h"
#include "RiaQuantityInfoTools.h"
#include "cafCmdFeatureManager.h"
#include "RiaRegressionTestRunner.h"
class RiaPythonApplication : public QCoreApplication, public RiaApplication
{
public:
RiaPythonApplication(int& argc, char** argv)
: QCoreApplication(argc, argv)
, RiaApplication()
{
installEventFilter(this);
}
ApplicationStatus handleArguments(gsl::not_null<cvf::ProgramOptions*> progOpt) override;
void showFormattedTextInMessageBoxOrConsole(const QString& errMsg) override;
protected:
void invokeProcessEvents(QEventLoop::ProcessEventsFlags flags = QEventLoop::AllEvents) override;
void onProjectOpened() override;
void onProjectOpeningError(const QString& errMsg) override;
void onProjectClosed() override;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaApplication::ApplicationStatus RiaPythonApplication::handleArguments(gsl::not_null<cvf::ProgramOptions*> progOpt)
{
return ApplicationStatus::KEEP_GOING;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPythonApplication::showFormattedTextInMessageBoxOrConsole(const QString& errMsg)
{
throw std::logic_error("The method or operation is not implemented.");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPythonApplication::invokeProcessEvents(QEventLoop::ProcessEventsFlags flags /*= QEventLoop::AllEvents */)
{
throw std::logic_error("The method or operation is not implemented.");
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPythonApplication::onProjectOpened()
{
return;
// throw std::logic_error( "The method or operation is not implemented." );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPythonApplication::onProjectOpeningError(const QString& errMsg)
{
return;
// throw std::logic_error( "The method or operation is not implemented." );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaPythonApplication::onProjectClosed()
{
return;
// throw std::logic_error( "The method or operation is not implemented." );
}
class PythonAppSingleton
{
public:
PythonAppSingleton()
{
// Create feature manager before the application object is created
caf::CmdFeatureManager::createSingleton();
RiaRegressionTestRunner::createSingleton();
caf::PdmDefaultObjectFactory::createSingleton();
RiaQuantityInfoTools::initializeSummaryKeywords();
int argc = 0;
m_app.reset( new RiaPythonApplication(argc, nullptr));
}
static PythonAppSingleton* instance()
{
static PythonAppSingleton theInstance;
return &theInstance;
}
private:
std::unique_ptr<RiaPythonApplication> m_app;
};
int add(int a, int b) {
return a + b;
}
void init_singletons() {
// RiaPythonApplication::ensureInstanceIsCreated();
}
std::string octave_path() {
std::string text;
text = "i was here";
//return text;
PythonAppSingleton::instance();
auto app = RiaApplication::instance();
if (app)
{
app->initialize();
text = app->octavePath().toStdString();
}
return text;
}
// Create a Python module named `example` and bind the `add` function.
PYBIND11_MODULE(msjmodule, m) {
m.doc() = "Example module created using Pybind11"; // Module docstring
m.def("init_singletons", &init_singletons, "Init application singletons");
m.def("add", &add, "A function that adds two numbers");
m.def("octave_path", &octave_path, "Read out Octave Path");
}

View File

@ -0,0 +1,28 @@
# test_example.py
import sys
sys.path.append('../../build/native-python') # Replace with the actual path
try:
import msjmodule
except ImportError as e:
print("Error importing module:", e)
try:
# Test the `add` function
result = msjmodule.add(3, 5)
print(f"Result of add(3, 5): {result}") # Expected output: 8
# Check edge cases
print(f"add(0, 0): {msjmodule.add(0, 0)}") # Expected output: 0
#msjmodule.init_singletons()
#print(f"add(-1, 1): {msjmodule.add(-1, 1)}") # Expected output: 0
text = msjmodule.octave_path()
print(f"octave_path(): {text}") # Expected output: "Singletons
print(f"add(100, 200): {msjmodule.add(100, 200)}") # Expected output: 300
except RuntimeError as e:
print("Error executing module:", e)