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 119765ba96
commit 80ca84a2eb
7 changed files with 463 additions and 1 deletions

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>;
//}