Salome HOME
#Initial implementation of support of dump and save to hdf in case SHAPER module...
[modules/shaper.git] / src / PartSet / PartSet_PreviewSketchPlane.cpp
1 // Copyright (C) 2014-2020  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_PreviewSketchPlane.h"
21 #include "PartSet_Tools.h"
22
23 #include <ModuleBase_IWorkshop.h>
24
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_CompositeFeature.h>
28 #include <ModelAPI_Tools.h>
29
30 #include <GeomAPI_AISObject.h>
31 #include <GeomAPI_Pnt.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 #include <BRepBndLib.hxx>
44
45 PartSet_PreviewSketchPlane::PartSet_PreviewSketchPlane()
46  : myPreviewIsDisplayed(false), mySizeOfView(0), myIsUseSizeOfView(false)
47 {
48 }
49
50 void PartSet_PreviewSketchPlane::eraseSketchPlane(ModuleBase_IWorkshop* theWorkshop,
51                                                   const bool isClearPlane)
52 {
53   if (myPreviewIsDisplayed) {
54     XGUI_Displayer* aDisp = XGUI_Tools::workshop(theWorkshop)->displayer();
55     aDisp->eraseAIS(myPlane, false);
56     if (isClearPlane) {
57       myPlane = std::shared_ptr<GeomAPI_AISObject>();
58       myShape = std::shared_ptr<GeomAPI_Shape>();
59     }
60     myPreviewIsDisplayed = false;
61   }
62 }
63
64 void PartSet_PreviewSketchPlane::createSketchPlane(const CompositeFeaturePtr& theSketch,
65                                                    ModuleBase_IWorkshop* theWorkshop)
66 {
67   // plane is visualized only if sketch plane is filled
68   if (!PartSet_Tools::sketchPlane(theSketch).get())
69     return;
70
71   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
72     (theSketch->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
73   if (!aSelAttr)
74     return;
75
76   if (myShape.get() && myShape->isSame(aSelAttr->value()))
77     return;
78
79   XGUI_Displayer* aDisp = XGUI_Tools::workshop(theWorkshop)->displayer();
80   if (myPreviewIsDisplayed) {
81     aDisp->eraseAIS(myPlane, false);
82   }
83
84   // Create Preview
85   // selected linear face parameters
86   myShape = aSelAttr->value();
87   // this case is needed by constructing sketch on a plane, where result shape is equal
88   // to context result, therefore value() returns NULL and we should use shape of context.
89   if (!myShape.get() && aSelAttr->context().get())
90     myShape = aSelAttr->context()->shape();
91
92   if (!myShape.get()) {
93     // Create Preview for default planes
94     std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
95         theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
96     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
97         theSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
98
99     double aFaceSize = myIsUseSizeOfView ? mySizeOfView
100       : Config_PropManager::real(SKETCH_TAB_NAME, "planes_size");
101     if (aFaceSize <= Precision::Confusion())
102       aFaceSize = 200;   // Set default value
103
104     myShape = GeomAlgoAPI_FaceBuilder::squareFace(
105       myViewCentralPoint.get() ? myViewCentralPoint : anOrigin->pnt(), aNormal->dir(), aFaceSize);
106   }
107   else if (myIsUseSizeOfView && (mySizeOfView > 0)) {
108     std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(myShape));
109     std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
110     if (aPlane.get()) {
111       double anA, aB, aC, aD;
112       aPlane->coefficients(anA, aB, aC, aD);
113       std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
114       std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
115       std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
116       aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
117       std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
118       myShape = GeomAlgoAPI_FaceBuilder::squareFace(
119         myViewCentralPoint.get() ? myViewCentralPoint : anOrigPnt, aNormDir, mySizeOfView);
120     }
121   }
122   myPlane = createPreviewPlane();
123
124   aDisp->displayAIS(myPlane, false/*load object in selection*/, 1/*shaded*/, false);
125   myPreviewIsDisplayed = true;
126 }
127
128 double maximumSize(double theXmin, double theYmin, double theZmin,
129                    double theXmax, double theYmax, double theZmax)
130 {
131   double aSize = fabs(theXmax - theXmin);
132   double aSizeToCompare = fabs(theYmax - theYmin);
133   if (aSizeToCompare > aSize)
134     aSize = aSizeToCompare;
135   aSizeToCompare = fabs(theZmax - theZmin);
136   if (aSizeToCompare > aSize)
137     aSize = aSizeToCompare;
138
139   return aSize;
140 }
141
142 bool PartSet_PreviewSketchPlane::getDefaultSizeOfView(
143   const CompositeFeaturePtr& theSketch, double& theSizeOfView,
144   std::shared_ptr<GeomAPI_Pnt>& theCentralPnt)
145 {
146   if (!PartSet_Tools::sketchPlane(theSketch).get())
147     return false;
148
149   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
150     (theSketch->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
151   if (aSelAttr) {
152     myShape = aSelAttr->value();
153     // this case is needed by constructing sketch on a plane, where result shape is equal
154     // to context result, therefore value() returns NULL and we should use shape of context.
155     if (!myShape.get() && aSelAttr->context().get())
156       myShape = aSelAttr->context()->shape();
157   }
158
159   if (myShape.get())
160     return false;
161
162   Bnd_Box aBox;
163   int aNumberOfSubs = theSketch->numberOfSubs();
164   for (int aSubFeatureId = 0; aSubFeatureId < aNumberOfSubs; aSubFeatureId++) {
165     FeaturePtr aFeature = theSketch->subFeature(aSubFeatureId);
166     if (!aFeature.get())
167       continue;
168
169     std::list<ResultPtr> aResults = aFeature->results();
170     std::list<ResultPtr>::const_iterator aResultIt;
171     for (aResultIt = aResults.begin(); aResultIt != aResults.end(); ++aResultIt) {
172       ResultPtr aResult = *aResultIt;
173       std::shared_ptr<GeomAPI_Shape> aShapePtr = aResult->shape();
174       if (aShapePtr.get()) {
175         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
176         if (aShape.IsNull())
177           continue;
178         BRepBndLib::Add(aShape, aBox);
179       }
180     }
181   }
182   if (aBox.IsVoid())
183     return 0;
184
185   double aXmin, aXmax, anYmin, anYmax, aZmin, aZmax;
186   aBox.Get(aXmin, anYmin, aZmin, aXmax, anYmax, aZmax);
187
188   theSizeOfView = maximumSize(aXmin, anYmin, aZmin, aXmax, anYmax, aZmax);
189   if (theSizeOfView > 0) {
190     gp_Pnt aCentre(aXmax-fabs(aXmax-aXmin)/2., anYmax-fabs(anYmax-anYmin)/2.,
191                    aZmax - fabs(aZmax-aZmin)/2.);
192     theCentralPnt = std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(),
193                                                                  aCentre.Z()));
194   }
195   return true;
196 }
197
198 void PartSet_PreviewSketchPlane::setSizeOfView(double theSizeOfView, bool isUseSizeOfView,
199   const std::shared_ptr<GeomAPI_Pnt>& theCentralPoint)
200 {
201   mySizeOfView = theSizeOfView;
202   myIsUseSizeOfView = isUseSizeOfView;
203
204   myViewCentralPoint = theCentralPoint;
205 }
206
207 AISObjectPtr PartSet_PreviewSketchPlane::createPreviewPlane()
208 {
209   if (myPlane.get()) {
210     myPlane->createShape(myShape);
211     return myPlane;
212   }
213   else {
214     AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
215     aAIS->createShape(myShape);
216     std::vector<int> aColor = Config_PropManager::color("Visualization", "sketch_preview_plane");
217     if (aColor.size() == 3)
218       aAIS->setColor(aColor[0], aColor[1], aColor[2]);
219     aAIS->setTransparensy(0.8);
220
221     int aDispMode = 1; // shading
222     Handle(AIS_InteractiveObject) anAISIO = aAIS->impl<Handle(AIS_InteractiveObject)>();
223     if (!anAISIO.IsNull()) {
224       //anAISIO->SetInfiniteState(Standard_True);
225       anAISIO->Attributes()->SetFaceBoundaryDraw( Standard_True );
226       anAISIO->SetDisplayMode(aDispMode);
227     }
228     return aAIS;
229   }
230 }