📄 rs_python_wrappers.cpp
字号:
/***************************************************************************** $Id**** Python language bindings for QCad II**** Copyright (C) 2003 Markus Meyer <meyer@mesw.de>**** Permission to copy, use, modify, sell and distribute this file is granted** provided this copyright notice is not removed or altered.** This software is provided "as is" without express or implied** warranty, and with no claim as to its suitability for any purpose.*****************************************************************************//** * TODO: * - Complete block support * - Support for hatches, dimensions, text, solids * - Support for user interactions * - Support for more than one document */#ifdef RS_OPT_PYTHON#include <boost/python.hpp>using namespace boost::python;#include "rs_python_wrappers.h"#include "rs_python.h"#include "rs.h"#include "rs_arc.h"#include "rs_atomicentity.h"#include "rs_block.h"#include "rs_blocklist.h"#include "rs_circle.h"#include "rs_color.h"#include "rs_constructionline.h"#include "rs_document.h"#include "rs_ellipse.h"#include "rs_entitycontainer.h"#include "rs_entity.h"#include "rs_flags.h"#include "rs_graphic.h"#include "rs_image.h"#include "rs_insert.h"#include "rs_layer.h"#include "rs_layerlist.h"#include "rs_line.h"#include "rs_pen.h"#include "rs_point.h"#include "rs_polyline.h"#include "rs_vector.h"/* Global root functions */RS_Graphic* currentGraphic() { return RS_PYTHON->getGraphic(); } /* more to be added later (access to global properties, all documents, creation of new documents, ... *//* To/From Python string conversion logic for string management */namespace RS_String_Python_Conversions { namespace { struct RS_String_to_python_str { static PyObject* convert(RS_String const& s) { return boost::python::incref(boost::python::object((const char*)s).ptr()); } }; struct RS_String_from_python_str { RS_String_from_python_str() { boost::python::converter::registry::push_back( &convertible, &construct, boost::python::type_id<RS_String>()); } static void* convertible(PyObject* obj_ptr) { if (!PyString_Check(obj_ptr)) return 0; return obj_ptr; } static void construct( PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data) { const char* value = PyString_AsString(obj_ptr); if (!value) boost::python::throw_error_already_set(); void* storage = ( (boost::python::converter::rvalue_from_python_storage<RS_String>*) data)->storage.bytes; new (storage) RS_String(value); data->convertible = storage; } }; void registerConversions() { using namespace boost::python; boost::python::to_python_converter< RS_String, RS_String_to_python_str>(); RS_String_from_python_str(); } }}/* Transfer of ownership is done by using auto pointers *//* These are the helper functions needed for this mechanism */#define TRANSFER_OWNERSHIP_FUNCTION(fname, container, addfunc, entity) \ void fname(container& cont, std::auto_ptr<entity> obj) \ { cont.addfunc(obj.get()); obj.release(); }TRANSFER_OWNERSHIP_FUNCTION(RS_Graphic_addLayer, RS_Graphic, addLayer, RS_Layer)TRANSFER_OWNERSHIP_FUNCTION(RS_LayerList_add, RS_LayerList, add, RS_Layer)#define ADDVERTEX_FUNCTION(fname, entity) \ TRANSFER_OWNERSHIP_FUNCTION(fname, RS_EntityContainer, addEntity, entity)ADDVERTEX_FUNCTION(RS_EntityContainer_addArc, RS_Arc)ADDVERTEX_FUNCTION(RS_EntityContainer_addBlock, RS_Block)ADDVERTEX_FUNCTION(RS_EntityContainer_addCircle, RS_Circle)ADDVERTEX_FUNCTION(RS_EntityContainer_addConstructionLine, RS_ConstructionLine)ADDVERTEX_FUNCTION(RS_EntityContainer_addEllipse, RS_Ellipse)ADDVERTEX_FUNCTION(RS_EntityContainer_addImage, RS_Image)ADDVERTEX_FUNCTION(RS_EntityContainer_addLine, RS_Line)ADDVERTEX_FUNCTION(RS_EntityContainer_addPoint, RS_Point)ADDVERTEX_FUNCTION(RS_EntityContainer_addPolyline, RS_Polyline)/* Overloaded functions helpers */void (RS_LayerList::*RS_LayerList_activate_string)(const RS_String&) = &RS_LayerList::activate;void (RS_LayerList::*RS_LayerList_activate_layer)(RS_Layer*) = &RS_LayerList::activate;void (RS_LayerList::*RS_LayerList_toggle_string)(const RS_String&) = &RS_LayerList::toggle;void (RS_LayerList::*RS_LayerList_toggle_layer)(const RS_String&) = &RS_LayerList::toggle;void (RS_Graphic::*RS_Graphic_toggleLayer_string)(const RS_String&) = &RS_Graphic::toggleLayer;void (RS_Graphic::*RS_Graphic_toggleLayer_layer)(RS_Layer*) = &RS_Graphic::toggleLayer;void (RS_Entity::*RS_Entity_setLayer_string)(const RS_String&) = &RS_Entity::setLayer;void (RS_Entity::*RS_Entity_setLayer_layer)(RS_Layer*) = &RS_Entity::setLayer;/** * The main python module */ BOOST_PYTHON_MODULE(qcad){ /* Initialization code */ RS_String_Python_Conversions::registerConversions(); /* Unbound functions */ def("currentGraphic", currentGraphic, return_value_policy<reference_existing_object>()); /* Enums */ enum_<RS2::Flags>("Flag") .value("Undone", RS2::FlagUndone) .value("Visible", RS2::FlagVisible) .value("ByLayer", RS2::FlagByLayer) .value("ByBlock", RS2::FlagByBlock) .value("Frozen", RS2::FlagFrozen) .value("DefFrozen", RS2::FlagDefFrozen) .value("Locked", RS2::FlagLocked) .value("Invalid", RS2::FlagInvalid) .value("Selected", RS2::FlagSelected) .value("Closed", RS2::FlagClosed) .value("Temp", RS2::FlagTemp) .value("Processed", RS2::FlagProcessed) .value("Selected1", RS2::FlagSelected1) .value("Selected2", RS2::FlagSelected2) ; enum_<RS2::VariableType>("VariableType") .value("String", RS2::VariableString) .value("Int", RS2::VariableInt) .value("Double", RS2::VariableDouble) .value("Vector", RS2::VariableVector) .value("Void", RS2::VariableVoid) ; enum_<RS2::EntityType>("EntityType") .value("Unknown", RS2::EntityUnknown) .value("Container", RS2::EntityContainer) .value("Block", RS2::EntityBlock) .value("FontChar", RS2::EntityFontChar) .value("Insert", RS2::EntityInsert) .value("Graphic", RS2::EntityGraphic) .value("Point", RS2::EntityPoint) .value("Line", RS2::EntityLine) .value("Polyline", RS2::EntityPolyline) .value("Vertex", RS2::EntityVertex) .value("Arc", RS2::EntityArc) .value("Circle", RS2::EntityCircle) .value("Ellipse", RS2::EntityEllipse) .value("Solid", RS2::EntitySolid) .value("ConstructionLine", RS2::EntityConstructionLine) .value("Text", RS2::EntityText) .value("DimAligned", RS2::EntityDimAligned) .value("DimLinear", RS2::EntityDimLinear) .value("DimRadial", RS2::EntityDimRadial) .value("DimDiametric", RS2::EntityDimDiametric) .value("DimAngular", RS2::EntityDimAngular) .value("DimLeader", RS2::EntityDimLeader) .value("Hatch", RS2::EntityHatch) .value("Image", RS2::EntityImage) ; enum_<RS2::LineType>("LineType") .value("NoPen", RS2::NoPen) .value("SolidLine", RS2::SolidLine) .value("DotLine", RS2::DotLine) .value("DotLine2", RS2::DotLine2) .value("DotLineX2", RS2::DotLineX2) .value("DashLine", RS2::DashLine) .value("DashLine2", RS2::DashLine2) .value("DashLineX2", RS2::DashLineX2) .value("DashDotLine", RS2::DashDotLine) .value("DashDotLine2", RS2::DashDotLine2) .value("DashDotLineX2", RS2::DashDotLineX2) .value("DivideLine", RS2::DivideLine) .value("DivideLine2", RS2::DivideLine2) .value("DivideLineX2", RS2::DivideLineX2) .value("CenterLine", RS2::CenterLine) .value("CenterLine2", RS2::CenterLine2) .value("CenterLineX2", RS2::CenterLineX2) .value("BorderLine", RS2::BorderLine) .value("BorderLine2", RS2::BorderLine2) .value("BorderLineX2", RS2::BorderLineX2) .value("ByLayer", RS2::LineByLayer) .value("ByBlock", RS2::LineByBlock) ; enum_<RS2::LineWidth>("LineWidth") .value("Width00", RS2::Width00) .value("Width01", RS2::Width01) .value("Width02", RS2::Width02) .value("Width03", RS2::Width03) .value("Width04", RS2::Width04) .value("Width05", RS2::Width05) .value("Width06", RS2::Width06) .value("Width07", RS2::Width07) .value("Width08", RS2::Width08) .value("Width09", RS2::Width09) .value("Width10", RS2::Width10) .value("Width11", RS2::Width11) .value("Width12", RS2::Width12) .value("Width13", RS2::Width13) .value("Width14", RS2::Width14) .value("Width15", RS2::Width15) .value("Width16", RS2::Width16) .value("Width17", RS2::Width17) .value("Width18", RS2::Width18) .value("Width19", RS2::Width19) .value("Width20", RS2::Width20) .value("Width21", RS2::Width21) .value("Width22", RS2::Width22) .value("Width23", RS2::Width23) .value("ByLayer", RS2::WidthByLayer) .value("ByBlock", RS2::WidthByBlock) .value("Default", RS2::WidthDefault) ; /* "Small" classes */ class_<RS_Flags>("Flags") .def(init<int>()) .add_property("flags", &RS_Flags::getFlags, &RS_Flags::setFlags) .def("resetFlags", &RS_Flags::resetFlags) .def("setFlag", &RS_Flags::setFlag) .def("delFlag", &RS_Flags::delFlag) .def("toggleFlag", &RS_Flags::toggleFlag) .def("getFlag", &RS_Flags::getFlag) ; class_<RS_Color, bases<RS_Flags> >("Color") .def(init<int, int, int>()) .def(init<int>()) .def("stripFlags", &RS_Color::stripFlags) .add_property("byLayer", &RS_Color::isByLayer) .add_property("byBlock", &RS_Color::isByBlock) ; class_<RS_Vector>("Vector") .def(init<double, double, optional<double> >()) .def("set", &RS_Vector::set) .def("setPolar", &RS_Vector::setPolar) .def("distanceTo", &RS_Vector::distanceTo) .def("angle", &RS_Vector::angle) .def("angleTo", &RS_Vector::angleTo) .def("magnitude", &RS_Vector::magnitude) .def("move", &RS_Vector::move) .def_readwrite("x", &RS_Vector::x) .def_readwrite("y", &RS_Vector::y) .def_readwrite("z", &RS_Vector::z) .def_readwrite("valid", &RS_Vector::valid) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -