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