Salome HOME
15eebf6d580cc8e24249cea71b2af26fdac175fe
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_SharedFaces.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_SharedFaces.h"
21
22 #include <FeaturesPlugin_GroupSharedFaces.h>
23
24 #include <ModelAPI_AttributeBoolean.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Validator.h>
32
33 #include <sstream>
34
35 //=================================================================================================
36 FeaturesPlugin_SharedFaces::FeaturesPlugin_SharedFaces()
37 {
38 }
39
40 //=================================================================================================
41 void FeaturesPlugin_SharedFaces::initAttributes()
42 {
43   data()->addAttribute(OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
44   data()->addAttribute(LIST_FACES_ID(), ModelAPI_AttributeSelectionList::typeId());
45
46   data()->addAttribute(NUMBER_FACES_ID(), ModelAPI_AttributeString::typeId());
47   data()->addAttribute(TRANSPARENCY_ID(), ModelAPI_AttributeInteger::typeId());
48   data()->addAttribute(CREATE_GROUP_ID(), ModelAPI_AttributeBoolean::typeId());
49   data()->addAttribute(GROUP_NAME_ID(), ModelAPI_AttributeString::typeId());
50   data()->addAttribute(COMPUTE_ID(), ModelAPI_AttributeBoolean::typeId());
51
52   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), GROUP_NAME_ID());
53   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), COMPUTE_ID());
54   data()->boolean(COMPUTE_ID())->setValue(true);
55 }
56
57 //=================================================================================================
58 AttributePtr FeaturesPlugin_SharedFaces::attributIsCompute()
59 {
60   return attribute(COMPUTE_ID());
61 }
62
63 //=================================================================================================
64 AttributePtr FeaturesPlugin_SharedFaces::attributObject()
65 {
66   return attribute(OBJECT_ID());
67 }
68
69 //=================================================================================================
70 AttributePtr FeaturesPlugin_SharedFaces::attributListFaces()
71 {
72   return attribute(LIST_FACES_ID());
73 }
74
75 //=================================================================================================
76 AttributePtr FeaturesPlugin_SharedFaces::attributNumberFaces()
77 {
78   return attribute(NUMBER_FACES_ID());
79 }
80
81 //=================================================================================================
82 void FeaturesPlugin_SharedFaces::execute()
83 {
84   if (boolean(CREATE_GROUP_ID())->value()) {
85
86     if (string(GROUP_NAME_ID())->value() != ""
87       && selectionList(LIST_FACES_ID())->isInitialized()) {
88
89       if (lastResult().get()) {
90         eraseResultFromList(lastResult());
91       }
92
93       if (!myCreateGroupFeature.get())
94         createGroup();
95       updateGroup();
96     }
97
98   } else {
99     if (selectionList(LIST_FACES_ID())->isInitialized()) {
100
101       if (myCreateGroupFeature.get()) {
102         myCreateGroupFeature->eraseResults();
103         SessionPtr aSession = ModelAPI_Session::get();
104         DocumentPtr aDoc =  aSession->activeDocument();
105         aDoc->removeFeature(myCreateGroupFeature);
106         myCreateGroupFeature.reset();
107       }
108
109       if (lastResult().get())
110         eraseResultFromList(lastResult());
111       setFacesGroup(L"Group_SharedFaces");
112     }
113   }
114
115   if (selection(OBJECT_ID())->isInitialized()) {
116     AttributeSelectionPtr aCompSolidAttr = selection(OBJECT_ID());
117     ResultPtr aResult = aCompSolidAttr->context();
118
119     double aTranparency = integer(TRANSPARENCY_ID())->value()/100.0;
120     ModelAPI_Tools::setTransparency(aResult, aTranparency);
121
122     ResultBodyPtr aResultBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
123     std::list<ResultPtr> allRes;
124     ModelAPI_Tools::allSubs(aResultBody, allRes);
125     std::list<ResultPtr>::iterator aRes;
126     for(aRes = allRes.begin(); aRes != allRes.end(); aRes++) {
127       ModelAPI_Tools::setTransparency(*aRes, aTranparency);
128     }
129   }
130
131   // Update the number of shared faces
132   AttributeSelectionListPtr aFacesListAttr =
133     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList> (attributListFaces());
134   AttributeStringPtr aNumberFacesAttr =
135     std::dynamic_pointer_cast<ModelAPI_AttributeString> (attributNumberFaces());
136   std::stringstream aLabel;
137   aLabel << aFacesListAttr->size();
138   aNumberFacesAttr->setValue(aLabel.str());
139 }
140
141 //=================================================================================================
142 void FeaturesPlugin_SharedFaces::attributeChanged(const std::string& theID)
143 {
144   if (theID == OBJECT_ID()) {
145     updateFaces();
146     if (myCreateGroupFeature.get())
147       updateGroup();
148   }
149 }
150
151
152 //=================================================================================================
153 void FeaturesPlugin_SharedFaces::createGroup()
154 {
155   SessionPtr aSession = ModelAPI_Session::get();
156
157   DocumentPtr aDoc =  aSession->activeDocument();
158
159   if (aDoc.get()) {
160     myCreateGroupFeature = aDoc->addFeature(FeaturesPlugin_GroupSharedFaces::ID());
161   }
162 }
163
164 //=================================================================================================
165 void FeaturesPlugin_SharedFaces::updateGroup()
166 {
167     myCreateGroupFeature->boolean(FeaturesPlugin_GroupSharedFaces::COMPUTE_ID())->setValue(false);
168     myCreateGroupFeature->string(FeaturesPlugin_GroupSharedFaces::GROUP_NAME_ID())
169       ->setValue(string(GROUP_NAME_ID())->value());
170
171     myCreateGroupFeature->selection(FeaturesPlugin_GroupSharedFaces::OBJECT_ID())
172       ->setValue(selection(OBJECT_ID())->context(), selection(OBJECT_ID())->value());
173     AttributeSelectionListPtr aFacesFeatures =
174       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>
175                                (myCreateGroupFeature->attribute(LIST_FACES_ID()));
176
177     AttributeSelectionListPtr aFaces =
178       std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute(LIST_FACES_ID()));
179     aFaces->copyTo(aFacesFeatures);
180
181     myCreateGroupFeature->integer(FeaturesPlugin_GroupSharedFaces::TRANSPARENCY_ID())
182                           ->setValue( integer(TRANSPARENCY_ID())->value());
183     myCreateGroupFeature->execute();
184     myCreateGroupFeature->boolean(FeaturesPlugin_GroupSharedFaces::COMPUTE_ID())->setValue(true);
185 }