| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2014 Jürgen Riegel <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <App/DocumentObject.h> |
| 26 |
|
| 27 |
#include "Part.h" |
| 28 |
#include "PartPy.h" |
| 29 |
|
| 30 |
|
| 31 |
using namespace App; |
| 32 |
|
| 33 |
|
| 34 |
PROPERTY_SOURCE_WITH_EXTENSIONS(App::Part, App::GeoFeature) |
| 35 |
|
| 36 |
|
| 37 |
//=========================================================================== |
| 38 |
// Part |
| 39 |
//=========================================================================== |
| 40 |
|
| 41 |
|
| 42 |
Part::Part() |
| 43 |
{ |
| 44 |
ADD_PROPERTY(Type, ("")); |
| 45 |
ADD_PROPERTY_TYPE(Material, (nullptr), 0, App::Prop_None, "The Material for this Part"); |
| 46 |
ADD_PROPERTY_TYPE(Meta, (), 0, App::Prop_None, "Map with additional meta information"); |
| 47 |
|
| 48 |
// create the uuid for the document |
| 49 |
Base::Uuid id; |
| 50 |
ADD_PROPERTY_TYPE(Id, (""), 0, App::Prop_None, "ID (Part-Number) of the Item"); |
| 51 |
ADD_PROPERTY_TYPE(Uid, (id), 0, App::Prop_None, "UUID of the Item"); |
| 52 |
|
| 53 |
// license stuff (leave them empty to avoid confusion with imported 3rd party STEP/IGES files) |
| 54 |
ADD_PROPERTY_TYPE(License, (""), 0, App::Prop_None, "License string of the Item"); |
| 55 |
ADD_PROPERTY_TYPE(LicenseURL, (""), 0, App::Prop_None, "URL to the license text/contract"); |
| 56 |
// color and appearance |
| 57 |
ADD_PROPERTY(Color, (1.0, 1.0, 1.0, 0.0)); // set transparent -> not used |
| 58 |
|
| 59 |
GroupExtension::initExtension(this); |
| 60 |
} |
| 61 |
|
| 62 |
Part::~Part() = default; |
| 63 |
|
| 64 |
static App::Part* _getPartOfObject(const DocumentObject* obj, |
| 65 |
std::set<const DocumentObject*>* objset) |
| 66 |
{ |
| 67 |
// as a Part is a geofeaturegroup it must directly link to all |
| 68 |
// objects it contains, even if they are in additional groups etc. |
| 69 |
// But we still must call 'hasObject()' to exclude link brought in by |
| 70 |
// expressions. |
| 71 |
for (auto inObj : obj->getInList()) { |
| 72 |
if (objset && !objset->insert(inObj).second) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
auto group = inObj->getExtensionByType<GeoFeatureGroupExtension>(true); |
| 76 |
if (group && group->hasObject(obj)) { |
| 77 |
if (inObj->isDerivedFrom<App::Part>()) { |
| 78 |
return static_cast<App::Part*>(inObj); |
| 79 |
} |
| 80 |
else if (objset) { |
| 81 |
return _getPartOfObject(inObj, objset); |
| 82 |
} |
| 83 |
// Only one parent geofeature group per object, so break |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
App::Part* Part::getPartOfObject(const DocumentObject* obj, bool recursive) |
| 92 |
{ |
| 93 |
if (!recursive) { |
| 94 |
return _getPartOfObject(obj, nullptr); |
| 95 |
} |
| 96 |
std::set<const DocumentObject*> objset; |
| 97 |
objset.insert(obj); |
| 98 |
return _getPartOfObject(obj, &objset); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
PyObject* Part::getPyObject() |
| 103 |
{ |
| 104 |
if (PythonObject.is(Py::_None())) { |
| 105 |
// ref counter is set to 1 |
| 106 |
PythonObject = Py::Object(new PartPy(this), true); |
| 107 |
} |
| 108 |
return Py::new_reference_to(PythonObject); |
| 109 |
} |
| 110 |
|
| 111 |
void Part::handleChangedPropertyType(Base::XMLReader& reader, |
| 112 |
const char* TypeName, |
| 113 |
App::Property* prop) |
| 114 |
{ |
| 115 |
// Migrate Material from App::PropertyMap to App::PropertyLink |
| 116 |
if (!strcmp(TypeName, "App::PropertyMap")) { |
| 117 |
App::PropertyMap oldvalue; |
| 118 |
oldvalue.Restore(reader); |
| 119 |
if (oldvalue.getSize()) { |
| 120 |
auto oldprop = static_cast<App::PropertyMap*>( |
| 121 |
addDynamicProperty("App::PropertyMap", "Material_old", "Base")); |
| 122 |
oldprop->setValues(oldvalue.getValues()); |
| 123 |
} |
| 124 |
} |
| 125 |
else { |
| 126 |
App::GeoFeature::handleChangedPropertyType(reader, TypeName, prop); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// Python feature --------------------------------------------------------- |
| 131 |
|
| 132 |
// Not quite sure yet making Part derivable in Python is good Idea! |
| 133 |
// JR 2014 |
| 134 |
|
| 135 |
// namespace App { |
| 136 |
///// @cond DOXERR |
| 137 |
// PROPERTY_SOURCE_TEMPLATE(App::PartPython, App::Part) |
| 138 |
// template<> const char* App::PartPython::getViewProviderName(void) const { |
| 139 |
// return "Gui::ViewProviderPartPython"; |
| 140 |
// } |
| 141 |
// template<> PyObject* App::PartPython::getPyObject(void) { |
| 142 |
// if (PythonObject.is(Py::_None())) { |
| 143 |
// // ref counter is set to 1 |
| 144 |
// PythonObject = Py::Object(new FeaturePythonPyT<App::PartPy>(this),true); |
| 145 |
// } |
| 146 |
// return Py::new_reference_to(PythonObject); |
| 147 |
// } |
| 148 |
///// @endcond |
| 149 |
// |
| 150 |
//// explicit template instantiation |
| 151 |
// template class AppExport FeaturePythonT<App::Part>; |
| 152 |
// } |