1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: XGUI_CustomPrs.cpp
4 // Created: 10 Mar 2015
5 // Author: Natalia ERMOLAEVA
7 #include <XGUI_CustomPrs.h>
8 #include <XGUI_Workshop.h>
9 #include <XGUI_Displayer.h>
11 #include <ModuleBase_IModule.h>
13 #include <ModelAPI_AttributeIntArray.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_ResultConstruction.h>
19 #include <GeomAPI_ShapeExplorer.h>
21 #include <Config_PropManager.h>
23 #include <Events_InfoMessage.h>
28 double getDeflection(const ResultPtr& theResult)
30 double aDeflection = -1;
31 // get color from the attribute of the result
32 if (theResult.get() != NULL &&
33 theResult->data()->attribute(ModelAPI_Result::DEFLECTION_ID()).get() != NULL) {
34 AttributeDoublePtr aDoubleAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
35 if (aDoubleAttr.get() && aDoubleAttr->isInitialized()) {
36 double aValue = aDoubleAttr->value();
37 if (aValue > 0) /// zero value should not be used as a deflection(previous studies)
38 aDeflection = aDoubleAttr->value();
44 void getColor(const ResultPtr& theResult, std::vector<int>& theColor)
47 // get color from the attribute of the result
48 if (theResult.get() != NULL &&
49 theResult->data()->attribute(ModelAPI_Result::COLOR_ID()).get() != NULL) {
50 AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
51 if (aColorAttr.get() && aColorAttr->size()) {
52 theColor.push_back(aColorAttr->value(0));
53 theColor.push_back(aColorAttr->value(1));
54 theColor.push_back(aColorAttr->value(2));
59 void XGUI_CustomPrs::getDefaultColor(ObjectPtr theObject, const bool isEmptyColorValid,
60 std::vector<int>& theColor)
63 // get default color from the preferences manager for the given result
64 if (theColor.empty()) {
65 std::string aSection, aName, aDefault;
66 theObject->colorConfigInfo(aSection, aName, aDefault);
67 if (!aSection.empty() && !aName.empty()) {
68 theColor = Config_PropManager::color(aSection, aName);
71 if (!isEmptyColorValid && theColor.empty()) {
72 // all AIS objects, where the color is not set, are in black.
73 // The color should be defined in XML or set in the attribute
74 theColor = Config_PropManager::color("Visualization", "object_default_color");
75 Events_InfoMessage("XGUI_CustomPrs",
76 "A default color is not defined in the preferences for this kind of result").send();
80 double XGUI_CustomPrs::getDefaultDeflection(const ObjectPtr& theObject)
82 double aDeflection = -1;
83 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
85 bool isConstruction = false;
87 std::string aResultGroup = aResult->groupName();
88 if (aResultGroup == ModelAPI_ResultConstruction::group())
89 isConstruction = true;
90 else if (aResultGroup == ModelAPI_ResultBody::group()) {
91 GeomShapePtr aGeomShape = aResult->shape();
92 if (aGeomShape.get()) {
93 // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
94 // correction of deviation for them should not influence to the application performance
95 GeomAPI_ShapeExplorer anExp(aGeomShape, GeomAPI_Shape::FACE);
96 isConstruction = !anExp.more();
100 aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
102 aDeflection = Config_PropManager::real("Visualization", "body_deflection");
107 XGUI_CustomPrs::XGUI_CustomPrs(XGUI_Workshop* theWorkshop)
108 : myWorkshop(theWorkshop)
112 void XGUI_CustomPrs::getResultColor(const ResultPtr& theResult, std::vector<int>& theColor)
114 getColor(theResult, theColor);
115 if (theColor.empty())
116 getDefaultColor(theResult, false, theColor);
119 double XGUI_CustomPrs::getResultDeflection(const ResultPtr& theResult)
121 double aDeflection = getDeflection(theResult);
123 aDeflection = getDefaultDeflection(theResult);
127 bool XGUI_CustomPrs::customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
128 std::shared_ptr<GeomAPI_ICustomPrs> theCustomPrs)
130 bool aCustomized = false;
131 if (theResult.get()) {
132 std::vector<int> aColor;
133 getResultColor(theResult, aColor);
135 SessionPtr aMgr = ModelAPI_Session::get();
136 if (aMgr->activeDocument() != theResult->document()) {
137 QColor aQColor(aColor[0], aColor[1], aColor[2]);
139 QColor::fromHsvF(aQColor.hueF(), aQColor.saturationF()/3., aQColor.valueF());
140 aColor[0] = aNewColor.red();
141 aColor[1] = aNewColor.green();
142 aColor[2] = aNewColor.blue();
144 aCustomized = !aColor.empty() && thePrs->setColor(aColor[0], aColor[1], aColor[2]);
146 aCustomized = thePrs->setDeflection(getResultDeflection(theResult)) | aCustomized;
148 ModuleBase_IModule* aModule = myWorkshop->module();
149 aCustomized = aModule->customisePresentation(theResult, thePrs, theCustomPrs) || aCustomized;