]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_SymbolPrs.cpp
Salome HOME
Fix for constraint selection problem
[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), myContext(theCtx) {}
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         if (myVboAttribs) {
68           const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
69           Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
70           myVboAttribs->init(aCtx, 0, aAttr->NbElements,
71                              aAttr->Data(), GL_NONE, aAttr->Stride);
72         } else
73           myIsVboInit = false;
74       } else {
75         myAttribs = myObj->myPntArray->Attributes();
76         myIndices = myObj->myPntArray->Indices();
77         myBounds = myObj->myPntArray->Bounds();
78       }
79     }
80     OpenGl_PrimitiveArray::Render(theWorkspace);
81
82     // Update selection position only if there is no selected object
83     // because it can corrupt selection of other objects
84     if ((myContext->NbCurrents() == 0) && (myContext->NbSelected() == 0))  {
85       myContext->MainSelector()->RebuildSensitivesTree(myObj);
86       myContext->MainSelector()->RebuildObjectsTree (false);
87     }
88   }
89
90 private:
91   Handle(SketcherPrs_SymbolPrs) myObj;
92   Handle(AIS_InteractiveContext) myContext;
93 };
94
95
96 //*****************************************************************************
97 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
98
99
100 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
101
102
103 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
104                                              const std::shared_ptr<GeomAPI_Ax3>& thePlane)
105  : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsConflicting(false)
106 {
107   SetAutoHilight(Standard_False);
108   myPntArray = new Graphic3d_ArrayOfPoints(1);
109   myPntArray->AddVertex(0., 0., 0.);
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 }
169
170 //*********************************************************************************
171 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
172                                     std::string theAttrName) const
173 {
174   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
175   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
176   if (aLine.get() == NULL)
177     return;
178   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
179
180   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
181   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
182   gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
183   gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
184
185   // Draw line by two points
186   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
187   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
188   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
189   theGroup->AddPrimitiveArray(aLines);
190 }
191
192 //*********************************************************************************
193 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
194                                             const SelectMgr_SequenceOfOwner& theOwners)
195 {
196
197   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
198   aSelectionPrs->Clear();
199   drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
200
201   aSelectionPrs->SetDisplayPriority(9);
202   aSelectionPrs->Display();
203   thePM->Color(this, GetContext()->SelectionStyle());
204 }
205
206 //*********************************************************************************
207 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
208                                   const Handle(PrsMgr_PresentationManager3d)& thePM,
209                                   const Handle(Graphic3d_HighlightStyle)& theStyle,
210                                   const Handle(SelectMgr_EntityOwner)& theOwner)
211 {
212   thePM->Color(this, theStyle);
213
214   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
215   aHilightPrs->Clear();
216   drawLines(aHilightPrs, theStyle->Color());
217   aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
218
219   if (thePM->IsImmediateModeOn())
220     thePM->AddToImmediateList(aHilightPrs);
221 }
222
223 //*********************************************************************************
224 void SketcherPrs_SymbolPrs::Compute(
225                 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
226                 const Handle(Prs3d_Presentation)& thePresentation,
227                 const Standard_Integer theMode)
228 {
229   // Create an icon
230   prepareAspect();
231
232   Handle(AIS_InteractiveContext) aCtx = GetContext();
233   Handle(OpenGl_GraphicDriver) aDriver =
234     Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
235   if (aDriver.IsNull())
236     return;
237
238   // Update points with default shift value
239   // it updates array of points if the presentation is ready to display, or the array of points
240   // contains the previous values
241
242   bool aReadyToDisplay = updateIfReadyToDisplay(20);
243
244   int aNbVertex = myPntArray->VertexNumber();
245   if (myOwner.IsNull()) {
246     myOwner = new SelectMgr_EntityOwner(this);
247   }
248
249   // Create sensitive point for each symbol
250   mySPoints.Clear();
251   for (int i = 1; i <= aNbVertex; i++) {
252     Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
253     mySPoints.Append(aSP);
254   }
255
256   Handle(OpenGl_Group) aGroup = Handle(OpenGl_Group)::DownCast(thePresentation->NewGroup());
257   aGroup->SetPrimitivesAspect(myAspect);
258
259   // Recompute boundary box of the group
260   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
261   gp_Pnt aVert;
262   aBnd.Clear();
263   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
264     aVert = myPntArray->Vertice(i);
265     aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
266   }
267
268   // Pint the group with custom procedure (see Render)
269   SketcherPrs_SymbolArray* aElem =
270     new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
271   aGroup->AddElement(aElem);
272
273   // Disable frustum culling for this object by marking it as mutable
274   aGroup->Structure()->SetMutable(true);
275
276   if (!aReadyToDisplay)
277     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
278                           "An empty AIS presentation: SketcherPrs_LengthDimension");
279 }
280
281
282 //*********************************************************************************
283 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
284                                              const Standard_Integer aMode)
285 {
286   ClearSelected();
287   if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
288     for (int i = 1; i <= mySPoints.Length(); i++)
289       aSelection->Add(mySPoints.Value(i));
290   }
291 }
292
293 //*********************************************************************************
294 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
295                                                      const std::vector<int>& theColor)
296 {
297   if (theConflicting)
298   {
299     if (!myAspect.IsNull())
300       myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255.,
301                           theColor[2] / 255., Quantity_TOC_RGB));
302     myIsConflicting = true;
303   }
304   else
305   {
306     if (!myAspect.IsNull())
307       myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
308     myIsConflicting = false;
309   }
310 }
311
312 //*********************************************************************************
313 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
314                                       const Handle(Prs3d_Presentation)& thePrs) const
315 {
316   if (theShape->isEdge()) {
317     // The shape is edge
318     std::shared_ptr<GeomAPI_Curve> aCurve =
319       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
320     if (aCurve->isLine()) {
321       // The shape is line
322       GeomAdaptor_Curve
323         aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
324       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
325     } else {
326       // The shape is circle or arc
327       GeomAdaptor_Curve
328         aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
329       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
330     }
331   } else if (theShape->isVertex()) {
332     // draw vertex
333     std::shared_ptr<GeomAPI_Vertex> aVertex =
334       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
335     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
336     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
337     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
338   }
339 }
340
341 //*********************************************************************************
342 void SketcherPrs_SymbolPrs::drawListOfShapes(
343                       const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
344                       const Handle(Prs3d_Presentation)& thePrs) const
345 {
346   int aNb = theListAttr->size();
347   if (aNb == 0)
348     return;
349   int i;
350   ObjectPtr aObj;
351   for (i = 0; i < aNb; i++) {
352     aObj = theListAttr->object(i);
353     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
354     if (aShape.get() != NULL)
355       drawShape(aShape, thePrs);
356   }
357 }
358
359 //*********************************************************************************
360 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
361 {
362   Select3D_BndBox3d aTmpBox;
363   for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
364     const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
365     aTmpBox.Combine (anEnt->BoundingBox());
366   }
367
368   theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
369                     aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());
370 }
371