Salome HOME
Update constraints symbols according to current sketch plane
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_SymbolPrs.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketcherPrs_SymbolPrs.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_PositionMgr.h"
23
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_Curve.h>
27
28 #include <ModelAPI_Tools.h>
29
30 #include <Events_InfoMessage.h>
31
32 #include <Graphic3d_ArrayOfSegments.hxx>
33 #include <Graphic3d_BndBox4f.hxx>
34
35 #include <SelectMgr_Selection.hxx>
36 #include <Select3D_SensitivePoint.hxx>
37 #include <TopLoc_Location.hxx>
38 #include <AIS_InteractiveContext.hxx>
39 #include <V3d_Viewer.hxx>
40 #include <Prs3d_Root.hxx>
41 #include <Geom_CartesianPoint.hxx>
42 #include <GeomAdaptor_Curve.hxx>
43 #include <StdPrs_DeflectionCurve.hxx>
44 #include <StdPrs_Point.hxx>
45 #include <StdPrs_Curve.hxx>
46 #include <Prs3d_LineAspect.hxx>
47
48 #include <OpenGl_Element.hxx>
49 #include <OpenGl_GraphicDriver.hxx>
50 #include <OpenGl_Context.hxx>
51 #include <OpenGl_View.hxx>
52 #include <OpenGl_Group.hxx>
53
54 #ifdef WIN32
55 # define FSEP "\\"
56 #else
57 # define FSEP "/"
58 #endif
59
60 /// Step between icons
61 static const double MyDist = 0.015;
62
63
64 //**************************************************************
65 //! Redefinition of OpenGl_Element
66 class SketcherPrs_SymbolArray: public OpenGl_PrimitiveArray
67 {
68 public:
69   SketcherPrs_SymbolArray(const OpenGl_GraphicDriver* theDriver,
70     const Handle(SketcherPrs_SymbolPrs)& theObj, const Handle(AIS_InteractiveContext)& theCtx)
71     :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
72     theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj),
73     myContext(theCtx) {}
74
75   virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
76   {
77     ModelAPI_Feature* aConstraint = myObj->feature();
78     if (aConstraint->data().get() && aConstraint->data()->isValid()) {
79       Handle(OpenGl_View) aView = theWorkspace->View();
80       double aScale = aView->Camera()->Scale();
81       // Update points coordinate taking the viewer scale into account
82       myObj->updateIfReadyToDisplay(MyDist * aScale, myObj->myIsCustomColor);
83       if (myIsVboInit) {
84         if (myVboAttribs) {
85           const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
86           Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
87           myVboAttribs->init(aCtx, 0, aAttr->NbElements,
88                              aAttr->Data(), GL_NONE, aAttr->Stride);
89         } else
90           myIsVboInit = false;
91       } else {
92         myAttribs = myObj->myPntArray->Attributes();
93         myIndices = myObj->myPntArray->Indices();
94         myBounds = myObj->myPntArray->Bounds();
95       }
96     }
97     OpenGl_PrimitiveArray::Render(theWorkspace);
98
99     // Update selection position only if there is no selected object
100     // because it can corrupt selection of other objects
101     if (myContext->NbSelected() == 0)  {
102       myContext->MainSelector()->RebuildSensitivesTree(myObj);
103       myContext->MainSelector()->RebuildObjectsTree (false);
104     }
105   }
106
107 private:
108   Handle(SketcherPrs_SymbolPrs) myObj;
109   Handle(AIS_InteractiveContext) myContext;
110 };
111
112
113 //*****************************************************************************
114 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
115
116
117 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
118
119
120 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
121   SketchPlugin_Sketch* theSketcher)
122  : AIS_InteractiveObject(), myConstraint(theConstraint),
123    myIsCustomColor(false),
124    mySketcher(theSketcher)
125 {
126   SetAutoHilight(Standard_False);
127 }
128
129 //*********************************************************************************
130 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
131 {
132   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
133   // Empty memory in position manager
134   aMgr->deleteConstraint(this);
135
136   Handle(Prs3d_Presentation) aSelPrs =
137     GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
138   if (!aSelPrs.IsNull()) {
139       if (!aSelPrs->Groups().IsEmpty()) {
140         aSelPrs->Clear();
141       }
142       aSelPrs->Erase();
143   }
144   Handle(Prs3d_Presentation) aHilightPrs =
145     GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
146   if (!aHilightPrs.IsNull()) {
147     if (!aHilightPrs->Groups().IsEmpty()) {
148       aHilightPrs->Clear();
149     }
150     aHilightPrs->Erase();
151   }
152 }
153
154 #ifdef _WINDOWS
155 #pragma warning( disable : 4996 )
156 #endif
157
158 //*********************************************************************************
159 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
160 {
161   if (myIconsMap.count(iconName()) == 1) {
162     return myIconsMap[iconName()];
163   }
164   // Load icon for the presentation
165   std::string aFile;
166   char* anEnv = getenv("SHAPER_ROOT_DIR");
167   if (anEnv) {
168     aFile = std::string(anEnv) +
169       FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
170   } else {
171     anEnv = getenv("OPENPARTS_ROOT_DIR");
172     if (anEnv)
173       aFile = std::string(anEnv) + FSEP + "resources";
174   }
175
176   aFile += FSEP;
177   aFile += iconName();
178   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
179   if (aPixMap->Load(aFile.c_str())) {
180     myIconsMap[iconName()] = aPixMap;
181     return aPixMap;
182   }
183   // The icon for constraint is not found
184   static const char aMsg[] = "Error! constraint images are not found";
185   cout<<aMsg<<endl;
186   Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
187   myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
188   return Handle(Image_AlienPixMap)();
189 }
190
191 //*********************************************************************************
192 void SketcherPrs_SymbolPrs::prepareAspect()
193 {
194   // Create an aspect with the icon
195   if (myAspect.IsNull()) {
196     Handle(Image_AlienPixMap) aIcon = icon();
197     if (aIcon.IsNull())
198       myAspect = new Graphic3d_AspectMarker3d();
199     else
200       myAspect = new Graphic3d_AspectMarker3d(aIcon);
201
202     myAspect->SetColor(myCustomColor);
203   }
204 }
205
206 //*********************************************************************************
207 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
208                                     std::string theAttrName) const
209 {
210   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
211   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
212   if (aLine.get() == NULL)
213     return;
214   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
215
216   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
217   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
218   gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
219   gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
220
221   // Draw line by two points
222   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
223   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
224   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
225   theGroup->AddPrimitiveArray(aLines);
226 }
227
228 //*********************************************************************************
229 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
230                                             const SelectMgr_SequenceOfOwner& theOwners)
231 {
232   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
233   aSelectionPrs->Clear();
234   drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
235
236   aSelectionPrs->SetDisplayPriority(9);
237   aSelectionPrs->Display();
238   thePM->Color(this, GetContext()->SelectionStyle());
239 }
240
241 //*********************************************************************************
242 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
243                                   const Handle(PrsMgr_PresentationManager3d)& thePM,
244                                   const Handle(Prs3d_Drawer)& theStyle,
245                                   const Handle(SelectMgr_EntityOwner)& theOwner)
246 {
247   thePM->Color(this, theStyle);
248
249   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
250   aHilightPrs->Clear();
251   drawLines(aHilightPrs, theStyle->Color());
252   aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
253
254   if (thePM->IsImmediateModeOn())
255     thePM->AddToImmediateList(aHilightPrs);
256 }
257
258 //*********************************************************************************
259 void SketcherPrs_SymbolPrs::Compute(
260                 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
261                 const Handle(Prs3d_Presentation)& thePresentation,
262                 const Standard_Integer theMode)
263 {
264   // Create an icon
265   prepareAspect();
266
267   Handle(AIS_InteractiveContext) aCtx = GetContext();
268   Handle(OpenGl_GraphicDriver) aDriver =
269     Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
270   if (aDriver.IsNull())
271     return;
272
273   // Update points with default shift value
274   // it updates array of points if the presentation is ready to display, or the array of points
275   // contains the previous values
276
277   bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
278
279   int aNbVertex = myPntArray->VertexNumber();
280   if (myOwner.IsNull()) {
281     myOwner = new SelectMgr_EntityOwner(this);
282   }
283
284   // Create sensitive point for each symbol
285   mySPoints.Clear();
286   for (int i = 1; i <= aNbVertex; i++) {
287     Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
288     mySPoints.Append(aSP);
289     if (myIsCustomColor)
290       myPntArray->SetVertexColor(i, myCustomColor);
291   }
292
293   Handle(OpenGl_Group) aGroup =
294     Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
295   aGroup->SetPrimitivesAspect(myAspect);
296
297   // Recompute boundary box of the group
298   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
299   gp_Pnt aVert;
300   aBnd.Clear();
301   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
302     aVert = myPntArray->Vertice(i);
303     aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
304   }
305
306   // Pint the group with custom procedure (see Render)
307   SketcherPrs_SymbolArray* aElem =
308     new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
309   aGroup->AddElement(aElem);
310
311   // Disable frustum culling for this object by marking it as mutable
312   aGroup->Structure()->SetMutable(true);
313
314   if (!aReadyToDisplay)
315     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
316                           "An empty AIS presentation: SketcherPrs_LengthDimension");
317 }
318
319
320 //*********************************************************************************
321 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
322                                              const Standard_Integer aMode)
323 {
324   ClearSelected();
325   if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
326     for (int i = 1; i <= mySPoints.Length(); i++)
327       aSelection->Add(mySPoints.Value(i));
328   }
329 }
330
331 //*********************************************************************************
332 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
333 {
334   myIsCustomColor = !theColor.empty();
335   if (myIsCustomColor)
336     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
337                                    theColor[2] / 255., Quantity_TOC_RGB);
338   else
339     myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
340
341   if (!myAspect.IsNull())
342     myAspect->SetColor (myCustomColor);
343
344   Handle(Prs3d_Presentation) aPrs = Presentation();
345   if (!aPrs.IsNull()) {
346     if (myIsCustomColor) {
347       Handle(Graphic3d_PresentationAttributes) aAttr = new Graphic3d_PresentationAttributes();
348       aAttr->SetColor(myCustomColor);
349       aPrs->Highlight(aAttr);
350     }
351     else {
352       aPrs->UnHighlight();
353     }
354   }
355 }
356
357 //*********************************************************************************
358 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
359                                       const Handle(Prs3d_Presentation)& thePrs,
360                                       Quantity_Color theColor) const
361 {
362   Handle(Graphic3d_AspectLine3d) aLineAspect =
363     new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
364
365   Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
366   Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
367   aLinesStyle->SetAspect(aLineAspect);
368   myDrawer->SetLineAspect(aLinesStyle);
369
370   if (theShape->isEdge()) {
371     // The shape is edge
372     std::shared_ptr<GeomAPI_Curve> aCurve =
373       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
374     if (aCurve->isLine()) {
375       // The shape is line
376       GeomAdaptor_Curve
377         aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
378       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
379     } else {
380       // The shape is circle or arc
381       GeomAdaptor_Curve
382         aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
383       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
384     }
385   } else if (theShape->isVertex()) {
386     // draw vertex
387     std::shared_ptr<GeomAPI_Vertex> aVertex =
388       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
389     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
390     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
391     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
392   }
393
394   aLinesStyle->SetAspect(aOldStyle);
395   myDrawer->SetLineAspect(aLinesStyle);
396 }
397
398 //*********************************************************************************
399 void SketcherPrs_SymbolPrs::drawListOfShapes(
400                       const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
401                       const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
402 {
403   int aNb = theListAttr->size();
404   if (aNb == 0)
405     return;
406   int i;
407   ObjectPtr aObj;
408   for (i = 0; i < aNb; i++) {
409     aObj = theListAttr->object(i);
410     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
411     if (aShape.get() != NULL)
412       drawShape(aShape, thePrs, theColor);
413   }
414 }
415
416 //*********************************************************************************
417 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
418 {
419   Select3D_BndBox3d aTmpBox;
420   for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
421     const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
422     aTmpBox.Combine (anEnt->BoundingBox());
423   }
424
425   theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
426                     aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());
427 }