Salome HOME
Copyright update 2022
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedChFi.cpp
1 // Copyright (C) 2017-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_VersionedChFi.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_Tools.h>
24
25 #include <GeomAlgoAPI_MakeShapeList.h>
26
27 #include <GeomAPI_ShapeExplorer.h>
28
29
30 static const std::string CHAMFERFILLET_VERSION_1("v9.5");
31
32
33 // Obtain all sub-shapes from the shape and append them to the list
34 static void collectSubs(const GeomShapePtr& theShape,
35                               ListOfShape& theSubs,
36                         const GeomAPI_Shape::ShapeType theShapeType)
37 {
38   GeomAPI_ShapeExplorer anExp(theShape, theShapeType);
39   for (; anExp.more(); anExp.next()) {
40     GeomShapePtr aShape = anExp.current();
41     // Store all shapes with FORWARD orientation to avoid duplication of shared edges/vertices
42     aShape->setOrientation(GeomAPI_Shape::FORWARD);
43     theSubs.push_back(aShape);
44   }
45 }
46
47
48 void FeaturesPlugin_VersionedChFi::initVersion(const AttributePtr& theObjectsAttr)
49 {
50   if (!theObjectsAttr->isInitialized()) {
51     // new feature, not read from file
52     data()->setVersion(CHAMFERFILLET_VERSION_1);
53   }
54 }
55
56 void FeaturesPlugin_VersionedChFi::execute()
57 {
58   GeomAPI_ShapeHierarchy anObjectHierarchy;
59   if (!processAttribute(objectsAttribute(), anObjectHierarchy))
60     return;
61
62   // Perform the operation
63   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
64   int aResultIndex = 0;
65   std::string anError;
66
67   ListOfShape anEdges;
68   ListOfShape anOriginalSolids;
69   for (GeomAPI_ShapeHierarchy::iterator anIt = anObjectHierarchy.begin();
70        anIt != anObjectHierarchy.end(); ++anIt) {
71     GeomShapePtr aSolid = anObjectHierarchy.parent(*anIt);
72     const ListOfShape& aSubs = anObjectHierarchy.objects(aSolid);
73
74     std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo = performOperation(aSolid, aSubs);
75     if (!anAlgo)
76       return; // something gone wrong
77
78     anObjectHierarchy.markModified(aSolid, anAlgo->shape());
79     aMakeShapeList->appendAlgo(anAlgo);
80
81     anOriginalSolids.push_back(aSolid);
82     anEdges.insert(anEdges.end(), aSubs.begin(), aSubs.end());
83   }
84
85   // Build results of the operaion.
86   const std::string& aPrefix = modifiedShapePrefix();
87   ListOfShape aTopLevel;
88   anObjectHierarchy.topLevelObjects(aTopLevel);
89   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
90     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
91     ModelAPI_Tools::loadModifiedShapes(aResultBody, anOriginalSolids, ListOfShape(),
92                                        aMakeShapeList, *anIt, aPrefix);
93     setResult(aResultBody, aResultIndex++);
94
95     for (ListOfShape::iterator aEIt = anEdges.begin(); aEIt != anEdges.end(); ++aEIt) {
96       GeomShapePtr aBase = *aEIt;
97       // Store new faces generated from edges and vertices
98       aResultBody->loadGeneratedShapes(aMakeShapeList, aBase, GeomAPI_Shape::EDGE, aPrefix, true);
99     }
100
101     ModelAPI_Tools::loadDeletedShapes(aResultBody, GeomShapePtr(), anOriginalSolids,
102                                       aMakeShapeList, *anIt);
103   }
104
105   removeResults(aResultIndex);
106 }
107
108
109 bool FeaturesPlugin_VersionedChFi::processAttribute(const AttributePtr& theAttribute,
110                                                     GeomAPI_ShapeHierarchy& theObjects)
111 {
112   bool isStoreFullHierarchy = data()->version() == CHAMFERFILLET_VERSION_1;
113
114   AttributeSelectionListPtr anObjectsSelList =
115       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
116   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); ++anObjectsIndex) {
117     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
118     GeomShapePtr anObject = anObjectAttr->value();
119     if (!anObject)
120       return false;
121
122     ResultPtr aContext = anObjectAttr->context();
123     GeomShapePtr aParent;
124     if (aContext.get()) {
125       ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext);
126       if (aCtxOwner && aCtxOwner->shape()->shapeType() == GeomAPI_Shape::COMPSOLID)
127         aContext = aCtxOwner;
128       aParent = aContext->shape();
129       if (!aParent)
130         return false;
131
132       // store full shape hierarchy for the corresponding version only
133       theObjects.addObject(anObject);
134       theObjects.addParent(anObject, aParent);
135       if (isStoreFullHierarchy)
136         ModelAPI_Tools::fillShapeHierarchy(aParent, aContext, theObjects);
137     } else { // get it from a feature
138       FeaturePtr aFeature = anObjectAttr->contextFeature();
139       if (aFeature.get()) {
140         aParent = aFeature->firstResult()->shape();
141         if (!aParent)
142           return false;
143
144         ListOfShape anEdges;
145         collectSubs(aParent, anEdges, GeomAPI_Shape::EDGE);
146         for (ListOfShape::iterator anIt = anEdges.begin(); anIt != anEdges.end(); ++anIt) {
147           theObjects.addObject(*anIt);
148           theObjects.addParent(*anIt, aParent);
149         }
150       }
151     }
152   }
153   return true;
154 }