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