| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 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 |
#include <App/GeoFeaturePy.h> |
| 24 |
|
| 25 |
#include <Base/Tools.h> |
| 26 |
|
| 27 |
#include "ComplexGeoData.h" |
| 28 |
#include "Document.h" |
| 29 |
#include "GeoFeature.h" |
| 30 |
#include "GeoFeatureGroupExtension.h" |
| 31 |
#include "ElementNamingUtils.h" |
| 32 |
#include "Link.h" |
| 33 |
|
| 34 |
|
| 35 |
using namespace App; |
| 36 |
|
| 37 |
|
| 38 |
PROPERTY_SOURCE(App::GeoFeature, App::DocumentObject) |
| 39 |
|
| 40 |
|
| 41 |
//=========================================================================== |
| 42 |
// Feature |
| 43 |
//=========================================================================== |
| 44 |
|
| 45 |
GeoFeature::GeoFeature() |
| 46 |
{ |
| 47 |
ADD_PROPERTY_TYPE(Placement, (Base::Placement()), nullptr, Prop_NoRecompute, nullptr); |
| 48 |
ADD_PROPERTY_TYPE(_ElementMapVersion, |
| 49 |
(""), |
| 50 |
"Base", |
| 51 |
(App::PropertyType)(Prop_Output | Prop_Hidden | Prop_Transient), |
| 52 |
""); |
| 53 |
} |
| 54 |
|
| 55 |
GeoFeature::~GeoFeature() = default; |
| 56 |
|
| 57 |
void GeoFeature::transformPlacement(const Base::Placement& transform) |
| 58 |
{ |
| 59 |
Base::Placement plm = this->Placement.getValue(); |
| 60 |
plm = transform * plm; |
| 61 |
this->Placement.setValue(plm); |
| 62 |
} |
| 63 |
|
| 64 |
Base::Placement GeoFeature::globalPlacement() const |
| 65 |
{ |
| 66 |
return GeoFeature::getGlobalPlacement(this); |
| 67 |
} |
| 68 |
|
| 69 |
const PropertyComplexGeoData* GeoFeature::getPropertyOfGeometry() const |
| 70 |
{ |
| 71 |
return nullptr; |
| 72 |
} |
| 73 |
|
| 74 |
PyObject* GeoFeature::getPyObject() |
| 75 |
{ |
| 76 |
if (PythonObject.is(Py::_None())) { |
| 77 |
// ref counter is set to 1 |
| 78 |
PythonObject = Py::Object(new GeoFeaturePy(this), true); |
| 79 |
} |
| 80 |
return Py::new_reference_to(PythonObject); |
| 81 |
} |
| 82 |
|
| 83 |
ElementNamePair GeoFeature::getElementName(const char* name, ElementNameType type) const |
| 84 |
{ |
| 85 |
(void)type; |
| 86 |
|
| 87 |
if (!name) { |
| 88 |
return {}; |
| 89 |
} |
| 90 |
|
| 91 |
auto prop = getPropertyOfGeometry(); |
| 92 |
if (!prop) { |
| 93 |
return ElementNamePair("", name); |
| 94 |
} |
| 95 |
|
| 96 |
auto geo = prop->getComplexData(); |
| 97 |
if (!geo) { |
| 98 |
return ElementNamePair("", name); |
| 99 |
} |
| 100 |
|
| 101 |
return _getElementName(name, geo->getElementName(name)); |
| 102 |
} |
| 103 |
|
| 104 |
ElementNamePair GeoFeature::_getElementName(const char* name, |
| 105 |
const Data::MappedElement& mapped) const |
| 106 |
{ |
| 107 |
ElementNamePair ret; |
| 108 |
if (mapped.index && mapped.name) { |
| 109 |
std::ostringstream ss; |
| 110 |
ss << Data::ComplexGeoData::elementMapPrefix() << mapped.name << '.' << mapped.index; |
| 111 |
std::string result; |
| 112 |
mapped.index.appendToStringBuffer(result); |
| 113 |
return ElementNamePair(ss.str().c_str(), result.c_str()); |
| 114 |
} |
| 115 |
else if (mapped.name) { |
| 116 |
// FC_TRACE("element mapped name " << name << " not found in " << getFullName()); |
| 117 |
const char* dot = strrchr(name, '.'); |
| 118 |
if (dot) { |
| 119 |
// deliberately mangle the old style element name to signal a |
| 120 |
// missing reference |
| 121 |
std::ostringstream ss; |
| 122 |
ss << Data::MISSING_PREFIX << dot + 1; |
| 123 |
return ElementNamePair(name, ss.str().c_str()); |
| 124 |
} |
| 125 |
return ElementNamePair(name, ""); |
| 126 |
} |
| 127 |
else { |
| 128 |
std::string oldName; |
| 129 |
mapped.index.appendToStringBuffer(oldName); |
| 130 |
return ElementNamePair("", oldName.c_str()); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
DocumentObject* GeoFeature::resolveElement(const DocumentObject* obj, |
| 135 |
const char* subname, |
| 136 |
ElementNamePair& elementName, |
| 137 |
bool append, |
| 138 |
ElementNameType type, |
| 139 |
const DocumentObject* filter, |
| 140 |
const char** _element, |
| 141 |
GeoFeature** geoFeature) |
| 142 |
{ |
| 143 |
elementName.newName.clear(); |
| 144 |
elementName.oldName.clear(); |
| 145 |
if (!obj || !obj->isAttachedToDocument()) { |
| 146 |
return nullptr; |
| 147 |
} |
| 148 |
if (!subname) { |
| 149 |
subname = ""; |
| 150 |
} |
| 151 |
const char* element = Data::findElementName(subname); |
| 152 |
if (_element) { |
| 153 |
*_element = element; |
| 154 |
} |
| 155 |
auto sobj = obj->getSubObject(std::string(subname, element).c_str()); |
| 156 |
if (!sobj) { |
| 157 |
return nullptr; |
| 158 |
} |
| 159 |
auto linked = sobj->getLinkedObject(true); |
| 160 |
auto geo = freecad_cast<GeoFeature*>(linked); |
| 161 |
if (!geo && linked) { |
| 162 |
auto ext = linked->getExtensionByType<LinkBaseExtension>(true); |
| 163 |
if (ext) { |
| 164 |
geo = freecad_cast<GeoFeature*>(ext->getTrueLinkedObject(true)); |
| 165 |
} |
| 166 |
} |
| 167 |
if (geoFeature) { |
| 168 |
*geoFeature = geo; |
| 169 |
} |
| 170 |
if (filter && geo != filter) { |
| 171 |
return nullptr; |
| 172 |
} |
| 173 |
if (!element || !element[0]) { |
| 174 |
if (append) { |
| 175 |
elementName.oldName = Data::oldElementName(subname); |
| 176 |
} |
| 177 |
return sobj; |
| 178 |
} |
| 179 |
|
| 180 |
if (!geo || hasHiddenMarker(element)) { |
| 181 |
if (!append) { |
| 182 |
elementName.oldName = element; |
| 183 |
} |
| 184 |
else { |
| 185 |
elementName.oldName = Data::oldElementName(subname); |
| 186 |
} |
| 187 |
return sobj; |
| 188 |
} |
| 189 |
if (!append) { |
| 190 |
elementName = geo->getElementName(element, type); |
| 191 |
} |
| 192 |
else { |
| 193 |
const auto& names = geo->getElementName(element, type); |
| 194 |
std::string prefix(subname, element - subname); |
| 195 |
if (!names.newName.empty()) { |
| 196 |
elementName.newName = prefix + names.newName; |
| 197 |
} |
| 198 |
elementName.oldName = prefix + names.oldName; |
| 199 |
} |
| 200 |
return sobj; |
| 201 |
} |
| 202 |
|
| 203 |
App::Material GeoFeature::getMaterialAppearance() const |
| 204 |
{ |
| 205 |
return App::Material(App::Material::DEFAULT); |
| 206 |
} |
| 207 |
|
| 208 |
void GeoFeature::setMaterialAppearance(const App::Material& material) |
| 209 |
{ |
| 210 |
Q_UNUSED(material) |
| 211 |
} |
| 212 |
|
| 213 |
bool GeoFeature::getCameraAlignmentDirection(Base::Vector3d& directionZ, Base::Vector3d& directionX, const char* subname) const |
| 214 |
{ |
| 215 |
Q_UNUSED(subname) |
| 216 |
Q_UNUSED(directionZ) |
| 217 |
Q_UNUSED(directionX) |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
bool GeoFeature::hasMissingElement(const char* subname) |
| 222 |
{ |
| 223 |
return Data::hasMissingElement(subname); |
| 224 |
} |
| 225 |
|
| 226 |
void GeoFeature::updateElementReference() |
| 227 |
{ |
| 228 |
auto prop = getPropertyOfGeometry(); |
| 229 |
if (!prop) { |
| 230 |
return; |
| 231 |
} |
| 232 |
auto geo = prop->getComplexData(); |
| 233 |
if (!geo) { |
| 234 |
return; |
| 235 |
} |
| 236 |
bool reset = false; |
| 237 |
|
| 238 |
auto version = getElementMapVersion(prop); |
| 239 |
if (_ElementMapVersion.getStrValue().empty()) { |
| 240 |
_ElementMapVersion.setValue(version); |
| 241 |
} |
| 242 |
else if (_ElementMapVersion.getStrValue() != version) { |
| 243 |
reset = true; |
| 244 |
_ElementMapVersion.setValue(version); |
| 245 |
} |
| 246 |
|
| 247 |
PropertyLinkBase::updateElementReferences(this, reset); |
| 248 |
} |
| 249 |
|
| 250 |
void GeoFeature::onChanged(const Property* prop) |
| 251 |
{ |
| 252 |
if (prop == getPropertyOfGeometry()) { |
| 253 |
if (getDocument() && !getDocument()->testStatus(Document::Restoring) |
| 254 |
&& !getDocument()->isPerformingTransaction()) { |
| 255 |
updateElementReference(); |
| 256 |
} |
| 257 |
} |
| 258 |
DocumentObject::onChanged(prop); |
| 259 |
} |
| 260 |
|
| 261 |
void GeoFeature::onDocumentRestored() |
| 262 |
{ |
| 263 |
if (!getDocument()->testStatus(Document::Status::Importing)) { |
| 264 |
_ElementMapVersion.setValue(getElementMapVersion(getPropertyOfGeometry(), true)); |
| 265 |
} |
| 266 |
DocumentObject::onDocumentRestored(); |
| 267 |
} |
| 268 |
|
| 269 |
const std::vector<std::string>& GeoFeature::searchElementCache(const std::string& element, |
| 270 |
Data::SearchOptions options, |
| 271 |
double tol, |
| 272 |
double atol) const |
| 273 |
{ |
| 274 |
static std::vector<std::string> none; |
| 275 |
(void)element; |
| 276 |
(void)options; |
| 277 |
(void)tol; |
| 278 |
(void)atol; |
| 279 |
return none; |
| 280 |
} |
| 281 |
|
| 282 |
std::vector<const char*> GeoFeature::getElementTypes(bool /*all*/) const |
| 283 |
{ |
| 284 |
static std::vector<const char*> nil; |
| 285 |
auto prop = getPropertyOfGeometry(); |
| 286 |
if (!prop) { |
| 287 |
return nil; |
| 288 |
} |
| 289 |
return prop->getComplexData()->getElementTypes(); |
| 290 |
} |
| 291 |
|
| 292 |
std::vector<Data::IndexedName> GeoFeature::getHigherElements(const char* element, bool silent) const |
| 293 |
{ |
| 294 |
auto prop = getPropertyOfGeometry(); |
| 295 |
if (!prop) { |
| 296 |
return {}; |
| 297 |
} |
| 298 |
return prop->getComplexData()->getHigherElements(element, silent); |
| 299 |
} |
| 300 |
|
| 301 |
Base::Placement GeoFeature::getPlacementFromProp(App::DocumentObject* obj, const char* propName) |
| 302 |
{ |
| 303 |
Base::Placement plc = Base::Placement(); |
| 304 |
if (!obj) { |
| 305 |
return plc; |
| 306 |
} |
| 307 |
|
| 308 |
auto* propPlacement = dynamic_cast<App::PropertyPlacement*>(obj->getPropertyByName(propName)); |
| 309 |
if (propPlacement) { |
| 310 |
plc = propPlacement->getValue(); |
| 311 |
} |
| 312 |
return plc; |
| 313 |
} |
| 314 |
|
| 315 |
Base::Placement GeoFeature::getGlobalPlacement(App::DocumentObject* targetObj, |
| 316 |
App::DocumentObject* rootObj, |
| 317 |
const std::string& sub) |
| 318 |
{ |
| 319 |
if (!rootObj) { |
| 320 |
return Base::Placement(); |
| 321 |
} |
| 322 |
|
| 323 |
return rootObj->getPlacementOf(sub, targetObj); |
| 324 |
} |
| 325 |
|
| 326 |
Base::Placement GeoFeature::getGlobalPlacement(App::DocumentObject* targetObj, |
| 327 |
App::PropertyXLinkSub* prop) |
| 328 |
{ |
| 329 |
if (!targetObj || !prop) { |
| 330 |
return Base::Placement(); |
| 331 |
} |
| 332 |
|
| 333 |
std::vector<std::string> subs = prop->getSubValues(); |
| 334 |
if (subs.empty()) { |
| 335 |
return Base::Placement(); |
| 336 |
} |
| 337 |
|
| 338 |
return getGlobalPlacement(targetObj, prop->getValue(), subs[0]); |
| 339 |
} |
| 340 |
|
| 341 |
Base::Placement GeoFeature::getGlobalPlacement(const DocumentObject* obj) |
| 342 |
{ |
| 343 |
auto placementProperty = obj->getPropertyByName<App::PropertyPlacement>("Placement"); |
| 344 |
|
| 345 |
if (!placementProperty) { |
| 346 |
return {}; |
| 347 |
} |
| 348 |
|
| 349 |
auto* group = GeoFeatureGroupExtension::getGroupOfObject(obj); |
| 350 |
if (group) { |
| 351 |
auto ext = group->getExtensionByType<GeoFeatureGroupExtension>(); |
| 352 |
return ext->globalGroupPlacement() * placementProperty->getValue(); |
| 353 |
} |
| 354 |
|
| 355 |
return placementProperty->getValue(); |
| 356 |
} |
| 357 |
|