Salome HOME
8c154ed14d82c3648cb74d7d70b4e22098ad722c
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_CommonSharedFaces.cpp
1 // Copyright (C) 2018-2021  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_CommonSharedFaces.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30
31 #include <sstream>
32
33 //=================================================================================================
34 void FeaturesPlugin_CommonSharedFaces::updateFaces()
35 {
36   AttributeSelectionPtr aCompSolidAttr =
37     std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(attributObject());
38   AttributeSelectionListPtr aFacesListAttr =
39     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList> (attributListFaces());
40
41   GeomShapePtr aShape = aCompSolidAttr->value();
42
43   AttributeBooleanPtr anIsCompute =
44     std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attributIsCompute());
45   if (!anIsCompute->value()) {
46     myShape = aShape;
47     anIsCompute->setValue(true);
48   }
49   if (aShape.get() && aCompSolidAttr->context().get() && !aShape->isEqual(myShape)) {
50     if (aFacesListAttr->isInitialized())
51         aFacesListAttr->clear();
52     aShape = aCompSolidAttr->context()->shape();
53     if (aShape) {
54       ListOfShape aFaces = GeomAlgoAPI_ShapeTools::getSharedFaces(aShape);
55       myShape = aShape;
56       aFacesListAttr->setSelectionType("face");
57       ListOfShape::const_iterator anIt = aFaces.cbegin();
58       for(; anIt != aFaces.cend(); ++anIt) {
59         GeomShapePtr aFacePtr = *anIt;
60         if (!(aFacesListAttr->isInList(aCompSolidAttr->context(), aFacePtr))) {
61           aFacesListAttr->append(aCompSolidAttr->context(), aFacePtr);
62         }
63       }
64       std::stringstream aLabel;
65       aLabel << aFacesListAttr->size();
66       AttributeStringPtr aNumberFacesAttr =
67         std::dynamic_pointer_cast<ModelAPI_AttributeString>(attributNumberFaces());
68       aNumberFacesAttr->setValue(aLabel.str());
69     }
70   }
71 }
72
73 //=================================================================================================
74 void FeaturesPlugin_CommonSharedFaces::setFacesGroup(const std::wstring& theName)
75 {
76   std::vector<int> aColor;
77   ResultGroupPtr aGroup = document()->createGroup(data());
78   // Clean the result of the operation
79   aGroup->data()->setName(theName);
80   aGroup->store(GeomShapePtr());
81
82   // shapes containing in group
83   ListOfShape aFaces;
84   AttributeSelectionListPtr aFacesListAttr =
85     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList> (attributListFaces());
86
87   for (int anI =0; anI< aFacesListAttr->size(); anI++) {
88     AttributeSelectionPtr aAtt = aFacesListAttr->value(anI);
89     aFaces.push_back(aAtt->value());
90   }
91
92   GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
93   aGroup->store(aCompound);
94   aColor = {255,0,0};
95   setResult(aGroup);
96   ModelAPI_Tools::setColor(lastResult(),aColor);
97 }
98