Salome HOME
Fixed crash when attribute External in feature Projection changed.
[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, const Handle(AIS_InteractiveContext)& theCtx)
55     :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
56     theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj),
57     myContext(theCtx) {}
58
59   virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
60   {
61     ModelAPI_Feature* aConstraint = myObj->feature();
62     if (aConstraint->data().get() && aConstraint->data()->isValid()) {
63       Handle(OpenGl_View) aView = theWorkspace->View();
64       double aScale = aView->Camera()->Scale();
65       // Update points coordinate taking the viewer scale into account
66       myObj->updateIfReadyToDisplay(MyDist * aScale, myObj->myIsCustomColor);
67       if (myIsVboInit) {
68         if (myVboAttribs) {
69           const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
70           Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
71           myVboAttribs->init(aCtx, 0, aAttr->NbElements,
72                              aAttr->Data(), GL_NONE, aAttr->Stride);
73         } else
74           myIsVboInit = false;
75       } else {
76         myAttribs = myObj->myPntArray->Attributes();
77         myIndices = myObj->myPntArray->Indices();
78         myBounds = myObj->myPntArray->Bounds();
79       }
80     }
81     OpenGl_PrimitiveArray::Render(theWorkspace);
82
83     // Update selection position only if there is no selected object
84     // because it can corrupt selection of other objects
85     if ((myContext->NbCurrents() == 0) && (myContext->NbSelected() == 0))  {
86       myContext->MainSelector()->RebuildSensitivesTree(myObj);
87       myContext->MainSelector()->RebuildObjectsTree (false);
88     }
89   }
90
91 private:
92   Handle(SketcherPrs_SymbolPrs) myObj;
93   Handle(AIS_InteractiveContext) myContext;
94 };
95
96
97 //*****************************************************************************
98 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
99
100
101 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
102
103
104 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
105                                              const std::shared_ptr<GeomAPI_Ax3>& thePlane)
106  : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsCustomColor(false)
107 {
108   SetAutoHilight(Standard_False);
109 }
110
111 //*********************************************************************************
112 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
113 {
114   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
115   // Empty memory in position manager
116   aMgr->deleteConstraint(this);
117 }
118
119 #ifdef _WINDOWS
120 #pragma warning( disable : 4996 )
121 #endif
122
123 //*********************************************************************************
124 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
125 {
126   if (myIconsMap.count(iconName()) == 1) {
127     return myIconsMap[iconName()];
128   }
129   // Load icon for the presentation
130   std::string aFile;
131   char* anEnv = getenv("SHAPER_ROOT_DIR");
132   if (anEnv) {
133     aFile = std::string(anEnv) +
134       FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
135   } else {
136     anEnv = getenv("OPENPARTS_ROOT_DIR");
137     if (anEnv)
138       aFile = std::string(anEnv) + FSEP + "resources";
139   }
140
141   aFile += FSEP;
142   aFile += iconName();
143   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
144   if (aPixMap->Load(aFile.c_str())) {
145     myIconsMap[iconName()] = aPixMap;
146     return aPixMap;
147   }
148   // The icon for constraint is not found
149   static const char aMsg[] = "Error! constraint images are not found";
150   cout<<aMsg<<endl;
151   Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
152   myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
153   return Handle(Image_AlienPixMap)();
154 }
155
156 //*********************************************************************************
157 void SketcherPrs_SymbolPrs::prepareAspect()
158 {
159   // Create an aspect with the icon
160   if (myAspect.IsNull()) {
161     Handle(Image_AlienPixMap) aIcon = icon();
162     if (aIcon.IsNull())
163       myAspect = new Graphic3d_AspectMarker3d();
164     else
165       myAspect = new Graphic3d_AspectMarker3d(aIcon);
166
167     myAspect->SetColor(myCustomColor);
168   }
169 }
170
171 //*********************************************************************************
172 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
173                                     std::string theAttrName) const
174 {
175   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
176   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
177   if (aLine.get() == NULL)
178     return;
179   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
180
181   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
182   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
183   gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
184   gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
185
186   // Draw line by two points
187   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
188   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
189   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
190   theGroup->AddPrimitiveArray(aLines);
191 }
192
193 //*********************************************************************************
194 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
195                                             const SelectMgr_SequenceOfOwner& theOwners)
196 {
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) const
314 {
315   if (theShape->isEdge()) {
316     // The shape is edge
317     std::shared_ptr<GeomAPI_Curve> aCurve =
318       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
319     if (aCurve->isLine()) {
320       // The shape is line
321       GeomAdaptor_Curve
322         aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
323       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
324     } else {
325       // The shape is circle or arc
326       GeomAdaptor_Curve
327         aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
328       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
329     }
330   } else if (theShape->isVertex()) {
331     // draw vertex
332     std::shared_ptr<GeomAPI_Vertex> aVertex =
333       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
334     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
335     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
336     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
337   }
338 }
339
340 //*********************************************************************************
341 void SketcherPrs_SymbolPrs::drawListOfShapes(
342                       const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
343                       const Handle(Prs3d_Presentation)& thePrs) const
344 {
345   int aNb = theListAttr->size();
346   if (aNb == 0)
347     return;
348   int i;
349   ObjectPtr aObj;
350   for (i = 0; i < aNb; i++) {
351     aObj = theListAttr->object(i);
352     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
353     if (aShape.get() != NULL)
354       drawShape(aShape, thePrs);
355   }
356 }
357
358 //*********************************************************************************
359 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
360 {
361   Select3D_BndBox3d aTmpBox;
362   for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
363     const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
364     aTmpBox.Combine (anEnt->BoundingBox());
365   }
366
367   theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
368                     aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());
369 }
370