1 // Copyright (C) 2014-2019 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 <XGUI_CustomPrs.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_Displayer.h>
24 #include <ModuleBase_IModule.h>
26 #include <ModelAPI_AttributeIntArray.h>
27 #include <ModelAPI_AttributeDouble.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_ResultConstruction.h>
32 #include <GeomAPI_ShapeExplorer.h>
34 #include <Config_PropManager.h>
36 #include <Events_InfoMessage.h>
41 double getDeflection(const ResultPtr& theResult)
43 double aDeflection = -1;
44 // get deflection from the attribute of the result
45 if (theResult.get() != NULL &&
46 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
47 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
48 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
49 double aValue = aDoubleAttr->value();
50 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
51 aDeflection = aDoubleAttr->value();
57 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
60 // get color from the attribute of the result
61 if (theResult.get() != NULL &&
62 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
63 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
64 if (aColorAttr.get() && aColorAttr->size()) {
65 theColor.push_back(aColorAttr->value(0));
66 theColor.push_back(aColorAttr->value(1));
67 theColor.push_back(aColorAttr->value(2));
72 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
73 std::vector<int>& theColor)
76 // get default color from the preferences manager for the given result
77 if (theColor.empty()) {
78 std::string aSection, aName, aDefault;
79 theObject->colorConfigInfo(aSection, aName, aDefault);
80 if (!aSection.empty() && !aName.empty()) {
81 theColor = Config_PropManager::color(aSection, aName);
84 if (!isEmptyColorValid && theColor.empty()) {
85 // all AIS objects, where the color is not set, are in black.
86 // The color should be defined in XML or set in the attribute
87 theColor = Config_PropManager::color("Visualization", "object_default_color");
88 Events_InfoMessage("XGUI_CustomPrs",
89 "A default color is not defined in the preferences for this kind of result").send();
93 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
95 double aDeflection = -1;
96 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
98 bool isConstruction = false;
100 std::string aResultGroup = aResult->groupName();
101 if (aResultGroup == ModelAPI_ResultConstruction::group())
102 isConstruction = true;
103 else if (aResultGroup == ModelAPI_ResultBody::group()) {
104 GeomShapePtr aGeomShape = aResult->shape();
105 if (aGeomShape.get()) {
106 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
107 // correction of deviation for them should not influence to the application performance
108 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
109 isConstruction = !anExp.more();
113 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
115 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
120 double getTransparency(const ResultPtr& theResult)
122 double aTransparency = -1;
123 // get transparency from the attribute of the result
124 if (theResult.get() != NULL &&
125 theResult->data()->attribute(ModelAPI_Result::TRANSPARENCY_ID()).get() != NULL) {
126 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::TRANSPARENCY_ID());
127 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
128 aTransparency = aDoubleAttr->value();
131 return aTransparency;
134 double getDefaultTransparency(const ResultPtr& theResult)
136 return Config_PropManager::real("Visualization", "default_transparency");
139 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
140 : myWorkshop(theWorkshop)
144 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
146 getColor(theResult, theColor);
147 if (theColor.empty())
148 getDefaultColor(theResult, false, theColor);
151 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
153 double aDeflection = getDeflection(theResult);
155 aDeflection = getDefaultDeflection(theResult);
159 double XGUI_CustomPrs::getResultTransparency(const ResultPtr& theResult)
161 double aTransparency = getTransparency(theResult);
162 if (aTransparency < 0)
163 aTransparency = getDefaultTransparency(theResult);
164 return aTransparency;
167 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
168 std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
170 bool aCustomized = false;
171 if (theResult.get()) {
172 std::vector<int> aColor;
173 getResultColor(theResult, aColor);
175 SessionPtr aMgr = ModelAPI_Session::get();
176 if (aMgr->activeDocument() != theResult->document()) {
177 QColor aQColor(aColor[0], aColor[1], aColor[2]);
179 QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
180 aColor[0] = aNewColor.red();
181 aColor[1] = aNewColor.green();
182 aColor[2] = aNewColor.blue();
184 aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
186 aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
188 aCustomized = thePrs->setTransparency(getResultTransparency(theResult)) | aCustomized;
190 ModuleBase_IModule* aModule = myWorkshop->module();
191 aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;