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