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