]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketcherPrs/SketcherPrs_SymbolPrs.cpp
Salome HOME
9bd5a88defd81b0ef14b098c58f64453b8f5367c
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_SymbolPrs.cpp
1 // Copyright (C) 2014-2020  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(),
123    myConstraint(theConstraint),
124    mySketcher(theSketcher),
125    myIsCustomColor(false)
126 {
127   SetAutoHilight(Standard_False);
128 }
129
130 //*********************************************************************************
131 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
132 {
133   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
134   // Empty memory in position manager
135   aMgr->deleteConstraint(this);
136
137   Handle(Prs3d_Presentation) aSelPrs =
138     GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
139   if (!aSelPrs.IsNull()) {
140       if (!aSelPrs->Groups().IsEmpty()) {
141         aSelPrs->Clear();
142       }
143       aSelPrs->Erase();
144   }
145   Handle(Prs3d_Presentation) aHilightPrs =
146     GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
147   if (!aHilightPrs.IsNull()) {
148     if (!aHilightPrs->Groups().IsEmpty()) {
149       aHilightPrs->Clear();
150     }
151     aHilightPrs->Erase();
152   }
153 }
154
155 #ifdef _WINDOWS
156 #pragma warning( disable : 4996 )
157 #endif
158
159 //*********************************************************************************
160 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
161 {
162   if (myIconsMap.count(iconName()) == 1) {
163     return myIconsMap[iconName()];
164   }
165   // Load icon for the presentation
166   std::string aFile;
167   char* anEnv = getenv("SHAPER_ROOT_DIR");
168   if (anEnv) {
169     aFile = std::string(anEnv) +
170       FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
171   } else {
172     anEnv = getenv("CADBUILDER_ROOT_DIR");
173     if (anEnv)
174       aFile = std::string(anEnv) + FSEP + "resources";
175   }
176
177   aFile += FSEP;
178   aFile += iconName();
179   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
180   if (aPixMap->Load(aFile.c_str())) {
181     int aRatio = SketcherPrs_Tools::pixelRatio();
182     if (aRatio > 1) {
183       Handle(Image_AlienPixMap) aSizedMap = new Image_AlienPixMap();
184       Standard_Size aWidth = aPixMap->Width() * aRatio;
185       Standard_Size aHeigh = aPixMap->Height() * aRatio;
186       aSizedMap->InitTrash(aPixMap->Format(), aWidth, aHeigh);
187       for (Standard_Size i = 0; i < aWidth; i++) {
188         for (Standard_Size j = 0; j < aHeigh; j++) {
189           aSizedMap->SetPixelColor(int(i), int(j),
190               aPixMap->PixelColor(int(i / aRatio), int(j / aRatio)));
191         }
192       }
193       aPixMap = aSizedMap;
194     }
195     myIconsMap[iconName()] = aPixMap;
196     return aPixMap;
197   }
198   // The icon for constraint is not found
199   static const char aMsg[] = "Error! constraint images are not found";
200   std::cout<<aMsg<<std::endl;
201   Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
202   myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
203   return Handle(Image_AlienPixMap)();
204 }
205
206 //*********************************************************************************
207 void SketcherPrs_SymbolPrs::prepareAspect()
208 {
209   // Create an aspect with the icon
210   if (myAspect.IsNull()) {
211     Handle(Image_AlienPixMap) aIcon = icon();
212     if (aIcon.IsNull())
213       myAspect = new Graphic3d_AspectMarker3d();
214     else
215       myAspect = new Graphic3d_AspectMarker3d(aIcon);
216
217     myAspect->SetColor(myCustomColor);
218   }
219 }
220
221 //*********************************************************************************
222 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
223                                     std::string theAttrName) const
224 {
225   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
226   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
227   if (aLine.get() == NULL)
228     return;
229   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
230
231   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
232   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
233
234   // Draw line by two points
235   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
236   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
237   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
238   theGroup->AddPrimitiveArray(aLines);
239 }
240
241 //*********************************************************************************
242 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
243                                             const SelectMgr_SequenceOfOwner& /*theOwners*/)
244 {
245   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
246   aSelectionPrs->Clear();
247   drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
248
249   aSelectionPrs->SetDisplayPriority(9);
250   aSelectionPrs->Display();
251   thePM->Color(this, GetContext()->SelectionStyle());
252 }
253
254 //*********************************************************************************
255 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
256                                   const Handle(PrsMgr_PresentationManager3d)& thePM,
257                                   const Handle(Prs3d_Drawer)& theStyle,
258                                   const Handle(SelectMgr_EntityOwner)& /*theOwner*/)
259 {
260   thePM->Color(this, theStyle);
261
262   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
263   aHilightPrs->Clear();
264   drawLines(aHilightPrs, theStyle->Color());
265   aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
266
267   if (thePM->IsImmediateModeOn())
268     thePM->AddToImmediateList(aHilightPrs);
269 }
270
271 //*********************************************************************************
272 void SketcherPrs_SymbolPrs::Compute(
273                 const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
274                 const Handle(Prs3d_Presentation)& thePresentation,
275                 const Standard_Integer /*theMode*/)
276 {
277   if (!plane().get())
278     return;
279   // Create an icon
280   prepareAspect();
281
282   Handle(AIS_InteractiveContext) aCtx = GetContext();
283   Handle(OpenGl_GraphicDriver) aDriver =
284     Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
285   if (aDriver.IsNull())
286     return;
287
288   // Update points with default shift value
289   // it updates array of points if the presentation is ready to display, or the array of points
290   // contains the previous values
291
292   bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
293
294   int aNbVertex = myPntArray->VertexNumber();
295   if (myOwner.IsNull()) {
296     myOwner = new SelectMgr_EntityOwner(this);
297   }
298
299   // Create sensitive point for each symbol
300   mySPoints.Clear();
301   for (int i = 1; i <= aNbVertex; i++) {
302     Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
303     mySPoints.Append(aSP);
304     if (myIsCustomColor)
305       myPntArray->SetVertexColor(i, myCustomColor);
306   }
307
308   Handle(OpenGl_Group) aGroup =
309     Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
310   aGroup->SetPrimitivesAspect(myAspect);
311
312   // Recompute boundary box of the group
313   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
314   gp_Pnt aVert;
315   aBnd.Clear();
316   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
317     aVert = myPntArray->Vertice(i);
318     aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
319   }
320
321   // Pint the group with custom procedure (see Render)
322   SketcherPrs_SymbolArray* aElem =
323     new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
324   aGroup->AddElement(aElem);
325
326   // Disable frustum culling for this object by marking it as mutable
327   aGroup->Structure()->SetMutable(true);
328
329   if (!aReadyToDisplay)
330     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
331                           "An empty AIS presentation: SketcherPrs_LengthDimension");
332 }
333
334
335 //*********************************************************************************
336 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
337                                              const Standard_Integer aMode)
338 {
339   ClearSelected();
340   if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
341     for (int i = 1; i <= mySPoints.Length(); i++)
342       aSelection->Add(mySPoints.Value(i));
343   }
344 }
345
346 //*********************************************************************************
347 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
348 {
349   myIsCustomColor = !theColor.empty();
350   if (myIsCustomColor)
351     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
352                                    theColor[2] / 255., Quantity_TOC_RGB);
353   else
354     myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
355
356   if (!myAspect.IsNull())
357     myAspect->SetColor (myCustomColor);
358
359   Handle(Prs3d_Presentation) aPrs = Presentation();
360   if (!aPrs.IsNull()) {
361     if (myIsCustomColor) {
362       Handle(Graphic3d_PresentationAttributes) aAttr = new Graphic3d_PresentationAttributes();
363       aAttr->SetColor(myCustomColor);
364       aPrs->Highlight(aAttr);
365     }
366     else {
367       aPrs->UnHighlight();
368     }
369   }
370 }
371
372 //*********************************************************************************
373 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
374                                       const Handle(Prs3d_Presentation)& thePrs,
375                                       Quantity_Color theColor) const
376 {
377   Handle(Graphic3d_AspectLine3d) aLineAspect =
378     new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
379
380   Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
381   Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
382   aLinesStyle->SetAspect(aLineAspect);
383   myDrawer->SetLineAspect(aLinesStyle);
384
385   if (theShape->isEdge()) {
386     // The shape is edge
387     std::shared_ptr<GeomAPI_Curve> aCurve =
388       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
389     if (aCurve->isLine()) {
390       // The shape is line
391       GeomAdaptor_Curve
392         aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
393       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
394     } else {
395       // The shape is circle or arc
396       GeomAdaptor_Curve
397         aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
398       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
399     }
400   } else if (theShape->isVertex()) {
401     // draw vertex
402     std::shared_ptr<GeomAPI_Vertex> aVertex =
403       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
404     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
405     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
406     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
407   }
408
409   aLinesStyle->SetAspect(aOldStyle);
410   myDrawer->SetLineAspect(aLinesStyle);
411 }
412
413 //*********************************************************************************
414 void SketcherPrs_SymbolPrs::drawListOfShapes(
415                       const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
416                       const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
417 {
418   int aNb = theListAttr->size();
419   if (aNb == 0)
420     return;
421   int i;
422   ObjectPtr aObj;
423   for (i = 0; i < aNb; i++) {
424     aObj = theListAttr->object(i);
425     std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
426     if (aShape.get() != NULL)
427       drawShape(aShape, thePrs, theColor);
428   }
429 }
430
431 //*********************************************************************************
432 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
433 {
434   Select3D_BndBox3d aTmpBox;
435   for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
436     const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
437     aTmpBox.Combine (anEnt->BoundingBox());
438   }
439
440   theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
441                     aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());
442 }