From d9c803ffceee69e99fab0379c67ff71ffd25f3dc Mon Sep 17 00:00:00 2001 From: DeepSOIC Date: Fri, 3 Mar 2017 03:39:51 +0300 Subject: [PATCH] PartDesign: add FeaturePython Opens a window to extend PartDesign with Python --- src/Mod/PartDesign/App/AppPartDesign.cpp | 1 + src/Mod/PartDesign/App/CMakeLists.txt | 3 ++ src/Mod/PartDesign/App/Feature.cpp | 31 +++++++++++++ src/Mod/PartDesign/App/Feature.h | 4 ++ src/Mod/PartDesign/App/FeaturePy.xml | 22 +++++++++ src/Mod/PartDesign/App/FeaturePyImp.cpp | 57 ++++++++++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 src/Mod/PartDesign/App/FeaturePy.xml create mode 100644 src/Mod/PartDesign/App/FeaturePyImp.cpp diff --git a/src/Mod/PartDesign/App/AppPartDesign.cpp b/src/Mod/PartDesign/App/AppPartDesign.cpp index 5722da092..0fb65d14d 100644 --- a/src/Mod/PartDesign/App/AppPartDesign.cpp +++ b/src/Mod/PartDesign/App/AppPartDesign.cpp @@ -85,6 +85,7 @@ PyMOD_INIT_FUNC(_PartDesign) // This function is responsible for adding inherited slots from a type's base class. PartDesign::Feature ::init(); + PartDesign::FeaturePython ::init(); PartDesign::Solid ::init(); PartDesign::DressUp ::init(); PartDesign::FeatureAddSub ::init(); diff --git a/src/Mod/PartDesign/App/CMakeLists.txt b/src/Mod/PartDesign/App/CMakeLists.txt index 3bdc2152c..26a345242 100644 --- a/src/Mod/PartDesign/App/CMakeLists.txt +++ b/src/Mod/PartDesign/App/CMakeLists.txt @@ -20,6 +20,7 @@ include_directories( link_directories(${OCC_LIBRARY_DIR}) generate_from_xml(BodyPy) +generate_from_xml(FeaturePy) set(PartDesign_LIBS @@ -122,6 +123,8 @@ SOURCE_GROUP("Module" FILES ${Module_SRCS}) SET(Python_SRCS BodyPy.xml BodyPyImp.cpp + FeaturePy.xml + FeaturePyImp.cpp ) SOURCE_GROUP("Python" FILES ${Python_SRCS}) diff --git a/src/Mod/PartDesign/App/Feature.cpp b/src/Mod/PartDesign/App/Feature.cpp index 4a4f20a9c..c56137284 100644 --- a/src/Mod/PartDesign/App/Feature.cpp +++ b/src/Mod/PartDesign/App/Feature.cpp @@ -36,9 +36,11 @@ // TODO Cleanup headers (2015-09-04, Fat-Zer) #include #include "App/Document.h" +#include #include "App/OriginFeature.h" #include "Body.h" #include "Feature.h" +#include "FeaturePy.h" #include "Mod/Part/App/DatumFeature.h" #include @@ -137,6 +139,15 @@ const Part::TopoShape Feature::getBaseTopoShape() const { return result; } +PyObject* Feature::getPyObject() +{ + if (PythonObject.is(Py::_None())){ + // ref counter is set to 1 + PythonObject = Py::Object(new FeaturePy(this),true); + } + return Py::new_reference_to(PythonObject); +} + bool Feature::isDatum(const App::DocumentObject* feature) { return feature->getTypeId().isDerivedFrom(App::OriginFeature::getClassTypeId()) || @@ -165,4 +176,24 @@ TopoDS_Shape Feature::makeShapeFromPlane(const App::DocumentObject* obj) return builder.Shape(); } +}//namespace PartDesign + +namespace App { +/// @cond DOXERR +PROPERTY_SOURCE_TEMPLATE(PartDesign::FeaturePython, PartDesign::Feature) +template<> const char* PartDesign::FeaturePython::getViewProviderName(void) const { + return "PartGui::ViewProviderPython"; } +template<> PyObject* PartDesign::FeaturePython::getPyObject(void) { + if (PythonObject.is(Py::_None())) { + // ref counter is set to 1 + PythonObject = Py::Object(new FeaturePythonPyT(this),true); + } + return Py::new_reference_to(PythonObject); +} +/// @endcond + +// explicit template instantiation +template class PartDesignExport FeaturePythonT; +} + diff --git a/src/Mod/PartDesign/App/Feature.h b/src/Mod/PartDesign/App/Feature.h index 70c33cec2..80d4d6c7d 100644 --- a/src/Mod/PartDesign/App/Feature.h +++ b/src/Mod/PartDesign/App/Feature.h @@ -68,6 +68,8 @@ public: /// Returns the BaseFeature property's TopoShape (if any) const Part::TopoShape getBaseTopoShape() const; + virtual PyObject* getPyObject(void); + protected: /** @@ -82,6 +84,8 @@ protected: static TopoDS_Shape makeShapeFromPlane(const App::DocumentObject* obj); }; +typedef App::FeaturePythonT FeaturePython; + } //namespace PartDesign diff --git a/src/Mod/PartDesign/App/FeaturePy.xml b/src/Mod/PartDesign/App/FeaturePy.xml new file mode 100644 index 000000000..29627ce35 --- /dev/null +++ b/src/Mod/PartDesign/App/FeaturePy.xml @@ -0,0 +1,22 @@ + + + + + + This is the father of all PartDesign object classes + + + + getBaseObject: returns feature this one fuses itself to, or None. Normally, this should be the same as BaseFeature property, except for legacy workflow. In legacy workflow, it will look up the support of referenced sketch. + + + + diff --git a/src/Mod/PartDesign/App/FeaturePyImp.cpp b/src/Mod/PartDesign/App/FeaturePyImp.cpp new file mode 100644 index 000000000..c2705499b --- /dev/null +++ b/src/Mod/PartDesign/App/FeaturePyImp.cpp @@ -0,0 +1,57 @@ +/*************************************************************************** + * Copyright (c) 2007 Werner Mayer * + * * + * This file is part of the FreeCAD CAx development system. * + * * + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Library General Public * + * License as published by the Free Software Foundation; either * + * version 2 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Library General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this library; see the file COPYING.LIB. If not, * + * write to the Free Software Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307, USA * + * * + ***************************************************************************/ + + +#include "PreCompiled.h" + +#include "Feature.h" + +// inclusion of the generated files (generated out of FeaturePy.xml) +#include "FeaturePy.h" +#include "FeaturePy.cpp" + +using namespace PartDesign; + +// returns a string which represent the object e.g. when printed in python +std::string FeaturePy::representation(void) const +{ + return std::string(""); +} + +PyObject *FeaturePy::getCustomAttributes(const char* ) const +{ + return 0; +} + +int FeaturePy::setCustomAttributes(const char* , PyObject *) +{ + return 0; +} + +PyObject* FeaturePy::getBaseObject(PyObject * /*args*/) +{ + App::DocumentObject* base = getFeaturePtr()->getBaseObject(); + if (base) + return base->getPyObject(); + else + return Py::new_reference_to(Py::None()); +}