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