Salome HOME
Issue #2204 Display a sketch plane: construction plane - using the plane face in...
[modules/shaper.git] / src / PartSet / PartSet_PreviewSketchPlane.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_PreviewSketchPlane.h"
22 #include "PartSet_Tools.h"
23
24 #include <ModuleBase_IWorkshop.h>
25
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_CompositeFeature.h>
29 #include <ModelAPI_Tools.h>
30
31 #include <GeomAPI_AISObject.h>
32
33 #include <XGUI_Tools.h>
34 #include <XGUI_Displayer.h>
35 #include <XGUI_Workshop.h>
36
37 #include <Config_PropManager.h>
38 #include <GeomAlgoAPI_FaceBuilder.h>
39
40 #include <SketchPlugin_Sketch.h>
41 #include <SketchPlugin_SketchEntity.h>
42
43 PartSet_PreviewSketchPlane::PartSet_PreviewSketchPlane()
44  : myPreviewIsDisplayed(false)
45 {
46 }
47
48 void PartSet_PreviewSketchPlane::eraseSketchPlane(ModuleBase_IWorkshop* theWorkshop,
49                                                   const bool isClearPlane)
50 {
51   if (myPreviewIsDisplayed) {
52     XGUI_Displayer* aDisp = XGUI_Tools::workshop(theWorkshop)->displayer();
53     aDisp->eraseAIS(myPlane, false);
54     if (isClearPlane) {
55       myPlane = std::shared_ptr<GeomAPI_AISObject>();
56       myShape = std::shared_ptr<GeomAPI_Shape>();
57     }
58     myPreviewIsDisplayed = false;
59   }
60 }
61
62 void PartSet_PreviewSketchPlane::createSketchPlane(const CompositeFeaturePtr& theSketch,
63                                                    ModuleBase_IWorkshop* theWorkshop)
64 {
65   // the preview plane has been already created and displayed
66   if (myPreviewIsDisplayed)
67     return;
68
69   // plane is visualized only if sketch plane is filled
70   if (!PartSet_Tools::sketchPlane(theSketch).get())
71     return;
72
73   if (!myPlane) { // If planes are not created
74     // Create Preview
75     // selected linear face parameters
76     AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
77       (theSketch->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
78     if (aSelAttr) {
79       myShape = aSelAttr->value();
80       // this case is needed by constructing sketch on a plane, where result shape is equal
81       // to context result, therefore value() returns NULL and we should use shape of context.
82       if (!myShape.get() && aSelAttr->context().get())
83         myShape = aSelAttr->context()->shape();
84     }
85     if (!myShape.get()) {
86       // Create Preview for default planes
87       std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
88           theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
89       std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
90           theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
91       myShape = GeomAlgoAPI_FaceBuilder::squareFace(anOrigin->pnt(), aNormal->dir(),
92         Config_PropManager::integer(SKETCH_TAB_NAME, "planes_size"));
93     }
94     myPlane = createPreviewPlane();
95   }
96
97   XGUI_Displayer* aDisp = XGUI_Tools::workshop(theWorkshop)->displayer();
98   aDisp->displayAIS(myPlane, true, 1/*shaded*/, false);
99   myPreviewIsDisplayed = true;
100 }
101
102 AISObjectPtr PartSet_PreviewSketchPlane::createPreviewPlane()
103 {
104   if (myPlane.get()) {
105     myPlane->createShape(myShape);
106     return myPlane;
107   }
108   else {
109     AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
110     aAIS->createShape(myShape);
111     std::vector<int> aColor = Config_PropManager::color("Visualization", "sketch_preview_plane");
112     if (aColor.size() == 3)
113       aAIS->setColor(aColor[0], aColor[1], aColor[2]);
114     aAIS->setTransparensy(0.8);
115
116     int aDispMode = 1; // shading
117     Handle(AIS_InteractiveObject) anAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
118     if (!anAISIO.IsNull()) {
119       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
120       anAISIO->SetDisplayMode(aDispMode);
121     }
122     return aAIS;
123   }
124 }