add Python objects for Part and GeoFeature

This commit is contained in:
jriegel 2014-09-10 16:14:08 +02:00 committed by Stefan Tröger
parent 015183a0d7
commit 602aeecaaf
7 changed files with 463 additions and 1 deletions

View File

@ -33,6 +33,9 @@ set(FreeCADApp_LIBS
generate_from_xml(DocumentPy)
generate_from_xml(DocumentObjectPy)
generate_from_xml(DocumentObjectGroupPy)
generate_from_xml(GeoFeaturePy)
generate_from_xml(PartPy)
generate_from_xml(ComplexGeoDataPy)
generate_from_xml(PropertyContainerPy)
generate_from_xml(MaterialPy)
@ -43,6 +46,8 @@ generate_from_py(FreeCADTest TestScript.h)
SET(FreeCADApp_XML_SRCS
DocumentObjectGroupPy.xml
DocumentObjectPy.xml
GeoFeaturePy.xml
PartPy.xml
DocumentPy.xml
PropertyContainerPy.xml
ComplexGeoDataPy.xml
@ -58,6 +63,8 @@ SET(Document_CPP_SRCS
DocumentObjectFileIncluded.cpp
DocumentObjectGroup.cpp
DocumentObjectGroupPyImp.cpp
PartPyImp.cpp
GeoFeaturePyImp.cpp
DocumentObjectPyImp.cpp
DocumentObserver.cpp
DocumentObserverPython.cpp

23
src/App/GeoFeaturePy.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="DocumentObjectPy"
Name="GeoFeaturePy"
Twin="GeoFeature"
TwinPointer="GeoFeature"
Include="App/GeoFeature.h"
Namespace="App"
FatherInclude="App/DocumentObjectPy.h"
FatherNamespace="App">
<Documentation>
<Author Licence="LGPL" Name="Jürgen Riegel" EMail="FreeCAD@juergen-riegel.net" />
<UserDocu>This class does the whole placement and position handling</UserDocu>
</Documentation>
<Methode Name="getPaths">
<Documentation>
<UserDocu>returns all posible paths to the root of the document</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>

View File

@ -0,0 +1,40 @@
#include "PreCompiled.h"
#include "App/GeoFeature.h"
// inclusion of the generated files (generated out of GeoFeaturePy.xml)
#include "GeoFeaturePy.h"
#include "GeoFeaturePy.cpp"
using namespace App;
// returns a string which represents the object e.g. when printed in python
std::string GeoFeaturePy::representation(void) const
{
return std::string("<GeoFeature object>");
}
PyObject* GeoFeaturePy::getPaths(PyObject * /*args*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");
return 0;
}
PyObject *GeoFeaturePy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int GeoFeaturePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}

View File

@ -1,5 +1,5 @@
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2014 *
* Copyright (c) Juergen Riegel (juergen.riegel@web.de) 2014 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
@ -26,7 +26,9 @@
#ifndef _PreComp_
#endif
#include <App/Document.h>
#include "Part.h"
#include "PartPy.h"
//#define new DEBUG_CLIENTBLOCK
using namespace App;
@ -47,3 +49,149 @@ Part::~Part(void)
{
}
DocumentObject* Part::addObject(const char* sType, const char* pObjectName)
{
DocumentObject* obj = getDocument()->addObject(sType, pObjectName);
if (obj) addObject(obj);
return obj;
}
void Part::addObject(DocumentObject* obj)
{
if (!hasObject(obj)) {
std::vector<DocumentObject*> grp = Member.getValues();
grp.push_back(obj);
Member.setValues(grp);
}
}
void Part::removeObject(DocumentObject* obj)
{
std::vector<DocumentObject*> grp = Member.getValues();
for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) {
if (*it == obj) {
grp.erase(it);
Member.setValues(grp);
break;
}
}
}
void Part::removeObjectsFromDocument()
{
std::vector<DocumentObject*> grp = Member.getValues();
for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) {
removeObjectFromDocument(*it);
}
}
void Part::removeObjectFromDocument(DocumentObject* obj)
{
// remove all children
if (obj->getTypeId().isDerivedFrom(Part::getClassTypeId())) {
std::vector<DocumentObject*> grp = static_cast<Part*>(obj)->Member.getValues();
for (std::vector<DocumentObject*>::iterator it = grp.begin(); it != grp.end(); ++it) {
// recursive call to remove all subgroups
removeObjectFromDocument(*it);
}
}
this->getDocument()->remObject(obj->getNameInDocument());
}
DocumentObject *Part::getObject(const char *Name) const
{
DocumentObject* obj = getDocument()->getObject(Name);
if (obj && hasObject(obj))
return obj;
return 0;
}
bool Part::hasObject(const DocumentObject* obj) const
{
const std::vector<DocumentObject*>& grp = Member.getValues();
for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) {
if (*it == obj)
return true;
}
return false;
}
bool Part::isChildOf(const Part* group) const
{
const std::vector<DocumentObject*>& grp = group->Member.getValues();
for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) {
if (*it == this)
return true;
if ((*it)->getTypeId().isDerivedFrom(Part::getClassTypeId())) {
if (this->isChildOf(static_cast<Part*>(*it)))
return true;
}
}
return false;
}
std::vector<DocumentObject*> Part::getObjects() const
{
return Member.getValues();
}
std::vector<DocumentObject*> Part::getObjectsOfType(const Base::Type& typeId) const
{
std::vector<DocumentObject*> type;
const std::vector<DocumentObject*>& grp = Member.getValues();
for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) {
if ( (*it)->getTypeId().isDerivedFrom(typeId))
type.push_back(*it);
}
return type;
}
int Part::countObjectsOfType(const Base::Type& typeId) const
{
int type=0;
const std::vector<DocumentObject*>& grp = Member.getValues();
for (std::vector<DocumentObject*>::const_iterator it = grp.begin(); it != grp.end(); ++it) {
if ( (*it)->getTypeId().isDerivedFrom(typeId))
type++;
}
return type;
}
PyObject *Part::getPyObject()
{
if (PythonObject.is(Py::_None())){
// ref counter is set to 1
PythonObject = Py::Object(new PartPy(this),true);
}
return Py::new_reference_to(PythonObject);
}
// Python feature ---------------------------------------------------------
// Not quit sure yet makeing Part derivable in Python is good Idea!
// JR 2014
//namespace App {
///// @cond DOXERR
//PROPERTY_SOURCE_TEMPLATE(App::PartPython, App::Part)
//template<> const char* App::PartPython::getViewProviderName(void) const {
// return "Gui::ViewProviderPartPython";
//}
//template<> PyObject* App::PartPython::getPyObject(void) {
// if (PythonObject.is(Py::_None())) {
// // ref counter is set to 1
// PythonObject = Py::Object(new FeaturePythonPyT<App::PartPy>(this),true);
// }
// return Py::new_reference_to(PythonObject);
//}
///// @endcond
//
//// explicit template instantiation
//template class AppExport FeaturePythonT<App::Part>;
//}

View File

@ -50,9 +50,54 @@ public:
virtual const char* getViewProviderName(void) const {
return "Gui::ViewProviderPart";
}
/** @name Object handling */
//@{
/** Adds an object of \a sType with \a pObjectName to the document this group belongs to and
* append it to this group as well.
*/
DocumentObject *addObject(const char* sType, const char* pObjectName);
/* Adds the object \a obj to this group.
*/
void addObject(DocumentObject* obj);
/** Removes an object from this group.
*/
void removeObject(DocumentObject* obj);
/** Removes all children objects from this group and the document.
*/
void removeObjectsFromDocument();
/** Returns the object of this group with \a Name. If the group doesn't have such an object 0 is returned.
* @note This method might return 0 even if the document this group belongs to contains an object with this name.
*/
DocumentObject *getObject(const char* Name) const;
/**
* Checks whether the object \a obj is part of this group.
*/
bool hasObject(const DocumentObject* obj) const;
/**
* Checks whether this group object is a child (or sub-child)
* of the given group object.
*/
bool isChildOf(const Part*) const;
/** Returns a list of all objects this group does have.
*/
std::vector<DocumentObject*> getObjects() const;
/** Returns a list of all objects of \a typeId this group does have.
*/
std::vector<DocumentObject*> getObjectsOfType(const Base::Type& typeId) const;
/** Returns the number of objects of \a typeId this group does have.
*/
int countObjectsOfType(const Base::Type& typeId) const;
//@}
virtual PyObject *getPyObject(void);
private:
void removeObjectFromDocument(DocumentObject*);
};
//typedef App::FeaturePythonT<Part> PartPython;
} //namespace App

48
src/App/PartPy.xml Normal file
View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="GeoFeaturePy"
Name="PartPy"
Twin="Part"
TwinPointer="Part"
Include="App/Part.h"
Namespace="App"
FatherInclude="App/GeoFeaturePy.h"
FatherNamespace="App">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>This class handles document objects in Part</UserDocu>
</Documentation>
<Methode Name="newObject">
<Documentation>
<UserDocu>Create and add an object with given type and name to the Part</UserDocu>
</Documentation>
</Methode>
<Methode Name="addObject">
<Documentation>
<UserDocu>Add an object to the Part</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeObject">
<Documentation>
<UserDocu>Remove an object from the Part</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeObjectsFromDocument">
<Documentation>
<UserDocu>Remove all child objects from the Part and document</UserDocu>
</Documentation>
</Methode>
<Methode Name="getObject">
<Documentation>
<UserDocu>Return the object with the given name</UserDocu>
</Documentation>
</Methode>
<Methode Name="hasObject">
<Documentation>
<UserDocu>Checks if the Part has a given object</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>

151
src/App/PartPyImp.cpp Normal file
View File

@ -0,0 +1,151 @@
#include "PreCompiled.h"
#include "App/Part.h"
// inclusion of the generated files (generated out of PartPy.xml)
#include "PartPy.h"
#include "PartPy.cpp"
using namespace App;
// returns a string which represents the object e.g. when printed in python
std::string PartPy::representation(void) const
{
return std::string("<Part object>");
}
PyObject* PartPy::newObject(PyObject *args)
{
char *sType,*sName=0;
if (!PyArg_ParseTuple(args, "s|s", &sType,&sName)) // convert args: Python->C
return NULL;
DocumentObject *object = getPartPtr()->addObject(sType, sName);
if ( object ) {
return object->getPyObject();
}
else {
PyErr_Format(PyExc_Exception, "Cannot create object of type '%s'", sType);
return NULL;
}
}
PyObject* PartPy::addObject(PyObject *args)
{
PyObject *object;
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C
return NULL; // NULL triggers exception
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
PyErr_SetString(PyExc_Exception, "Cannot add an invalid object");
return NULL;
}
if (docObj->getDocumentObjectPtr()->getDocument() != getPartPtr()->getDocument()) {
PyErr_SetString(PyExc_Exception, "Cannot add an object from another document to this Part");
return NULL;
}
if (docObj->getDocumentObjectPtr() == this->getPartPtr()) {
PyErr_SetString(PyExc_Exception, "Cannot add a Part to itself");
return NULL;
}
if (docObj->getDocumentObjectPtr()->getTypeId().isDerivedFrom(Part::getClassTypeId())) {
PyErr_SetString(PyExc_Exception, "Cannot add a Part to a Part");
return NULL;
}
Part* part = getPartPtr();
part->addObject(docObj->getDocumentObjectPtr());
Py_Return;
}
PyObject* PartPy::removeObject(PyObject *args)
{
PyObject *object;
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C
return NULL; // NULL triggers exception
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
PyErr_SetString(PyExc_Exception, "Cannot remove an invalid object");
return NULL;
}
if (docObj->getDocumentObjectPtr()->getDocument() != getPartPtr()->getDocument()) {
PyErr_SetString(PyExc_Exception, "Cannot remove an object from another document from this group");
return NULL;
}
Part* part = getPartPtr();
part->removeObject(docObj->getDocumentObjectPtr());
Py_Return;
}
PyObject* PartPy::removeObjectsFromDocument(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
getPartPtr()->removeObjectsFromDocument();
Py_Return;
}
PyObject* PartPy::getObject(PyObject *args)
{
char* pcName;
if (!PyArg_ParseTuple(args, "s", &pcName)) // convert args: Python->C
return NULL; // NULL triggers exception
DocumentObject* obj = getPartPtr()->getObject(pcName);
if ( obj ) {
return obj->getPyObject();
} else {
Py_Return;
}
}
PyObject* PartPy::hasObject(PyObject *args)
{
PyObject *object;
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) // convert args: Python->C
return NULL; // NULL triggers exception
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->getNameInDocument()) {
PyErr_SetString(PyExc_Exception, "Cannot check an invalid object");
return NULL;
}
if (docObj->getDocumentObjectPtr()->getDocument() != getPartPtr()->getDocument()) {
PyErr_SetString(PyExc_Exception, "Cannot check an object from another document with this group");
return NULL;
}
if (getPartPtr()->hasObject(docObj->getDocumentObjectPtr())) {
Py_INCREF(Py_True);
return Py_True;
}
else {
Py_INCREF(Py_False);
return Py_False;
}
}
PyObject *PartPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int PartPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}