Salome HOME
5cd38b33b91be11bba776bda02e9dbd075d009b2
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_CustomPrs.cpp
4 // Created:     30 Jun 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <PartSet_CustomPrs.h>
8 #include <PartSet_Module.h>
9 #include "PartSet_OperationPrs.h"
10
11 #include <XGUI_ModuleConnector.h>
12 #include <XGUI_Workshop.h>
13 #include <XGUI_Displayer.h>
14
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_IViewer.h>
17
18 #include <GeomValidators_Tools.h>
19
20 #include <Config_PropManager.h>
21
22 #include <AIS_InteractiveContext.hxx>
23 #include <AIS_InteractiveObject.hxx>
24 #include <Prs3d_PointAspect.hxx>
25
26 #define OPERATION_PARAMETER_COLOR "255, 255, 0"
27
28 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
29   : myWorkshop(theWorkshop)
30 {
31   initPrs();
32 }
33
34 bool PartSet_CustomPrs::isActive()
35 {
36   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
37   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
38
39   return aContext->IsDisplayed(anOperationPrs);
40 }
41
42 void PartSet_CustomPrs::activate(const FeaturePtr& theFeature)
43 {
44   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
45
46   if (anOperationPrs->canActivate(theFeature)) {
47     anOperationPrs->setFeature(theFeature);
48     if (theFeature.get())
49       displayPresentation();
50   }
51 }
52
53 void PartSet_CustomPrs::deactivate()
54 {
55   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
56   anOperationPrs->setFeature(FeaturePtr());
57
58   erasePresentation();
59 }
60
61
62 void PartSet_CustomPrs::displayPresentation()
63 {
64   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
65
66   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
67   if (!aContext->IsDisplayed(anOperationPrs)) {
68     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
69
70     XGUI_Workshop* aWorkshop = workshop();
71     aWorkshop->displayer()->displayAIS(myOperationPrs, false/*load object in selection*/, true);
72     aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
73   }
74   else
75     anOperationPrs->Redisplay();
76 }
77
78 void PartSet_CustomPrs::erasePresentation()
79 {
80   XGUI_Workshop* aWorkshop = workshop();
81   aWorkshop->displayer()->eraseAIS(myOperationPrs, true);
82 }
83
84 Handle(PartSet_OperationPrs) PartSet_CustomPrs::getPresentation()
85 {
86   if (!myOperationPrs.get())
87     initPrs();
88   Handle(AIS_InteractiveObject) anAISIO = myOperationPrs->impl<Handle(AIS_InteractiveObject)>();
89   return Handle(PartSet_OperationPrs)::DownCast(anAISIO);
90 }
91
92 void PartSet_CustomPrs::customize(const ObjectPtr& theObject)
93 {
94   // the presentation should be recomputed if the previous AIS depend on the result
95   // [it should be hiddend] or the new AIS depend on it [it should be visualized]
96   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
97   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
98   if (aContext->IsDisplayed(anOperationPrs)) {
99     // if there are performance poblems, to improve them, the necessity of redisplay can be checked
100     //bool aChanged = anOperationPrs->dependOn(theObject);
101     anOperationPrs->updateShapes();
102     //aChanged = aChanged || anOperationPrs->dependOn(theObject);
103     //if (aChanged)
104     anOperationPrs->Redisplay();
105   }
106 }
107
108 void PartSet_CustomPrs::clearPrs()
109 {
110   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
111   if (!anOperationPrs.IsNull())
112     anOperationPrs.Nullify();
113
114   myOperationPrs.reset();
115 }
116
117 void PartSet_CustomPrs::initPrs()
118 {
119   myOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
120   myOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(new PartSet_OperationPrs(myWorkshop)));
121
122   std::vector<int> aColor = Config_PropManager::color("Visualization", "operation_parameter_color",
123                                                       OPERATION_PARAMETER_COLOR);
124   myOperationPrs->setColor(aColor[0], aColor[1], aColor[2]);
125
126   myOperationPrs->setPointMarker(5, 2.);
127   myOperationPrs->setWidth(1);
128 }
129
130 XGUI_Workshop* PartSet_CustomPrs::workshop() const
131 {
132   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
133   return aConnector->workshop();
134 }