1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_SymbolPrs.cpp
4 // Created: 12 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_SymbolPrs.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Vertex.h>
13 #include <GeomAPI_Curve.h>
15 #include <Events_InfoMessage.h>
17 #include <Graphic3d_ArrayOfSegments.hxx>
18 #include <Graphic3d_BndBox4f.hxx>
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>
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>
44 /// Step between icons
45 static const double MyDist = 0.02;
48 //**************************************************************
49 //! Redefinition of OpenGl_Element
50 class SketcherPrs_SymbolArray: public OpenGl_PrimitiveArray
53 SketcherPrs_SymbolArray(const OpenGl_GraphicDriver* theDriver,
54 const Handle(SketcherPrs_SymbolPrs)& theObj)
55 :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
56 theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj) {}
58 virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
60 ModelAPI_Feature* aConstraint = myObj->feature();
61 if (aConstraint->data().get() && aConstraint->data()->isValid()) {
62 Handle(OpenGl_View) aView = theWorkspace->View();
63 double aScale = aView->Camera()->Scale();
64 // Update points coordinate taking the viewer scale into account
65 myObj->updateIfReadyToDisplay(MyDist * aScale);
68 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
69 Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
70 myVboAttribs->init(aCtx, 0, aAttr->NbElements,
71 aAttr->Data(), GL_NONE, aAttr->Stride);
75 myAttribs = myObj->myPntArray->Attributes();
76 myIndices = myObj->myPntArray->Indices();
77 myBounds = myObj->myPntArray->Bounds();
80 OpenGl_PrimitiveArray::Render(theWorkspace);
84 Handle(SketcherPrs_SymbolPrs) myObj;
88 //*****************************************************************************
89 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
92 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
95 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
96 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
97 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsConflicting(false)
99 SetAutoHilight(Standard_False);
100 myPntArray = new Graphic3d_ArrayOfPoints(1);
101 myPntArray->AddVertex(0., 0., 0.);
104 //*********************************************************************************
105 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
107 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
108 // Empty memory in position manager
109 aMgr->deleteConstraint(this);
113 #pragma warning( disable : 4996 )
116 //*********************************************************************************
117 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
119 if (myIconsMap.count(iconName()) == 1) {
120 return myIconsMap[iconName()];
122 // Load icon for the presentation
124 char* anEnv = getenv("SHAPER_ROOT_DIR");
126 aFile = std::string(anEnv) +
127 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
129 anEnv = getenv("OPENPARTS_ROOT_DIR");
131 aFile = std::string(anEnv) + FSEP + "resources";
136 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
137 if (aPixMap->Load(aFile.c_str())) {
138 myIconsMap[iconName()] = aPixMap;
141 // The icon for constraint is not found
142 static const char aMsg[] = "Error! constraint images are not found";
144 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
145 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
146 return Handle(Image_AlienPixMap)();
149 //*********************************************************************************
150 void SketcherPrs_SymbolPrs::prepareAspect()
152 // Create an aspect with the icon
153 if (myAspect.IsNull()) {
154 Handle(Image_AlienPixMap) aIcon = icon();
156 myAspect = new Graphic3d_AspectMarker3d();
158 myAspect = new Graphic3d_AspectMarker3d(aIcon);
162 //*********************************************************************************
163 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
164 std::string theAttrName) const
166 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
167 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
168 if (aLine.get() == NULL)
170 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
172 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
173 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
174 gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
175 gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
177 // Draw line by two points
178 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
179 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
180 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
181 theGroup->AddPrimitiveArray(aLines);
184 //*********************************************************************************
185 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
186 const SelectMgr_SequenceOfOwner& theOwners)
189 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
190 aSelectionPrs->Clear();
191 drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
193 aSelectionPrs->SetDisplayPriority(9);
194 aSelectionPrs->Display();
195 thePM->Color(this, GetContext()->SelectionStyle());
198 //*********************************************************************************
199 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
200 const Handle(PrsMgr_PresentationManager3d)& thePM,
201 const Handle(Graphic3d_HighlightStyle)& theStyle,
202 const Handle(SelectMgr_EntityOwner)& theOwner)
204 thePM->Color(this, theStyle);
206 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
207 aHilightPrs->Clear();
208 drawLines(aHilightPrs, theStyle->Color());
209 aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
211 if (thePM->IsImmediateModeOn())
212 thePM->AddToImmediateList(aHilightPrs);
215 //*********************************************************************************
216 void SketcherPrs_SymbolPrs::Compute(
217 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
218 const Handle(Prs3d_Presentation)& thePresentation,
219 const Standard_Integer theMode)
224 Handle(AIS_InteractiveContext) aCtx = GetContext();
225 Handle(OpenGl_GraphicDriver) aDriver =
226 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
227 if (aDriver.IsNull())
230 // Update points with default shift value
231 // it updates array of points if the presentation is ready to display, or the array of points
232 // contains the previous values
234 bool aReadyToDisplay = updateIfReadyToDisplay(20);
236 int aNbVertex = myPntArray->VertexNumber();
237 if (myOwner.IsNull()) {
238 myOwner = new SelectMgr_EntityOwner(this);
241 // Create sensitive point for each symbol
243 for (int i = 1; i <= aNbVertex; i++) {
244 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
245 mySPoints.Append(aSP);
248 Handle(OpenGl_Group) aGroup = Handle(OpenGl_Group)::DownCast(thePresentation->NewGroup());
249 aGroup->SetPrimitivesAspect(myAspect);
251 // Recompute boundary box of the group
252 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
255 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
256 aVert = myPntArray->Vertice(i);
257 aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
260 // Pint the group with custom procedure (see Render)
261 SketcherPrs_SymbolArray* aElem =
262 new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this);
263 aGroup->AddElement(aElem);
265 // Disable frustum culling for this object by marking it as mutable
266 aGroup->Structure()->SetMutable(true);
268 if (!aReadyToDisplay)
269 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
270 "An empty AIS presentation: SketcherPrs_LengthDimension");
274 //*********************************************************************************
275 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
276 const Standard_Integer aMode)
279 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
280 for (int i = 1; i <= mySPoints.Length(); i++)
281 aSelection->Add(mySPoints.Value(i));
285 //*********************************************************************************
286 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
287 const std::vector<int>& theColor)
291 if (!myAspect.IsNull())
292 myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255.,
293 theColor[2] / 255., Quantity_TOC_RGB));
294 myIsConflicting = true;
298 if (!myAspect.IsNull())
299 myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
300 myIsConflicting = false;
304 //*********************************************************************************
305 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
306 const Handle(Prs3d_Presentation)& thePrs) const
308 if (theShape->isEdge()) {
310 std::shared_ptr<GeomAPI_Curve> aCurve =
311 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
312 if (aCurve->isLine()) {
315 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
316 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
318 // The shape is circle or arc
320 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
321 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
323 } else if (theShape->isVertex()) {
325 std::shared_ptr<GeomAPI_Vertex> aVertex =
326 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
327 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
328 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
329 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
333 //*********************************************************************************
334 void SketcherPrs_SymbolPrs::drawListOfShapes(
335 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
336 const Handle(Prs3d_Presentation)& thePrs) const
338 int aNb = theListAttr->size();
343 for (i = 0; i < aNb; i++) {
344 aObj = theListAttr->object(i);
345 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
346 if (aShape.get() != NULL)
347 drawShape(aShape, thePrs);
351 //*********************************************************************************
352 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
354 Select3D_BndBox3d aTmpBox;
355 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
356 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
357 aTmpBox.Combine (anEnt->BoundingBox());
360 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
361 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());