Debian Codegraph @

src/App/GeoFeatureGroupExtension.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2014 Jürgen Riegel <[email protected]> *
3 * Copyright (c) 2015 Alexander Golubev (Fat-Zer) <[email protected]> *
4 * *
5 * This file is part of the FreeCAD CAx development system. *
6 * *
7 * This library is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU Library General Public *
9 * License as published by the Free Software Foundation; either *
10 * version 2 of the License, or (at your option) any later version. *
11 * *
12 * This library is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Library General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Library General Public *
18 * License along with this library; see the file COPYING.LIB. If not, *
19 * write to the Free Software Foundation, Inc., 59 Temple Place, *
20 * Suite 330, Boston, MA 02111-1307, USA *
21 * *
22 ***************************************************************************/
23
24 #include <App/Document.h>
25 #include <Base/Tools.h>
26
27 #include "GeoFeatureGroupExtension.h"
28 #include "Link.h"
29 #include "Origin.h"
30 #include "Datums.h"
31 #include "OriginGroupExtension.h"
32
33
34 using namespace App;
35
36
37 EXTENSION_PROPERTY_SOURCE(App::GeoFeatureGroupExtension, App::GroupExtension)
38
39
40 //===========================================================================
41 // Feature
42 //===========================================================================
43
44 GeoFeatureGroupExtension::GeoFeatureGroupExtension()
45 {
46 initExtensionType(GeoFeatureGroupExtension::getExtensionClassTypeId());
47 Group.setScope(LinkScope::Child);
48 }
49
50 GeoFeatureGroupExtension::~GeoFeatureGroupExtension() = default;
51
52 void GeoFeatureGroupExtension::initExtension(ExtensionContainer* obj)
53 {
54
55 if (!obj->isDerivedFrom<App::GeoFeature>()) {
56 throw Base::RuntimeError("GeoFeatureGroupExtension can only be applied to GeoFeatures");
57 }
58
59 App::GroupExtension::initExtension(obj);
60 }
61
62 PropertyPlacement& GeoFeatureGroupExtension::placement()
63 {
64
65 if (!getExtendedContainer()) {
66 throw Base::RuntimeError("GeoFeatureGroupExtension was not applied to GeoFeature");
67 }
68
69 return static_cast<App::GeoFeature*>(getExtendedContainer())->Placement;
70 }
71
72
73 void GeoFeatureGroupExtension::transformPlacement(const Base::Placement& transform)
74 {
75 // NOTE: Keep in sync with APP::GeoFeature
76 Base::Placement plm = this->placement().getValue();
77 plm = transform * plm;
78 this->placement().setValue(plm);
79 }
80
81 DocumentObject* GeoFeatureGroupExtension::getGroupOfObject(const DocumentObject* obj)
82 {
83 if (!obj) {
84 return nullptr;
85 }
86
87 // we will find origins, but not origin features
88 if (obj->isDerivedFrom<App::DatumElement>()) {
89 return OriginGroupExtension::getGroupOfObject(obj);
90 }
91
92 // compared to GroupExtension we do return here all GeoFeatureGroups including all extensions
93 // derived from it like OriginGroup. That is needed as we use this function to get all local
94 // coordinate systems. Also there is no reason to distinguish between GeoFeatuerGroups, there is
95 // only between group/geofeaturegroup
96 auto list = obj->getInList();
97 for (auto inObj : list) {
98
99 // There is a chance that a derived geofeaturegroup links with a local link and hence is not
100 // the parent group even though it links to the object. We use hasObject as one and only
101 // truth if it has the object within the group
102 auto group = inObj->getExtensionByType<GeoFeatureGroupExtension>(true);
103 if (group && group->hasObject(obj)) {
104 return inObj;
105 }
106 }
107
108 return nullptr;
109 }
110
111 Base::Placement GeoFeatureGroupExtension::globalGroupPlacement()
112 {
113 if (getExtendedObject()->isRecomputing()) {
114 throw Base::RuntimeError("Global placement cannot be calculated on recompute");
115 }
116
117 std::unordered_set<GeoFeatureGroupExtension*> history;
118 history.insert(this);
119 return recursiveGroupPlacement(this, history);
120 }
121
122
123 Base::Placement GeoFeatureGroupExtension::recursiveGroupPlacement(
124 GeoFeatureGroupExtension* group,
125 std::unordered_set<GeoFeatureGroupExtension*>& history)
126 {
127 history.insert(this);
128
129 auto inList = group->getExtendedObject()->getInList();
130 for (auto* link : inList) {
131 auto parent = link->getExtensionByType<GeoFeatureGroupExtension>(true);
132 if (parent && parent->hasObject(group->getExtendedObject())) {
133 // Cyclic dependencies detected
134 if (history.contains(parent)) {
135 break;
136 }
137 return recursiveGroupPlacement(parent, history) * group->placement().getValue();
138 }
139 }
140
141 return group->placement().getValue();
142 }
143
144 std::vector<DocumentObject*>
145 GeoFeatureGroupExtension::addObjects(std::vector<App::DocumentObject*> objects)
146 {
147
148 std::vector<DocumentObject*> grp = Group.getValues();
149 std::vector<DocumentObject*> ret;
150
151 for (auto object : objects) {
152
153 if (!allowObject(object)) {
154 continue;
155 }
156
157 std::vector<App::DocumentObject*> links;
158
159 // Prevent from extracting children of nested groups fixed issue:(#26743, #28830)
160 // As groups manage their own local coordinate systems and children,
161 // so transfer this and remove from the old group. if not a group then get CSRelevantLinks
162 if (!object->hasExtension(GeoFeatureGroupExtension::getExtensionClassTypeId())) {
163 links = getCSRelevantLinks(object);
164 }
165 links.push_back(object);
166
167 for (auto obj : links) {
168 // only one geofeaturegroup per object.
169 auto* group = App::GeoFeatureGroupExtension::getGroupOfObject(obj);
170 if (group && group != getExtendedObject()) {
171 group->getExtensionByType<App::GroupExtension>()->removeObject(obj);
172 }
173
174 if (!hasObject(obj)) {
175 grp.push_back(obj);
176 ret.push_back(obj);
177 }
178 }
179 }
180
181 Group.setValues(grp);
182 return ret;
183 }
184
185 std::vector<DocumentObject*>
186 GeoFeatureGroupExtension::removeObjects(std::vector<App::DocumentObject*> objects)
187 {
188
189 std::vector<DocumentObject*> removed;
190 std::vector<DocumentObject*> grp = Group.getValues();
191
192 for (auto object : objects) {
193 // cross CoordinateSystem links are not allowed, so we need to remove the whole link group
194 std::vector<DocumentObject*> links = getCSRelevantLinks(object);
195 links.push_back(object);
196
197 // remove all links out of group
198 for (auto link : links) {
199 auto end = std::remove(grp.begin(), grp.end(), link);
200 if (end != grp.end()) {
201 grp.erase(end, grp.end());
202 removed.push_back(link);
203 }
204 }
205 }
206
207 if (!removed.empty()) {
208 Group.setValues(grp);
209 }
210
211 return removed;
212 }
213
214 void GeoFeatureGroupExtension::extensionOnChanged(const Property* p)
215 {
216
217 // objects are only allowed in a single GeoFeatureGroup
218 if (p == &Group && !Group.testStatus(Property::User3)) {
219
220 if ((!getExtendedObject()->isRestoring()
221 || getExtendedObject()->getDocument()->testStatus(Document::Importing))
222 && !getExtendedObject()->getDocument()->isPerformingTransaction()) {
223
224 bool error = false;
225 auto corrected = Group.getValues();
226 for (auto obj : Group.getValues()) {
227
228 // we have already set the obj into the group, so in a case of multiple groups
229 // getGroupOfObject would return anyone of it and hence it is possible that we miss
230 // an error. We need a custom check
231 auto list = obj->getInList();
232 for (auto in : list) {
233 if (in == getExtendedObject()) {
234 continue;
235 }
236 auto parent = in->getExtensionByType<GeoFeatureGroupExtension>(true);
237 if (parent && parent->hasObject(obj)) {
238 error = true;
239 corrected.erase(std::remove(corrected.begin(), corrected.end(), obj),
240 corrected.end());
241 }
242 }
243 }
244
245 // if an error was found we need to correct the values and inform the user
246 if (error) {
247 Base::ObjectStatusLocker<Property::Status, Property> guard(Property::User3, &Group);
248 Group.setValues(corrected);
249 throw Base::RuntimeError("Object can only be in a single GeoFeatureGroup");
250 }
251 }
252 }
253
254 App::GroupExtension::extensionOnChanged(p);
255 }
256
257
258 std::vector<DocumentObject*>
259 GeoFeatureGroupExtension::getScopedObjectsFromLinks(const DocumentObject* obj, LinkScope scope)
260 {
261
262 if (!obj) {
263 return {};
264 }
265
266 // we get all linked objects. We can't use outList() as this includes the links from expressions
267 std::vector<App::DocumentObject*> result;
268 std::vector<App::Property*> list;
269 obj->getPropertyList(list);
270 for (App::Property* prop : list) {
271 auto vec = getScopedObjectsFromLink(prop, scope);
272 result.insert(result.end(), vec.begin(), vec.end());
273 }
274
275 // clear all null objects and duplicates
276 std::sort(result.begin(), result.end());
277 result.erase(std::unique(result.begin(), result.end()), result.end());
278
279 return result;
280 }
281
282 std::vector<DocumentObject*> GeoFeatureGroupExtension::getScopedObjectsFromLink(App::Property* prop,
283 LinkScope scope)
284 {
285
286 if (!prop) {
287 return {};
288 }
289
290 std::vector<App::DocumentObject*> result;
291 auto link = freecad_cast<PropertyLinkBase*>(prop);
292 if (link && link->getScope() == scope) {
293 link->getLinks(result);
294 }
295
296 return result;
297 }
298
299 void GeoFeatureGroupExtension::getCSOutList(const App::DocumentObject* obj,
300 std::vector<DocumentObject*>& vec)
301 {
302
303 if (!obj) {
304 return;
305 }
306
307 // we get all relevant linked objects. We can't use outList() as this includes the links from
308 // expressions, also we only want links with scope Local
309 auto result = getScopedObjectsFromLinks(obj, LinkScope::Local);
310
311 //we remove all links to origin features and origins, they belong to a CS too and can't be moved
312 result.erase(std::remove_if(result.begin(), result.end(), [](App::DocumentObject* obj)->bool {
313 return (obj->isDerivedFrom<App::DatumElement>() ||
314 obj->isDerivedFrom<App::Origin>());
315 }), result.end());
316
317 vec.insert(vec.end(), result.begin(), result.end());
318
319 // post process the vector
320 std::sort(vec.begin(), vec.end());
321 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
322 }
323
324 void GeoFeatureGroupExtension::getCSInList(const DocumentObject* obj,
325 std::vector<DocumentObject*>& vec)
326 {
327
328 if (!obj) {
329 return;
330 }
331
332 // search the inlist for objects that have non-expression links to us
333 for (App::DocumentObject* parent : obj->getInList()) {
334
335 // not interested in other groups (and here we mean all groups, normal ones and
336 // geofeaturegroup)
337 if (parent->hasExtension(App::GroupExtension::getExtensionClassTypeId())) {
338 continue;
339 }
340
341 // check if the link is real Local scope one or if it is a expression one (could also be
342 // both, so it is not enough to check the expressions)
343 auto res = getScopedObjectsFromLinks(parent, LinkScope::Local);
344 if (std::ranges::find(res, obj) != res.end()) {
345 vec.push_back(parent);
346 }
347 }
348
349 // clear all duplicates
350 std::sort(vec.begin(), vec.end());
351 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
352 }
353
354 std::vector<DocumentObject*> GeoFeatureGroupExtension::getCSRelevantLinks(const DocumentObject* obj)
355 {
356
357 if (!obj) {
358 return {};
359 }
360
361 // get all out links
362 std::vector<DocumentObject*> vec;
363
364 recursiveCSRelevantLinks(obj, vec);
365
366 // post process the list after we added many things
367 std::sort(vec.begin(), vec.end());
368 vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
369 vec.erase(std::remove(vec.begin(), vec.end(), obj), vec.end());
370
371 return vec;
372 }
373
374 void GeoFeatureGroupExtension::recursiveCSRelevantLinks(const DocumentObject* obj,
375 std::vector<DocumentObject*>& vec)
376 {
377
378 if (!obj) {
379 return;
380 }
381
382 std::vector<DocumentObject*> links;
383 getCSOutList(obj, links);
384 getCSInList(obj, links);
385
386 // go on traversing the graph in all directions!
387 for (auto o : links) {
388 if (!o || o == obj || std::ranges::find(vec, o) != vec.end()) {
389 continue;
390 }
391
392 vec.push_back(o);
393 recursiveCSRelevantLinks(o, vec);
394 }
395 }
396
397 bool GeoFeatureGroupExtension::extensionGetSubObject(DocumentObject*& ret,
398 const char* subname,
399 PyObject** pyObj,
400 Base::Matrix4D* mat,
401 bool transform,
402 int depth) const
403 {
404 ret = nullptr;
405 const char* dot;
406 if (!subname || *subname == 0) {
407 auto obj = dynamic_cast<const DocumentObject*>(getExtendedContainer());
408 ret = const_cast<DocumentObject*>(obj);
409 if (mat && transform) {
410 *mat *= const_cast<GeoFeatureGroupExtension*>(this)->placement().getValue().toMatrix();
411 }
412 }
413 else if ((dot = strchr(subname, '.'))) {
414 if (subname[0] != '$') {
415 ret = Group.findUsingMap(std::string(subname, dot));
416 }
417 else {
418 std::string name = std::string(subname + 1, dot);
419 for (auto child : Group.getValues()) {
420 if (name == child->Label.getStrValue()) {
421 ret = child;
422 break;
423 }
424 }
425 }
426 if (ret) {
427 ++dot;
428 if (*dot && !ret->hasExtension(App::LinkBaseExtension::getExtensionClassTypeId())
429 && !ret->hasExtension(App::GeoFeatureGroupExtension::getExtensionClassTypeId())) {
430 // Consider this
431 // Body
432 // | -- Pad
433 // | -- Sketch
434 //
435 // Suppose we want to getSubObject(Body,"Pad.Sketch.")
436 // Because of the special property of geo feature group, both
437 // Pad and Sketch are children of Body, so we can't call
438 // getSubObject(Pad,"Sketch.") as this will transform Sketch
439 // using Pad's placement.
440 //
441 const char* next = strchr(dot, '.');
442 if (next) {
443 App::DocumentObject* nret = nullptr;
444 extensionGetSubObject(nret, dot, pyObj, mat, transform, depth + 1);
445 if (nret) {
446 ret = nret;
447 return true;
448 }
449 }
450 }
451 if (mat && transform) {
452 *mat *=
453 const_cast<GeoFeatureGroupExtension*>(this)->placement().getValue().toMatrix();
454 }
455 ret = ret->getSubObject(dot, pyObj, mat, true, depth + 1);
456 }
457 }
458 return true;
459 }
460
461 bool GeoFeatureGroupExtension::areLinksValid(const DocumentObject* obj)
462 {
463
464 if (!obj) {
465 return true;
466 }
467
468 std::vector<App::Property*> list;
469 obj->getPropertyList(list);
470 for (App::Property* prop : list) {
471 if (!isLinkValid(prop)) {
472 return false;
473 }
474 }
475
476 return true;
477 }
478
479 bool GeoFeatureGroupExtension::isLinkValid(App::Property* prop)
480 {
481
482 if (!prop) {
483 return true;
484 }
485
486 // get the object that holds the property
487 if (!prop->getContainer()->isDerivedFrom<App::DocumentObject>()) {
488 return true; // this link comes not from a document object, scopes are meaningless
489 }
490 auto obj = static_cast<App::DocumentObject*>(prop->getContainer());
491
492 // no cross CS link for local links.
493 auto result = getScopedObjectsFromLink(prop, LinkScope::Local);
494 auto group = getGroupOfObject(obj);
495 for (auto link : result) {
496 if (getGroupOfObject(link) != group) {
497 return false;
498 }
499 }
500
501 // for links with scope SubGroup we need to check if all features are part of subgroups
502 if (obj->hasExtension(App::GeoFeatureGroupExtension::getExtensionClassTypeId())) {
503 result = getScopedObjectsFromLink(prop, LinkScope::Child);
504 auto groupExt = obj->getExtensionByType<App::GeoFeatureGroupExtension>();
505 for (auto link : result) {
506 if (!groupExt->hasObject(link, true)) {
507 return false;
508 }
509 }
510 }
511
512 return true;
513 }
514
515 void GeoFeatureGroupExtension::getInvalidLinkObjects(const DocumentObject* obj,
516 std::vector<DocumentObject*>& vec)
517 {
518
519 if (!obj) {
520 return;
521 }
522
523 // no cross CS link for local links.
524 auto result = getScopedObjectsFromLinks(obj, LinkScope::Local);
525 auto group = obj->hasExtension(App::GeoFeatureGroupExtension::getExtensionClassTypeId())
526 ? obj
527 : getGroupOfObject(obj);
528 for (auto link : result) {
529 if (getGroupOfObject(link) != group) {
530 vec.push_back(link);
531 }
532 }
533
534 // for links with scope SubGroup we need to check if all features are part of subgroups
535 if (group) {
536 result = getScopedObjectsFromLinks(obj, LinkScope::Child);
537 auto groupExt = group->getExtensionByType<App::GeoFeatureGroupExtension>();
538 for (auto link : result) {
539 if (!groupExt->hasObject(link, true)) {
540 vec.push_back(link);
541 }
542 }
543 }
544 }
545
546 bool GeoFeatureGroupExtension::extensionGetSubObjects(std::vector<std::string>& ret, int) const
547 {
548 for (auto obj : Group.getValues()) {
549 if (obj && obj->isAttachedToDocument() && !obj->testStatus(ObjectStatus::GeoExcluded)) {
550 ret.push_back(std::string(obj->getNameInDocument()) + '.');
551 }
552 }
553 return true;
554 }
555
556
557 // Python feature ---------------------------------------------------------
558
559 namespace App
560 {
561 EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::GeoFeatureGroupExtensionPython,
562 App::GeoFeatureGroupExtension)
563
564 // explicit template instantiation
565 template class AppExport ExtensionPythonT<GroupExtensionPythonT<GeoFeatureGroupExtension>>;
566 } // namespace App