diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/CMakeLists.txt | 13 | ||||
-rw-r--r-- | python/GenerateHeaderPy.cpp | 40 | ||||
-rw-r--r-- | python/include/trygvis/kicad/GenerateHeaderPy.h | 45 | ||||
-rw-r--r-- | python/include/trygvis/kicad/python.h | 3 | ||||
-rw-r--r-- | python/kicad_utils_py.cpp | 38 | ||||
m--------- | python/pybind11 | 0 |
6 files changed, 139 insertions, 0 deletions
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt new file mode 100644 index 0000000..2b612b9 --- /dev/null +++ b/python/CMakeLists.txt @@ -0,0 +1,13 @@ +add_subdirectory(pybind11) + +pybind11_add_module(kicad_utils_py kicad_utils_py.cpp + include/trygvis/kicad/python.h + GenerateHeaderPy.cpp include/trygvis/kicad/GenerateHeaderPy.h) +pybind11_enable_warnings(kicad_utils_py) + +target_include_directories(kicad_utils_py PRIVATE include) +target_link_libraries(kicad_utils_py PRIVATE kicad-utils-core) + +install(TARGETS kicad_utils_py + EXPORT kicad_utils_export + LIBRARY DESTINATION lib) diff --git a/python/GenerateHeaderPy.cpp b/python/GenerateHeaderPy.cpp new file mode 100644 index 0000000..1a039a3 --- /dev/null +++ b/python/GenerateHeaderPy.cpp @@ -0,0 +1,40 @@ +#include "trygvis/kicad/GenerateHeaderPy.h" + +namespace trygvis { +namespace kicad { +namespace python { + +using namespace std; + +GenerateHeaderPy::GenerateHeaderPy(const string &ref, nl *netlist) : ref_(ref), netlist_(netlist) { +} + +GenerateHeaderPy::~GenerateHeaderPy() {} + +string GenerateHeaderPy::ref() { + return ref_; +} + +nl *GenerateHeaderPy::netlist() { + if (!netlist_) { + throw domain_error("No current netlist"); + } + + return netlist_; +} + +string GenerateHeaderPy::str() { + return buf_.str(); +} + +void GenerateHeaderPy::print(const string &str) { + buf_ << str; +} + +void GenerateHeaderPy::println(const string &str) { + buf_ << str << endl; +} + +} // namespace python +} // namespace kicad +} // namespace trygvis diff --git a/python/include/trygvis/kicad/GenerateHeaderPy.h b/python/include/trygvis/kicad/GenerateHeaderPy.h new file mode 100644 index 0000000..893b7ed --- /dev/null +++ b/python/include/trygvis/kicad/GenerateHeaderPy.h @@ -0,0 +1,45 @@ +#ifndef KICAD_UTILS_GENERATEHEADEPY_H +#define KICAD_UTILS_GENERATEHEADEPY_H + +#include "trygvis/kicad.h" +#include "pybind11/pybind11.h" +#include <memory> +#include <sstream> + +namespace trygvis { +namespace kicad { +namespace python { + +namespace py = pybind11; + +using nl = trygvis::kicad::netlist::netlist; + +class GenerateHeaderPy { +public: + GenerateHeaderPy(const std::string &ref, nl *netlist); + + virtual ~GenerateHeaderPy(); + + std::string ref(); + + nl *netlist(); + + std::string str(); + + void print(const std::string &str); + + void println(const std::string &str); + +private: + std::string ref_; + + nl *netlist_; + + std::stringstream buf_; +}; + +} // namespace python +} // namespace kicad +} // namespace trygvis + +#endif diff --git a/python/include/trygvis/kicad/python.h b/python/include/trygvis/kicad/python.h new file mode 100644 index 0000000..f2ae816 --- /dev/null +++ b/python/include/trygvis/kicad/python.h @@ -0,0 +1,3 @@ +#pragma once + +#include "trygvis/kicad/GenerateHeaderPy.h" diff --git a/python/kicad_utils_py.cpp b/python/kicad_utils_py.cpp new file mode 100644 index 0000000..6cd3948 --- /dev/null +++ b/python/kicad_utils_py.cpp @@ -0,0 +1,38 @@ +#include "trygvis/kicad/GenerateHeaderPy.h" + +#include "pybind11/stl.h" + +using namespace std; +using namespace trygvis::kicad::netlist; +using namespace trygvis::kicad::python; + +void init_module(py::module &m) { + py::class_<node>(m, "node"). + def_readonly("ref", &node::ref). + def_readonly("pin", &node::pin); + + py::class_<net>(m, "net"). + def_readonly("code", &net::code). + def_readonly("name", &net::name). + def_readonly("nodes", &net::nodes). + def("node_for_ref", &net::node_for_ref, py::return_value_policy::reference); + + py::class_<nl>(m, "netlist"). + def("find_usages_of", &nl::find_usage_of, py::return_value_policy::reference); + + py::class_<GenerateHeaderPy>(m, "GenerateHeader"). + def(py::init<const string &, nl *>()). + def_property_readonly("netlist", &GenerateHeaderPy::netlist). + def_property_readonly("ref", &GenerateHeaderPy::ref). + def_property_readonly("str", &GenerateHeaderPy::str). + def("print", &GenerateHeaderPy::print). + def("println", &GenerateHeaderPy::println); +} + +PYBIND11_PLUGIN(kicad_utils_py) { + py::module kicad_utils("kicad_utils_py"); + + init_module(kicad_utils); + + return kicad_utils.ptr(); +} diff --git a/python/pybind11 b/python/pybind11 new file mode 160000 +Subproject f38f359f96815421f1780c1a676715efd041f1a |