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