PartDesign: add FeaturePython

Opens a window to extend PartDesign with Python
This commit is contained in:
DeepSOIC 2017-03-03 03:39:51 +03:00 committed by Yorik van Havre
parent 271bc7450e
commit d9c803ffce
6 changed files with 118 additions and 0 deletions

View File

@ -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();

View File

@ -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})

View File

@ -36,9 +36,11 @@
// TODO Cleanup headers (2015-09-04, Fat-Zer)
#include <Base/Exception.h>
#include "App/Document.h"
#include <App/FeaturePythonPyImp.h>
#include "App/OriginFeature.h"
#include "Body.h"
#include "Feature.h"
#include "FeaturePy.h"
#include "Mod/Part/App/DatumFeature.h"
#include <Base/Console.h>
@ -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<PartDesign::FeaturePy>(this),true);
}
return Py::new_reference_to(PythonObject);
}
/// @endcond
// explicit template instantiation
template class PartDesignExport FeaturePythonT<PartDesign::Feature>;
}

View File

@ -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<Feature> FeaturePython;
} //namespace PartDesign

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="PartFeaturePy"
Name="FeaturePy"
Twin="Feature"
TwinPointer="Feature"
Include="Mod/PartDesign/App/Feature.h"
Namespace="PartDesign"
FatherInclude="Mod/Part/App/PartFeaturePy.h"
FatherNamespace="Part">
<Documentation>
<Author Licence="LGPL" Name="Juergen Riegel" EMail="FreeCAD@juergen-riegel.net" />
<UserDocu>This is the father of all PartDesign object classes</UserDocu>
</Documentation>
<Methode Name="getBaseObject">
<Documentation>
<UserDocu>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.</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@ -0,0 +1,57 @@
/***************************************************************************
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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("<PartDesign::Feature>");
}
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());
}