Skip to content

Commit

Permalink
feat: add python interface
Browse files Browse the repository at this point in the history
  • Loading branch information
leehsiang authored and hsiang-lee committed Mar 5, 2024
1 parent 6bedd0e commit ff19c85
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions src/pygerber-parser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 54 additions & 0 deletions src/pygerber-parser/pygerber-parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <QApplication>
#include <QPixmap>

#include <iostream>

#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<GerberParser>(file_path);
auto gerber = parser->GetGerber();
auto image = std::make_unique<QPixmap>(width, height);
auto engine = std::make_unique<QPainterEngine>(
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 {};
}
}

}
6 changes: 6 additions & 0 deletions src/pygerber-parser/pygerber-parser.py
Original file line number Diff line number Diff line change
@@ -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")))

0 comments on commit ff19c85

Please sign in to comment.