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