Salome HOME
Implementation of shared faces inspection, bos #29568
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_GroupSharedFaces.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_GroupSharedFaces.h"
21
22 #include <ModelAPI_AttributeSelection.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeBoolean.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <Locale_Convert.h>
32
33 #include <sstream>
34
35 //=================================================================================================
36 FeaturesPlugin_GroupSharedFaces::FeaturesPlugin_GroupSharedFaces()
37 {
38 }
39
40 //=================================================================================================
41 void FeaturesPlugin_GroupSharedFaces::initAttributes()
42 {
43   data()->addAttribute(OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
44   data()->addAttribute(LIST_FACES_ID(), ModelAPI_AttributeSelectionList::typeId());
45   data()->addAttribute(NUMBER_FACES_ID(), ModelAPI_AttributeString::typeId());
46   data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeInteger::typeId());
47   data()->addAttribute(GROUP_NAME_ID(), ModelAPI_AttributeString::typeId());
48   data()->addAttribute(COMPUTE_ID(), ModelAPI_AttributeBoolean::typeId());
49
50   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TRANSPARENCY_ID());
51   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMPUTE_ID());
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), NUMBER_FACES_ID());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LIST_FACES_ID());
54
55   data()->boolean(COMPUTE_ID())->setValue(true);
56 }
57
58 //=================================================================================================
59 AttributePtr FeaturesPlugin_GroupSharedFaces::attributObject()
60 {
61   return attribute(OBJECT_ID());
62 }
63
64 //=================================================================================================
65 AttributePtr FeaturesPlugin_GroupSharedFaces::attributIsCompute()
66 {
67   return attribute(COMPUTE_ID());
68 }
69
70 //=================================================================================================
71 AttributePtr FeaturesPlugin_GroupSharedFaces::attributListFaces()
72 {
73   return attribute(LIST_FACES_ID());
74 }
75
76 //=================================================================================================
77 AttributePtr FeaturesPlugin_GroupSharedFaces::attributNumberFaces()
78 {
79   return attribute(NUMBER_FACES_ID());
80 }
81
82 //=================================================================================================
83 void FeaturesPlugin_GroupSharedFaces::execute()
84 {
85   if (selectionList(LIST_FACES_ID())->isInitialized()
86     && string(GROUP_NAME_ID())->value() != "") {
87     AttributeStringPtr aNameAtt = string(GROUP_NAME_ID()) ;
88     std::wstring aNameFace = aNameAtt->isUValue() ?
89                              Locale::Convert::toWString(aNameAtt->valueU()) :
90                              Locale::Convert::toWString(aNameAtt->value());
91
92     if (lastResult().get())
93       eraseResultFromList(lastResult());
94     setFacesGroup(aNameFace);
95   } else {
96     if (lastResult().get()) {
97       eraseResultFromList(lastResult());
98     }
99   }
100   if (selection(OBJECT_ID())->isInitialized() && integer(TRANSPARENCY_ID())->isInitialized()) {
101     AttributeSelectionPtr aCompSolidAttr = selection(OBJECT_ID());
102     ResultPtr aResult = aCompSolidAttr->context();
103
104     double aTranparency = integer(TRANSPARENCY_ID())->value() / 100.0;
105     ModelAPI_Tools::setTransparency(aResult, aTranparency);
106
107     ResultBodyPtr aResultBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
108     std::list<ResultPtr> allRes;
109     ModelAPI_Tools::allSubs(aResultBody, allRes);
110     std::list<ResultPtr>::iterator aRes;
111     for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
112       ModelAPI_Tools::setTransparency(*aRes, aTranparency);
113     }
114   }
115
116   // Update the number of shared faces
117   AttributeSelectionListPtr aFacesListAttr =
118     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList> (attributListFaces());
119   AttributeStringPtr aNumberFacesAttr =
120     std::dynamic_pointer_cast<ModelAPI_AttributeString> (attributNumberFaces());
121   std::stringstream aLabel;
122   aLabel << aFacesListAttr->size();
123   aNumberFacesAttr->setValue(aLabel.str());
124 }
125
126 //=================================================================================================
127 void FeaturesPlugin_GroupSharedFaces::attributeChanged(const std::string& theID)
128 {
129   if (theID == OBJECT_ID() || theID == LIST_FACES_ID())
130     updateFaces();
131 }