Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / PartSet / PartSet_PreviewPlanes.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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "PartSet_PreviewPlanes.h"
22
23 #include <ModuleBase_IWorkshop.h>
24
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultConstruction.h>
27
28 #include <XGUI_Tools.h>
29 #include <XGUI_Displayer.h>
30 #include <XGUI_Workshop.h>
31
32 #include <Config_PropManager.h>
33 #include <GeomAlgoAPI_FaceBuilder.h>
34
35 #include <SketchPlugin_Sketch.h>
36
37 PartSet_PreviewPlanes::PartSet_PreviewPlanes()
38  : myPreviewDisplayed(false)
39 {
40 }
41
42 bool PartSet_PreviewPlanes::hasVisualizedBodies(ModuleBase_IWorkshop* theWorkshop)
43 {
44   bool aBodyIsVisualized = false;
45
46   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(theWorkshop);
47   XGUI_Displayer* aDisp = aWorkshop->displayer();
48   QObjectPtrList aDisplayed = aDisp->displayedObjects();
49   foreach (ObjectPtr anObj, aDisplayed) {
50     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
51     // result constructions should not be taken as a body
52     if (aResult.get() != NULL && aResult->groupName() == ModelAPI_ResultBody::group()) {
53       GeomShapePtr aShape = aResult->shape();
54       if (aShape.get()) {
55         // vertices, edges should not be taken as a body
56         aBodyIsVisualized = aShape->shapeType() <= GeomAPI_Shape::FACE;
57         if (aBodyIsVisualized)
58           break;
59       }
60     }
61   }
62   return aBodyIsVisualized;
63 }
64
65 bool PartSet_PreviewPlanes::hasVisualizedSketch(ModuleBase_IWorkshop* theWorkshop)
66 {
67   bool aSketchIsVisualized = false;
68
69   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(theWorkshop);
70   XGUI_Displayer* aDisp = aWorkshop->displayer();
71   QObjectPtrList aDisplayed = aDisp->displayedObjects();
72   foreach (ObjectPtr anObj, aDisplayed) {
73     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
74     if (aResult.get() != NULL) {
75       FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
76       if (aFeature.get() && aFeature->getKind() == SketchPlugin_Sketch::ID()) {
77         ResultConstructionPtr aCResult =  std::dynamic_pointer_cast<ModelAPI_ResultConstruction>
78                                                                                       (aResult);
79         if (aCResult.get() && aCResult->facesNum() > 0) {
80           aSketchIsVisualized = true;
81           break;
82         }
83       }
84       break;
85     }
86   }
87   return aSketchIsVisualized;
88 }
89
90 bool PartSet_PreviewPlanes::isPreviewShape(std::shared_ptr<GeomAPI_Shape> theShape)
91 {
92   return (myYZPlane->getShape()->isEqual(theShape) ||
93           myXZPlane->getShape()->isEqual(theShape) ||
94           myXYPlane->getShape()->isEqual(theShape));
95 }
96
97 void PartSet_PreviewPlanes::erasePreviewPlanes(ModuleBase_IWorkshop* theWorkshop)
98 {
99   if (myPreviewDisplayed) {
100     XGUI_Displayer* aDisp = XGUI_Tools::workshop(theWorkshop)->displayer();
101     aDisp->eraseAIS(myYZPlane, false);
102     aDisp->eraseAIS(myXZPlane, false);
103     aDisp->eraseAIS(myXYPlane, false);
104     myPreviewDisplayed = false;
105   }
106 }
107
108 void PartSet_PreviewPlanes::showPreviewPlanes(ModuleBase_IWorkshop* theWorkshop)
109 {
110   if (myPreviewDisplayed)
111     return;
112
113   if (!myYZPlane) { // If planes are not created
114     // Create Preview
115     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
116     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
117     // -1, not 1 for correct internal sketch coords (issue 898)
118     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, -1, 0));
119     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
120
121     std::vector<int> aYZRGB(3, 0), aXZRGB(3, 0), aXYRGB(3, 0);
122 #ifdef SET_PLANES_COLOR_IN_PREFERENCES
123     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color");
124     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color");
125     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color");
126 #else
127     aYZRGB[0] = 225;
128     aXZRGB[1] = 225;
129     aXYRGB[2] = 225;
130 #endif
131     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
132     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
133     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
134
135     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
136     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
137     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
138   }
139   XGUI_Displayer* aDisp = XGUI_Tools::workshop(theWorkshop)->displayer();
140   aDisp->displayAIS(myYZPlane, true, false);
141   aDisp->displayAIS(myXZPlane, true, false);
142   aDisp->displayAIS(myXYPlane, true, false);
143   myPreviewDisplayed = true;
144 }
145
146 AISObjectPtr PartSet_PreviewPlanes::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin,
147                                                        std::shared_ptr<GeomAPI_Dir> theNorm,
148                                                        const int theRGB[3])
149 {
150   double aSize = Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size");
151   std::shared_ptr<GeomAPI_Shape> aFace =
152     GeomAlgoAPI_FaceBuilder::squareFace(theOrigin, theNorm, aSize);
153   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
154   aAIS->createShape(aFace);
155   aAIS->setWidth(Config_PropManager::integer(SKETCH_TAB_NAME, "planes_thickness"));
156   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
157   return aAIS;
158 }