Salome HOME
Copyright update 2020
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedChFi.cpp
1 // Copyright (C) 2017-2020  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 #include "FeaturesPlugin_Tools.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
25
26 #include <GeomAlgoAPI_MakeShapeList.h>
27
28 #include <GeomAPI_ShapeExplorer.h>
29
30
31 static const std::string CHAMFERFILLET_VERSION_1("v9.5");
32
33
34 // Obtain all sub-shapes from the shape and append them to the list
35 static void collectSubs(const GeomShapePtr& theShape,
36                               ListOfShape& theSubs,
37                         const GeomAPI_Shape::ShapeType theShapeType)
38 {
39   GeomAPI_ShapeExplorer anExp(theShape, theShapeType);
40   for (; anExp.more(); anExp.next()) {
41     GeomShapePtr aShape = anExp.current();
42     // Store all shapes with FORWARD orientation to avoid duplication of shared edges/vertices
43     aShape->setOrientation(GeomAPI_Shape::FORWARD);
44     theSubs.push_back(aShape);
45   }
46 }
47
48
49 void FeaturesPlugin_VersionedChFi::initVersion(const AttributePtr& theObjectsAttr)
50 {
51   if (!theObjectsAttr->isInitialized()) {
52     // new feature, not read from file
53     data()->setVersion(CHAMFERFILLET_VERSION_1);
54   }
55 }
56
57 void FeaturesPlugin_VersionedChFi::execute()
58 {
59   GeomAPI_ShapeHierarchy anObjectHierarchy;
60   if (!processAttribute(objectsAttribute(), anObjectHierarchy))
61     return;
62
63   // Perform the operation
64   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
65   int aResultIndex = 0;
66   std::string anError;
67
68   ListOfShape anEdges;
69   ListOfShape anOriginalSolids;
70   for (GeomAPI_ShapeHierarchy::iterator anIt = anObjectHierarchy.begin();
71        anIt != anObjectHierarchy.end(); ++anIt) {
72     GeomShapePtr aSolid = anObjectHierarchy.parent(*anIt);
73     const ListOfShape& aSubs = anObjectHierarchy.objects(aSolid);
74
75     std::shared_ptr<GeomAlgoAPI_MakeShape> anAlgo = performOperation(aSolid, aSubs);
76     if (!anAlgo)
77       return; // something gone wrong
78
79     anObjectHierarchy.markModified(aSolid, anAlgo->shape());
80     aMakeShapeList->appendAlgo(anAlgo);
81
82     anOriginalSolids.push_back(aSolid);
83     anEdges.insert(anEdges.end(), aSubs.begin(), aSubs.end());
84   }
85
86   // Build results of the operaion.
87   const std::string& aPrefix = modifiedShapePrefix();
88   ListOfShape aTopLevel;
89   anObjectHierarchy.topLevelObjects(aTopLevel);
90   for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) {
91     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
92     FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalSolids, ListOfShape(),
93                                              aMakeShapeList, *anIt, aPrefix);
94     setResult(aResultBody, aResultIndex++);
95
96     for (ListOfShape::iterator anIt = anEdges.begin(); anIt != anEdges.end(); ++anIt) {
97       GeomShapePtr aBase = *anIt;
98       // Store new faces generated from edges and vertices
99       aResultBody->loadGeneratedShapes(aMakeShapeList, aBase, GeomAPI_Shape::EDGE, aPrefix, true);
100     }
101
102     FeaturesPlugin_Tools::loadDeletedShapes(aResultBody, GeomShapePtr(), anOriginalSolids,
103                                             aMakeShapeList, *anIt);
104   }
105
106   removeResults(aResultIndex);
107 }
108
109
110 bool FeaturesPlugin_VersionedChFi::processAttribute(const AttributePtr& theAttribute,
111                                                     GeomAPI_ShapeHierarchy& theObjects)
112 {
113   bool isStoreFullHierarchy = data()->version() == CHAMFERFILLET_VERSION_1;
114
115   AttributeSelectionListPtr anObjectsSelList =
116       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
117   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); ++anObjectsIndex) {
118     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
119     GeomShapePtr anObject = anObjectAttr->value();
120     if (!anObject)
121       return false;
122
123     ResultPtr aContext = anObjectAttr->context();
124     GeomShapePtr aParent;
125     if (aContext.get()) {
126       ResultBodyPtr aCtxOwner = ModelAPI_Tools::bodyOwner(aContext);
127       if (aCtxOwner && aCtxOwner->shape()->shapeType() == GeomAPI_Shape::COMPSOLID)
128         aContext = aCtxOwner;
129       aParent = aContext->shape();
130       if (!aParent)
131         return false;
132
133       // store full shape hierarchy for the corresponding version only
134       theObjects.addObject(anObject);
135       theObjects.addParent(anObject, aParent);
136       if (isStoreFullHierarchy)
137         ModelAPI_Tools::fillShapeHierarchy(aParent, aContext, theObjects);
138     } else { // get it from a feature
139       FeaturePtr aFeature = anObjectAttr->contextFeature();
140       if (aFeature.get()) {
141         aParent = aFeature->firstResult()->shape();
142         if (!aParent)
143           return false;
144
145         ListOfShape anEdges;
146         collectSubs(aParent, anEdges, GeomAPI_Shape::EDGE);
147         for (ListOfShape::iterator anIt = anEdges.begin(); anIt != anEdges.end(); ++anIt) {
148           theObjects.addObject(*anIt);
149           theObjects.addParent(*anIt, aParent);
150         }
151       }
152     }
153   }
154   return true;
155 }