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