/*************************************************************************** * Copyright (c) Alexander Golubev (Fat-Zer) 2015 * * * * 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 "OriginGroupExtension.h" #ifndef _PreComp_ #endif #include #include #include "Origin.h" #include "GeoFeature.h" using namespace App; EXTENSION_PROPERTY_SOURCE(App::OriginGroupExtension, App::GeoFeatureGroupExtension); OriginGroupExtension::OriginGroupExtension () { initExtension(OriginGroupExtension::getExtensionClassTypeId()); EXTENSION_ADD_PROPERTY_TYPE ( Origin, (0), 0, App::Prop_Hidden, "Origin linked to the group" ); } OriginGroupExtension::~OriginGroupExtension () { } App::Origin *OriginGroupExtension::getOrigin () const { App::DocumentObject *originObj = Origin.getValue (); if ( !originObj ) { std::stringstream err; err << "Can't find Origin for \"" << getExtendedObject()->getNameInDocument () << "\""; throw Base::Exception ( err.str().c_str () ); } else if (! originObj->isDerivedFrom ( App::Origin::getClassTypeId() ) ) { std::stringstream err; err << "Bad object \"" << originObj->getNameInDocument () << "\"(" << originObj->getTypeId().getName() << ") linked to the Origin of \"" << getExtendedObject()->getNameInDocument () << "\""; throw Base::Exception ( err.str().c_str () ); } else { return static_cast ( originObj ); } } App::DocumentObject *OriginGroupExtension::getGroupOfObject (const DocumentObject* obj, bool indirect) { const Document* doc = obj->getDocument(); std::vector grps = doc->getObjectsWithExtension ( OriginGroupExtension::getExtensionClassTypeId() ); for (auto grpObj: grps) { OriginGroupExtension* grp = dynamic_cast (grpObj->getExtension( OriginGroupExtension::getExtensionClassTypeId())); if(!grp) throw Base::TypeError("Wrong type in origin group extenion"); if ( indirect ) { if ( grp->geoHasObject (obj) ) { return grp->getExtendedObject(); } } else { if ( grp->hasObject (obj) ) { return grp->getExtendedObject(); } } } return 0; } short OriginGroupExtension::extensionMustExecute() { if (Origin.isTouched ()) { return 1; } else { return GeoFeatureGroupExtension::extensionMustExecute(); } } App::DocumentObjectExecReturn *OriginGroupExtension::extensionExecute() { try { // try to find all base axis and planes in the origin getOrigin (); } catch (const Base::Exception &ex) { //getExtendedObject()->setError (); return new App::DocumentObjectExecReturn ( ex.what () ); } return GeoFeatureGroupExtension::extensionExecute (); } void OriginGroupExtension::onExtendedSetupObject () { App::Document *doc = getExtendedObject()->getDocument (); std::string objName = std::string ( getExtendedObject()->getNameInDocument()).append ( "Origin" ); App::DocumentObject *originObj = doc->addObject ( "App::Origin", objName.c_str () ); assert ( originObj && originObj->isDerivedFrom ( App::Origin::getClassTypeId () ) ); Origin.setValue (originObj); GeoFeatureGroupExtension::onExtendedSetupObject (); } void OriginGroupExtension::onExtendedUnsetupObject () { App::DocumentObject *origin = Origin.getValue (); if (origin && !origin->isDeleting ()) { origin->getDocument ()->remObject (origin->getNameInDocument()); } GeoFeatureGroupExtension::onExtendedUnsetupObject (); } // Python feature --------------------------------------------------------- namespace App { EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::OriginGroupExtensionPython, App::OriginGroupExtension) // explicit template instantiation template class AppExport ExtensionPythonT>; }