From ff19c85ceb96ca64b675987b4d5036a367df8ab9 Mon Sep 17 00:00:00 2001 From: leehsiang Date: Tue, 5 Mar 2024 16:13:34 +0800 Subject: [PATCH] feat: add python interface --- src/CMakeLists.txt | 2 + src/pygerber-parser/CMakeLists.txt | 7 ++++ src/pygerber-parser/pygerber-parser.cpp | 54 +++++++++++++++++++++++++ src/pygerber-parser/pygerber-parser.py | 6 +++ 4 files changed, 69 insertions(+) create mode 100644 src/pygerber-parser/CMakeLists.txt create mode 100644 src/pygerber-parser/pygerber-parser.cpp create mode 100644 src/pygerber-parser/pygerber-parser.py diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 221ce5c..33da117 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,3 +10,5 @@ target_include_directories( target_link_libraries(gerber_engine PUBLIC gerber_parser Qt6::Core Qt6::Widgets Qt6::Gui) source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${Engine}) + +add_subdirectory(pygerber-parser) diff --git a/src/pygerber-parser/CMakeLists.txt b/src/pygerber-parser/CMakeLists.txt new file mode 100644 index 0000000..c6836b2 --- /dev/null +++ b/src/pygerber-parser/CMakeLists.txt @@ -0,0 +1,7 @@ +find_package(Qt6 COMPONENTS Core Widgets Gui OpenGLWidgets REQUIRED) +set(CMAKE_AUTORCC ON) + +file(GLOB Source ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.h) + +add_library(pygerber-parser SHARED ${Source}) +target_link_libraries(pygerber-parser PRIVATE gerber_engine Qt6::Gui Qt6::Widgets Qt6::Core) \ No newline at end of file diff --git a/src/pygerber-parser/pygerber-parser.cpp b/src/pygerber-parser/pygerber-parser.cpp new file mode 100644 index 0000000..c7249b5 --- /dev/null +++ b/src/pygerber-parser/pygerber-parser.cpp @@ -0,0 +1,54 @@ +#include +#include + +#include + +#include "gerber_parser/bound_box.h" +#include "gerber/gerber.h" +#include "gerber_parser/gerber_parser.h" +#include "engines/qpainter_engine.h" + +extern "C" { + +typedef struct ImageData_t { + uchar* data; + int size; +}ImageData; + +ImageData RenderGerber2Image(char* gerber_file, int width, int height) +{ + int argc = 1; + char arg1[] = "pygerber-parser"; + char* argv[] = {arg1}; + QApplication app(argc, argv); + + std::string file_path = std::string(gerber_file); + if (file_path.empty()) { + std::cout << "null gerber file path." << std::endl; + return {}; + } + + try { + auto parser = std::make_shared(file_path); + auto gerber = parser->GetGerber(); + auto image = std::make_unique(width, height); + auto engine = std::make_unique( + image.get(), gerber->GetBBox(), 0.00); + engine->RenderGerber(gerber); + + auto img = image->toImage(); + ImageData result; + result.size = img.sizeInBytes(); + auto* data = (uchar*)malloc(result.size); + memcpy(data, img.bits(), result.size); + result.data = data; + + return result; + } + catch (const std::exception& e) { + std::cout << e.what() << std::endl; + return {}; + } +} + +} diff --git a/src/pygerber-parser/pygerber-parser.py b/src/pygerber-parser/pygerber-parser.py new file mode 100644 index 0000000..9f76e90 --- /dev/null +++ b/src/pygerber-parser/pygerber-parser.py @@ -0,0 +1,6 @@ +import ctypes + +lib = ctypes.cdll.LoadLibrary('./libpygerber-parser.so') + +def gerber2image(gerber_file:str): + result = lib.RenderGerber2Image(ctypes.c_char_p(bytes(gerber_file, encoding="utf-8"))) \ No newline at end of file