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