1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <XGUI_CustomPrs.h>
22 #include <XGUI_Workshop.h>
23 #include <XGUI_Displayer.h>
25 #include <ModuleBase_IModule.h>
27 #include <ModelAPI_AttributeIntArray.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_ResultConstruction.h>
33 #include <GeomAPI_ShapeExplorer.h>
35 #include <Config_PropManager.h>
37 #include <Events_InfoMessage.h>
42 double getDeflection(const ResultPtr& theResult)
44 double aDeflection = -1;
45 // get color from the attribute of the result
46 if (theResult.get() != NULL &&
47 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
48 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
49 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
50 double aValue = aDoubleAttr->value();
51 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
52 aDeflection = aDoubleAttr->value();
58 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
61 // get color from the attribute of the result
62 if (theResult.get() != NULL &&
63 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
64 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
65 if (aColorAttr.get() && aColorAttr->size()) {
66 theColor.push_back(aColorAttr->value(0));
67 theColor.push_back(aColorAttr->value(1));
68 theColor.push_back(aColorAttr->value(2));
73 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
74 std::vector<int>& theColor)
77 // get default color from the preferences manager for the given result
78 if (theColor.empty()) {
79 std::string aSection, aName, aDefault;
80 theObject->colorConfigInfo(aSection, aName, aDefault);
81 if (!aSection.empty() && !aName.empty()) {
82 theColor = Config_PropManager::color(aSection, aName);
85 if (!isEmptyColorValid && theColor.empty()) {
86 // all AIS objects, where the color is not set, are in black.
87 // The color should be defined in XML or set in the attribute
88 theColor = Config_PropManager::color("Visualization", "object_default_color");
89 Events_InfoMessage("XGUI_CustomPrs",
90 "A default color is not defined in the preferences for this kind of result").send();
94 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
96 double aDeflection = -1;
97 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
99 bool isConstruction = false;
101 std::string aResultGroup = aResult->groupName();
102 if (aResultGroup == ModelAPI_ResultConstruction::group())
103 isConstruction = true;
104 else if (aResultGroup == ModelAPI_ResultBody::group()) {
105 GeomShapePtr aGeomShape = aResult->shape();
106 if (aGeomShape.get()) {
107 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
108 // correction of deviation for them should not influence to the application performance
109 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
110 isConstruction = !anExp.more();
114 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
116 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
121 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
122 : myWorkshop(theWorkshop)
126 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
128 getColor(theResult, theColor);
129 if (theColor.empty())
130 getDefaultColor(theResult, false, theColor);
133 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
135 double aDeflection = getDeflection(theResult);
137 aDeflection = getDefaultDeflection(theResult);
141 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
142 std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
144 bool aCustomized = false;
145 if (theResult.get()) {
146 std::vector<int> aColor;
147 getResultColor(theResult, aColor);
149 SessionPtr aMgr = ModelAPI_Session::get();
150 if (aMgr->activeDocument() != theResult->document()) {
151 QColor aQColor(aColor[0], aColor[1], aColor[2]);
153 QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
154 aColor[0] = aNewColor.red();
155 aColor[1] = aNewColor.green();
156 aColor[2] = aNewColor.blue();
158 aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
160 aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
162 ModuleBase_IModule* aModule = myWorkshop->module();
163 aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;