]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_SymbolPrs.cpp
Salome HOME
3a498d8277bb16b959ea1f606df6b8e946505fc2
[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   Handle(Prs3d_Presentation) aSelPrs =
120     GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
121   if (!aSelPrs.IsNull()) {
122       if (!aSelPrs->Groups().IsEmpty()) {
123         aSelPrs->Clear();
124       }
125       aSelPrs->Erase();
126   }
127   Handle(Prs3d_Presentation) aHilightPrs =
128     GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
129   if (!aHilightPrs.IsNull()) {
130     if (!aHilightPrs->Groups().IsEmpty()) {
131       aHilightPrs->Clear();
132     }
133     aHilightPrs->Erase();
134   }
135 }
136
137 #ifdef _WINDOWS
138 #pragma warning( disable : 4996 )
139 #endif
140
141 //*********************************************************************************
142 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
143 {
144   if (myIconsMap.count(iconName()) == 1) {
145     return myIconsMap[iconName()];
146   }
147   // Load icon for the presentation
148   std::string aFile;
149   char* anEnv = getenv("SHAPER_ROOT_DIR");
150   if (anEnv) {
151     aFile = std::string(anEnv) +
152       FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
153   } else {
154     anEnv = getenv("OPENPARTS_ROOT_DIR");
155     if (anEnv)
156       aFile = std::string(anEnv) + FSEP + "resources";
157   }
158
159   aFile += FSEP;
160   aFile += iconName();
161   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
162   if (aPixMap->Load(aFile.c_str())) {
163     myIconsMap[iconName()] = aPixMap;
164     return aPixMap;
165   }
166   // The icon for constraint is not found
167   static const char aMsg[] = "Error! constraint images are not found";
168   cout<<aMsg<<endl;
169   Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
170   myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
171   return Handle(Image_AlienPixMap)();
172 }
173
174 //*********************************************************************************
175 void SketcherPrs_SymbolPrs::prepareAspect()
176 {
177   // Create an aspect with the icon
178   if (myAspect.IsNull()) {
179     Handle(Image_AlienPixMap) aIcon = icon();
180     if (aIcon.IsNull())
181       myAspect = new Graphic3d_AspectMarker3d();
182     else
183       myAspect = new Graphic3d_AspectMarker3d(aIcon);
184
185     myAspect->SetColor(myCustomColor);
186   }
187 }
188
189 //*********************************************************************************
190 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
191                                     std::string theAttrName) const
192 {
193   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
194   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
195   if (aLine.get() == NULL)
196     return;
197   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
198
199   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
200   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
201   gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
202   gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
203
204   // Draw line by two points
205   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
206   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
207   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
208   theGroup->AddPrimitiveArray(aLines);
209 }
210
211 //*********************************************************************************
212 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
213                                             const SelectMgr_SequenceOfOwner& theOwners)
214 {
215   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
216   aSelectionPrs->Clear();
217   drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
218
219   aSelectionPrs->SetDisplayPriority(9);
220   aSelectionPrs->Display();
221   thePM->Color(this, GetContext()->SelectionStyle());
222 }
223
224 //*********************************************************************************
225 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
226                                   const Handle(PrsMgr_PresentationManager3d)& thePM,
227                                   const Handle(Graphic3d_HighlightStyle)& theStyle,
228                                   const Handle(SelectMgr_EntityOwner)& theOwner)
229 {
230   thePM->Color(this, theStyle);
231
232   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
233   aHilightPrs->Clear();
234   drawLines(aHilightPrs, theStyle->Color());
235   aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
236
237   if (thePM->IsImmediateModeOn())
238     thePM->AddToImmediateList(aHilightPrs);
239 }
240
241 //*********************************************************************************
242 void SketcherPrs_SymbolPrs::Compute(
243                 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
244                 const Handle(Prs3d_Presentation)& thePresentation,
245                 const Standard_Integer theMode)
246 {
247   // Create an icon
248   prepareAspect();
249
250   Handle(AIS_InteractiveContext) aCtx = GetContext();
251   Handle(OpenGl_GraphicDriver) aDriver =
252     Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
253   if (aDriver.IsNull())
254     return;
255
256   // Update points with default shift value
257   // it updates array of points if the presentation is ready to display, or the array of points
258   // contains the previous values
259
260   bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
261
262   int aNbVertex = myPntArray->VertexNumber();
263   if (myOwner.IsNull()) {
264     myOwner = new SelectMgr_EntityOwner(this);
265   }
266
267   // Create sensitive point for each symbol
268   mySPoints.Clear();
269   for (int i = 1; i <= aNbVertex; i++) {
270     Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
271     mySPoints.Append(aSP);
272     if (myIsCustomColor)
273       myPntArray->SetVertexColor(i, myCustomColor);
274   }
275
276   Handle(OpenGl_Group) aGroup =
277     Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
278   aGroup->SetPrimitivesAspect(myAspect);
279
280   // Recompute boundary box of the group
281   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
282   gp_Pnt aVert;
283   aBnd.Clear();
284   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
285     aVert = myPntArray->Vertice(i);
286     aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
287   }
288
289   // Pint the group with custom procedure (see Render)
290   SketcherPrs_SymbolArray* aElem =
291     new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
292   aGroup->AddElement(aElem);
293
294   // Disable frustum culling for this object by marking it as mutable
295   aGroup->Structure()->SetMutable(true);
296
297   if (!aReadyToDisplay)
298     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
299                           "An empty AIS presentation: SketcherPrs_LengthDimension");
300 }
301
302
303 //*********************************************************************************
304 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
305                                              const Standard_Integer aMode)
306 {
307   ClearSelected();
308   if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
309     for (int i = 1; i <= mySPoints.Length(); i++)
310       aSelection->Add(mySPoints.Value(i));
311   }
312 }
313
314 //*********************************************************************************
315 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
316 {
317   myIsCustomColor = !theColor.empty();
318   if (myIsCustomColor)
319     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
320                                    theColor[2] / 255., Quantity_TOC_RGB);
321   else
322     myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
323
324   if (!myAspect.IsNull())
325     myAspect->SetColor (myCustomColor);
326 }
327
328 //*********************************************************************************
329 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
330                                       const Handle(Prs3d_Presentation)& thePrs,
331                                       Quantity_Color theColor) const
332 {
333   int aColNam = theColor.Name();
334   //cout<<"### SketcherPrs_SymbolPrs::drawShape "<<theColor.Name()<<endl;
335   Handle(Graphic3d_AspectLine3d) aLineAspect =
336     new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
337
338   Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
339   Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
340   aLinesStyle->SetAspect(aLineAspect);
341   myDrawer->SetLineAspect(aLinesStyle);
342
343   if (theShape->isEdge()) {
344     // The shape is edge
345     std::shared_ptr<GeomAPI_Curve> aCurve =
346       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
347     if (aCurve->isLine()) {
348       // The shape is line
349       GeomAdaptor_Curve
350         aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
351       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
352     } else {
353       // The shape is circle or arc
354       GeomAdaptor_Curve
355         aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
356       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
357     }
358   } else if (theShape->isVertex()) {
359     // draw vertex
360     std::shared_ptr<GeomAPI_Vertex> aVertex =
361       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
362     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
363     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
364     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
365   }
366
367   aLinesStyle->SetAspect(aOldStyle);
368   myDrawer->SetLineAspect(aLinesStyle);
369 }
370
371 //*********************************************************************************
372 void SketcherPrs_SymbolPrs::drawListOfShapes(
373                       const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
374                       const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
375 {
376   int aNb = theListAttr->size();
377   if (aNb == 0)
378     return;
379   int i;
380   ObjectPtr aObj;
381   for (i = 0; i < aNb; i++) {
382     aObj = theListAttr->object(i);
383     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
384     if (aShape.get() != NULL)
385       drawShape(aShape, thePrs, theColor);
386   }
387 }
388
389 //*********************************************************************************
390 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
391 {
392   Select3D_BndBox3d aTmpBox;
393   for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
394     const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
395     aTmpBox.Combine (anEnt->BoundingBox());
396   }
397
398   theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
399                     aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());
400 }
401