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