1 // Copyright (C) 2018-2024 CEA, EDF
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_SharedFaces.h"
22 #include <FeaturesPlugin_GroupSharedFaces.h>
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>
35 //=================================================================================================
36 FeaturesPlugin_SharedFaces::FeaturesPlugin_SharedFaces()
40 //=================================================================================================
41 void FeaturesPlugin_SharedFaces::initAttributes()
43 data()->addAttribute(OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
44 data()->addAttribute(LIST_FACES_ID(), ModelAPI_AttributeSelectionList::typeId());
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());
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);
57 //=================================================================================================
58 AttributePtr FeaturesPlugin_SharedFaces::attributIsCompute()
60 return attribute(COMPUTE_ID());
63 //=================================================================================================
64 AttributePtr FeaturesPlugin_SharedFaces::attributObject()
66 return attribute(OBJECT_ID());
69 //=================================================================================================
70 AttributePtr FeaturesPlugin_SharedFaces::attributListFaces()
72 return attribute(LIST_FACES_ID());
75 //=================================================================================================
76 AttributePtr FeaturesPlugin_SharedFaces::attributNumberFaces()
78 return attribute(NUMBER_FACES_ID());
81 //=================================================================================================
82 void FeaturesPlugin_SharedFaces::execute()
84 if (boolean(CREATE_GROUP_ID())->value()) {
86 if (string(GROUP_NAME_ID())->value() != ""
87 && selectionList(LIST_FACES_ID())->isInitialized()) {
89 if (lastResult().get()) {
90 eraseResultFromList(lastResult());
93 if (!myCreateGroupFeature.get())
99 if (selectionList(LIST_FACES_ID())->isInitialized()) {
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();
109 if (lastResult().get())
110 eraseResultFromList(lastResult());
111 setFacesGroup(L"Group_SharedFaces");
115 if (selection(OBJECT_ID())->isInitialized()) {
116 AttributeSelectionPtr aCompSolidAttr = selection(OBJECT_ID());
117 ResultPtr aResult = aCompSolidAttr->context();
119 double aTranparency = integer(TRANSPARENCY_ID())->value()/100.0;
120 ModelAPI_Tools::setTransparency(aResult, aTranparency);
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);
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());
141 //=================================================================================================
142 void FeaturesPlugin_SharedFaces::attributeChanged(const std::string& theID)
144 if (theID == OBJECT_ID()) {
146 if (myCreateGroupFeature.get())
152 //=================================================================================================
153 void FeaturesPlugin_SharedFaces::createGroup()
155 SessionPtr aSession = ModelAPI_Session::get();
157 DocumentPtr aDoc = aSession->activeDocument();
160 myCreateGroupFeature = aDoc->addFeature(FeaturesPlugin_GroupSharedFaces::ID());
164 //=================================================================================================
165 void FeaturesPlugin_SharedFaces::updateGroup()
167 myCreateGroupFeature->boolean(FeaturesPlugin_GroupSharedFaces::COMPUTE_ID())->setValue(false);
168 myCreateGroupFeature->string(FeaturesPlugin_GroupSharedFaces::GROUP_NAME_ID())
169 ->setValue(string(GROUP_NAME_ID())->value());
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()));
177 AttributeSelectionListPtr aFaces =
178 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute(LIST_FACES_ID()));
179 aFaces->copyTo(aFacesFeatures);
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);