Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_SymbolPrs.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 "SketcherPrs_SymbolPrs.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_PositionMgr.h"
24
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Curve.h>
28
29 #include <ModelAPI_Tools.h>
30
31 #include <Events_InfoMessage.h>
32
33 #include <Graphic3d_ArrayOfSegments.hxx>
34 #include <Graphic3d_BndBox4f.hxx>
35
36 #include <SelectMgr_Selection.hxx>
37 #include <Select3D_SensitivePoint.hxx>
38 #include <TopLoc_Location.hxx>
39 #include <AIS_InteractiveContext.hxx>
40 #include <V3d_Viewer.hxx>
41 #include <Prs3d_Root.hxx>
42 #include <Geom_CartesianPoint.hxx>
43 #include <GeomAdaptor_Curve.hxx>
44 #include <StdPrs_DeflectionCurve.hxx>
45 #include <StdPrs_Point.hxx>
46 #include <StdPrs_Curve.hxx>
47 #include <Prs3d_LineAspect.hxx>
48
49 #include <OpenGl_Element.hxx>
50 #include <OpenGl_GraphicDriver.hxx>
51 #include <OpenGl_Context.hxx>
52 #include <OpenGl_View.hxx>
53 #include <OpenGl_Group.hxx>
54
55 #ifdef WIN32
56 # define FSEP "\\"
57 #else
58 # define FSEP "/"
59 #endif
60
61 /// Step between icons
62 static const double MyDist = 0.015;
63
64
65 //**************************************************************
66 //! Redefinition of OpenGl_Element
67 class SketcherPrs_SymbolArray: public OpenGl_PrimitiveArray
68 {
69 public:
70   SketcherPrs_SymbolArray(const OpenGl_GraphicDriver* theDriver,
71     const Handle(SketcherPrs_SymbolPrs)& theObj, const Handle(AIS_InteractiveContext)& theCtx)
72     :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
73     theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj),
74     myContext(theCtx) {}
75
76   virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
77   {
78     ModelAPI_Feature* aConstraint = myObj->feature();
79     if (aConstraint->data().get() && aConstraint->data()->isValid()) {
80       Handle(OpenGl_View) aView = theWorkspace->View();
81       double aScale = aView->Camera()->Scale();
82       // Update points coordinate taking the viewer scale into account
83       myObj->updateIfReadyToDisplay(MyDist * aScale, myObj->myIsCustomColor);
84       if (myIsVboInit) {
85         if (myVboAttribs) {
86           const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
87           Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
88           myVboAttribs->init(aCtx, 0, aAttr->NbElements,
89                              aAttr->Data(), GL_NONE, aAttr->Stride);
90         } else
91           myIsVboInit = false;
92       } else {
93         myAttribs = myObj->myPntArray->Attributes();
94         myIndices = myObj->myPntArray->Indices();
95         myBounds = myObj->myPntArray->Bounds();
96       }
97     }
98     OpenGl_PrimitiveArray::Render(theWorkspace);
99
100     // Update selection position only if there is no selected object
101     // because it can corrupt selection of other objects
102     if (myContext->NbSelected() == 0)  {
103       myContext->MainSelector()->RebuildSensitivesTree(myObj);
104       myContext->MainSelector()->RebuildObjectsTree (false);
105     }
106   }
107
108 private:
109   Handle(SketcherPrs_SymbolPrs) myObj;
110   Handle(AIS_InteractiveContext) myContext;
111 };
112
113
114 //*****************************************************************************
115 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
116
117
118 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
119
120
121 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
122   SketchPlugin_Sketch* theSketcher)
123  : AIS_InteractiveObject(), myConstraint(theConstraint),
124    myPlane(theSketcher->coordinatePlane()), myIsCustomColor(false),
125    mySketcher(theSketcher)
126 {
127   SetAutoHilight(Standard_False);
128 }
129
130 //*********************************************************************************
131 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
132 {
133   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
134   // Empty memory in position manager
135   aMgr->deleteConstraint(this);
136
137   Handle(Prs3d_Presentation) aSelPrs =
138     GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
139   if (!aSelPrs.IsNull()) {
140       if (!aSelPrs->Groups().IsEmpty()) {
141         aSelPrs->Clear();
142       }
143       aSelPrs->Erase();
144   }
145   Handle(Prs3d_Presentation) aHilightPrs =
146     GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
147   if (!aHilightPrs.IsNull()) {
148     if (!aHilightPrs->Groups().IsEmpty()) {
149       aHilightPrs->Clear();
150     }
151     aHilightPrs->Erase();
152   }
153 }
154
155 #ifdef _WINDOWS
156 #pragma warning( disable : 4996 )
157 #endif
158
159 //*********************************************************************************
160 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
161 {
162   if (myIconsMap.count(iconName()) == 1) {
163     return myIconsMap[iconName()];
164   }
165   // Load icon for the presentation
166   std::string aFile;
167   char* anEnv = getenv("SHAPER_ROOT_DIR");
168   if (anEnv) {
169     aFile = std::string(anEnv) +
170       FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
171   } else {
172     anEnv = getenv("OPENPARTS_ROOT_DIR");
173     if (anEnv)
174       aFile = std::string(anEnv) + FSEP + "resources";
175   }
176
177   aFile += FSEP;
178   aFile += iconName();
179   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
180   if (aPixMap->Load(aFile.c_str())) {
181     myIconsMap[iconName()] = aPixMap;
182     return aPixMap;
183   }
184   // The icon for constraint is not found
185   static const char aMsg[] = "Error! constraint images are not found";
186   cout<<aMsg<<endl;
187   Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
188   myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
189   return Handle(Image_AlienPixMap)();
190 }
191
192 //*********************************************************************************
193 void SketcherPrs_SymbolPrs::prepareAspect()
194 {
195   // Create an aspect with the icon
196   if (myAspect.IsNull()) {
197     Handle(Image_AlienPixMap) aIcon = icon();
198     if (aIcon.IsNull())
199       myAspect = new Graphic3d_AspectMarker3d();
200     else
201       myAspect = new Graphic3d_AspectMarker3d(aIcon);
202
203     myAspect->SetColor(myCustomColor);
204   }
205 }
206
207 //*********************************************************************************
208 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
209                                     std::string theAttrName) const
210 {
211   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
212   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
213   if (aLine.get() == NULL)
214     return;
215   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
216
217   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
218   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
219   gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
220   gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
221
222   // Draw line by two points
223   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
224   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
225   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
226   theGroup->AddPrimitiveArray(aLines);
227 }
228
229 //*********************************************************************************
230 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
231                                             const SelectMgr_SequenceOfOwner& theOwners)
232 {
233   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
234   aSelectionPrs->Clear();
235   drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
236
237   aSelectionPrs->SetDisplayPriority(9);
238   aSelectionPrs->Display();
239   thePM->Color(this, GetContext()->SelectionStyle());
240 }
241
242 //*********************************************************************************
243 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
244                                   const Handle(PrsMgr_PresentationManager3d)& thePM,
245                                   const Handle(Prs3d_Drawer)& theStyle,
246                                   const Handle(SelectMgr_EntityOwner)& theOwner)
247 {
248   thePM->Color(this, theStyle);
249
250   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
251   aHilightPrs->Clear();
252   drawLines(aHilightPrs, theStyle->Color());
253   aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
254
255   if (thePM->IsImmediateModeOn())
256     thePM->AddToImmediateList(aHilightPrs);
257 }
258
259 //*********************************************************************************
260 void SketcherPrs_SymbolPrs::Compute(
261                 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
262                 const Handle(Prs3d_Presentation)& thePresentation,
263                 const Standard_Integer theMode)
264 {
265   // Create an icon
266   prepareAspect();
267
268   Handle(AIS_InteractiveContext) aCtx = GetContext();
269   Handle(OpenGl_GraphicDriver) aDriver =
270     Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
271   if (aDriver.IsNull())
272     return;
273
274   // Update points with default shift value
275   // it updates array of points if the presentation is ready to display, or the array of points
276   // contains the previous values
277
278   bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
279
280   int aNbVertex = myPntArray->VertexNumber();
281   if (myOwner.IsNull()) {
282     myOwner = new SelectMgr_EntityOwner(this);
283   }
284
285   // Create sensitive point for each symbol
286   mySPoints.Clear();
287   for (int i = 1; i <= aNbVertex; i++) {
288     Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
289     mySPoints.Append(aSP);
290     if (myIsCustomColor)
291       myPntArray->SetVertexColor(i, myCustomColor);
292   }
293
294   Handle(OpenGl_Group) aGroup =
295     Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
296   aGroup->SetPrimitivesAspect(myAspect);
297
298   // Recompute boundary box of the group
299   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
300   gp_Pnt aVert;
301   aBnd.Clear();
302   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
303     aVert = myPntArray->Vertice(i);
304     aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
305   }
306
307   // Pint the group with custom procedure (see Render)
308   SketcherPrs_SymbolArray* aElem =
309     new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
310   aGroup->AddElement(aElem);
311
312   // Disable frustum culling for this object by marking it as mutable
313   aGroup->Structure()->SetMutable(true);
314
315   if (!aReadyToDisplay)
316     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
317                           "An empty AIS presentation: SketcherPrs_LengthDimension");
318 }
319
320
321 //*********************************************************************************
322 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
323                                              const Standard_Integer aMode)
324 {
325   ClearSelected();
326   if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
327     for (int i = 1; i <= mySPoints.Length(); i++)
328       aSelection->Add(mySPoints.Value(i));
329   }
330 }
331
332 //*********************************************************************************
333 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
334 {
335   myIsCustomColor = !theColor.empty();
336   if (myIsCustomColor)
337     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
338                                    theColor[2] / 255., Quantity_TOC_RGB);
339   else
340     myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
341
342   if (!myAspect.IsNull())
343     myAspect->SetColor (myCustomColor);
344
345   Handle(Prs3d_Presentation) aPrs = Presentation();
346   if (!aPrs.IsNull()) {
347     if (myIsCustomColor) {
348       Handle(Graphic3d_PresentationAttributes) aAttr = new Graphic3d_PresentationAttributes();
349       aAttr->SetColor(myCustomColor);
350       aPrs->Highlight(aAttr);
351     }
352     else {
353       aPrs->UnHighlight();
354     }
355   }
356 }
357
358 //*********************************************************************************
359 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
360                                       const Handle(Prs3d_Presentation)& thePrs,
361                                       Quantity_Color theColor) const
362 {
363   Handle(Graphic3d_AspectLine3d) aLineAspect =
364     new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
365
366   Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
367   Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
368   aLinesStyle->SetAspect(aLineAspect);
369   myDrawer->SetLineAspect(aLinesStyle);
370
371   if (theShape->isEdge()) {
372     // The shape is edge
373     std::shared_ptr<GeomAPI_Curve> aCurve =
374       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
375     if (aCurve->isLine()) {
376       // The shape is line
377       GeomAdaptor_Curve
378         aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
379       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
380     } else {
381       // The shape is circle or arc
382       GeomAdaptor_Curve
383         aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
384       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
385     }
386   } else if (theShape->isVertex()) {
387     // draw vertex
388     std::shared_ptr<GeomAPI_Vertex> aVertex =
389       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
390     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
391     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
392     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
393   }
394
395   aLinesStyle->SetAspect(aOldStyle);
396   myDrawer->SetLineAspect(aLinesStyle);
397 }
398
399 //*********************************************************************************
400 void SketcherPrs_SymbolPrs::drawListOfShapes(
401                       const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
402                       const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
403 {
404   int aNb = theListAttr->size();
405   if (aNb == 0)
406     return;
407   int i;
408   ObjectPtr aObj;
409   for (i = 0; i < aNb; i++) {
410     aObj = theListAttr->object(i);
411     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
412     if (aShape.get() != NULL)
413       drawShape(aShape, thePrs, theColor);
414   }
415 }
416
417 //*********************************************************************************
418 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
419 {
420   Select3D_BndBox3d aTmpBox;
421   for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
422     const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
423     aTmpBox.Combine (anEnt->BoundingBox());
424   }
425
426   theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
427                     aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());
428 }