]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_CustomPrs.cpp
Salome HOME
c14f9932529cd5a939a710a6132906ed609414d5
[modules/shaper.git] / src / XGUI / XGUI_CustomPrs.cpp
1 // Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include <XGUI_CustomPrs.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_Displayer.h>
23
24 #include <ModuleBase_IModule.h>
25
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>
31
32 #include <GeomAPI_ShapeExplorer.h>
33
34 #include <Config_PropManager.h>
35
36 #include <Events_InfoMessage.h>
37
38 #include <vector>
39 #include <QColor>
40
41 double getDeflection(const ResultPtr& theResult)
42 {
43   double aDeflection = -1;
44   // get color 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();
52     }
53   }
54   return aDeflection;
55 }
56
57 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
58 {
59   theColor.clear();
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));
68     }
69   }
70 }
71
72 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
73                                      std::vector<int>& theColor)
74 {
75   theColor.clear();
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);
82     }
83   }
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();
90   }
91 }
92
93 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
94 {
95   double aDeflection = -1;
96   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
97   if (aResult.get()) {
98     bool isConstruction = false;
99
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();
110       }
111     }
112     if (isConstruction)
113       aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
114     else
115       aDeflection = Config_PropManager::real("Visualization", "body_deflection");
116   }
117   return aDeflection;
118 }
119
120 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
121 : myWorkshop(theWorkshop)
122 {
123 }
124
125 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
126 {
127   getColor(theResult, theColor);
128   if (theColor.empty())
129     getDefaultColor(theResult, false, theColor);
130 }
131
132 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
133 {
134   double aDeflection = getDeflection(theResult);
135   if (aDeflection < 0)
136     aDeflection = getDefaultDeflection(theResult);
137   return aDeflection;
138 }
139
140 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
141                                            std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
142 {
143   bool aCustomized = false;
144   if (theResult.get()) {
145     std::vector<int> aColor;
146     getResultColor(theResult, aColor);
147
148     SessionPtr aMgr = ModelAPI_Session::get();
149     if (aMgr->activeDocument() != theResult->document()) {
150       QColor aQColor(aColor[0], aColor[1], aColor[2]);
151       QColor aNewColor =
152         QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
153       aColor[0] = aNewColor.red();
154       aColor[1] = aNewColor.green();
155       aColor[2] = aNewColor.blue();
156     }
157     aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
158
159     aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
160   }
161   ModuleBase_IModule* aModule = myWorkshop->module();
162   aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;
163   return aCustomized;
164 }