Salome HOME
Copyright update 2022
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Tools.cpp
1 // Copyright (C) 2014-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_Tools.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30
31 #include <GeomAPI_PlanarEdges.h>
32 #include <GeomAPI_ShapeIterator.h>
33
34 #include <GeomValidators_ShapeType.h>
35
36 //==================================================================================================
37 bool FeaturesPlugin_Tools::getShape(const AttributeSelectionListPtr theSelectionList,
38                                     const bool theShareTopology,
39                                     ListOfShape& theShapesList,
40                                     std::string& theError)
41 {
42   theShapesList.clear();
43
44   ListOfShape aBaseFacesList;
45   std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
46   if(!theSelectionList.get()) {
47     theError = "Error: Could not get base objects selection list.";
48     return false;
49   }
50   if(theSelectionList->size() == 0) {
51     theError = "Error: Base objects list is empty.";
52     return false;
53   }
54   for(int anIndex = 0; anIndex < theSelectionList->size(); anIndex++) {
55     AttributeSelectionPtr aBaseObjectSelection = theSelectionList->value(anIndex);
56     if(!aBaseObjectSelection.get()) {
57       theError = "Error: Selected base object is empty.";
58       return false;
59     }
60     GeomShapePtr aBaseShape = aBaseObjectSelection->value();
61     if(aBaseShape.get() && !aBaseShape->isNull()) {
62       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
63       if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
64         theError = "Error: Selected shapes has unsupported type.";
65         return false;
66       }
67       ResultConstructionPtr aConstruction =
68         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
69       if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
70           aST == GeomAPI_Shape::WIRE) {
71         // It is a wire on the sketch, store it to make face later.
72         aSketchWiresMap[aConstruction].push_back(aBaseShape);
73         continue;
74       } else {
75       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
76                                    theShapesList.push_back(aBaseShape);
77       }
78     } else {
79       // This may be the whole sketch result selected, check and get faces.
80       ResultConstructionPtr aConstruction =
81         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
82       if(!aConstruction.get()) {
83         theError = "Error: Selected sketches does not have results.";
84         return false;
85       }
86       GeomValidators_ShapeType::TypeOfShape aSelType =
87           GeomValidators_ShapeType::shapeType(theSelectionList->selectionType());
88       int aFacesNum = 0;
89       if (aSelType != GeomValidators_ShapeType::Vertex &&
90           aSelType != GeomValidators_ShapeType::Edge)
91         aFacesNum = aConstruction->facesNum();
92       if(aFacesNum == 0) {
93         // Probably it can be construction.
94         aBaseShape = aConstruction->shape();
95         if(aBaseShape.get() && !aBaseShape->isNull()) {
96           GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
97           if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
98             theError = "Error: Selected shapes has unsupported type.";
99             return false;
100           }
101           aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
102                                        theShapesList.push_back(aBaseShape);
103         }
104       } else {
105         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
106           GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
107           if(!aBaseFace.get() || aBaseFace->isNull()) {
108             theError = "Error: One of the faces on selected sketch is null.";
109             return false;
110           }
111           aBaseFacesList.push_back(aBaseFace);
112         }
113       }
114     }
115   }
116
117   // Make faces from sketch wires.
118   for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
119       anIt != aSketchWiresMap.cend(); ++anIt) {
120     const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
121         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
122     const ListOfShape& aWiresList = (*anIt).second;
123     ListOfShape aFaces;
124     GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
125                                                aSketchPlanarEdges->norm(),
126                                                aWiresList,
127                                                aFaces);
128     aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
129   }
130
131   // Searching faces with common edges.
132   if(theShareTopology && aBaseFacesList.size() > 1) {
133     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
134     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, theShapesList);
135   } else {
136     theShapesList.insert(theShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
137   }
138   return true;
139 }
140
141 //==================================================================================================
142 bool FeaturesPlugin_Tools::shapesFromSelectionList(
143     const std::shared_ptr<ModelAPI_AttributeSelectionList> theSelectionList,
144     const bool theStoreFullHierarchy,
145     GeomAPI_ShapeHierarchy& theHierarchy,
146     std::list<ResultPtr>& theParts, ResultPtr& theTextureSource)
147 {
148   int aSize = theSelectionList->size();
149   if (aSize == 1) {
150     auto anObjectAttr = theSelectionList->value(0);
151     if (anObjectAttr.get()) {
152       FeaturePtr aFeature = anObjectAttr->contextFeature();
153       if (aFeature.get() && aFeature->results().size() == 1) {
154         theTextureSource = aFeature->firstResult();
155       }
156       else {
157         if (!aFeature.get()) {
158           auto aResult = anObjectAttr->context();
159           if (aResult.get()) {
160             theTextureSource = aResult;
161           }
162         }
163       }
164     }
165   }
166   for (int anObjectsIndex = 0; anObjectsIndex < aSize; anObjectsIndex++) {
167     AttributeSelectionPtr anObjectAttr = theSelectionList->value(anObjectsIndex);
168     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
169     if (!anObject.get()) { // may be for not-activated parts
170       return false;
171     }
172     ResultPtr aContext = anObjectAttr->context();
173     if (aContext && aContext->groupName() == ModelAPI_ResultPart::group())
174       theParts.push_back(aContext);
175     else {
176       // store full shape hierarchy for the corresponding version only
177       theHierarchy.addObject(anObject);
178       if (theStoreFullHierarchy)
179         ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theHierarchy);
180     }
181   }
182   return true;
183 }