1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_Tools.h"
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>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAPI_PlanarEdges.h>
32 #include <GeomAPI_ShapeIterator.h>
34 #include <GeomValidators_ShapeType.h>
36 //==================================================================================================
37 bool FeaturesPlugin_Tools::getShape(const AttributeSelectionListPtr theSelectionList,
38 const bool theShareTopology,
39 ListOfShape& theShapesList,
40 std::string& theError)
42 theShapesList.clear();
44 ListOfShape aBaseFacesList;
45 std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
46 if(!theSelectionList.get()) {
47 theError = "Error: Could not get base objects selection list.";
50 if(theSelectionList->size() == 0) {
51 theError = "Error: Base objects list is empty.";
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.";
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.";
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);
75 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
76 theShapesList.push_back(aBaseShape);
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.";
86 GeomValidators_ShapeType::TypeOfShape aSelType =
87 GeomValidators_ShapeType::shapeType(theSelectionList->selectionType());
89 if (aSelType != GeomValidators_ShapeType::Vertex &&
90 aSelType != GeomValidators_ShapeType::Edge)
91 aFacesNum = aConstruction->facesNum();
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.";
101 aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
102 theShapesList.push_back(aBaseShape);
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.";
111 aBaseFacesList.push_back(aBaseFace);
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;
124 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
125 aSketchPlanarEdges->norm(),
128 aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
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);
136 theShapesList.insert(theShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
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)
148 int aSize = theSelectionList->size();
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();
157 if (!aFeature.get()) {
158 auto aResult = anObjectAttr->context();
160 theTextureSource = aResult;
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
172 ResultPtr aContext = anObjectAttr->context();
173 if (aContext && aContext->groupName() == ModelAPI_ResultPart::group())
174 theParts.push_back(aContext);
176 // store full shape hierarchy for the corresponding version only
177 theHierarchy.addObject(anObject);
178 if (theStoreFullHierarchy)
179 ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theHierarchy);