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