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, const Handle(AIS_InteractiveContext)& theCtx)
55 :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
56 theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj), myContext(theCtx) {}
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);
82 // Update selection position only if there is no selected object
83 // because it can corrupt selection of other objects
84 if ((myContext->NbCurrents() == 0) && (myContext->NbSelected() == 0)) {
85 myContext->MainSelector()->RebuildSensitivesTree(myObj);
86 myContext->MainSelector()->RebuildObjectsTree (false);
91 Handle(SketcherPrs_SymbolPrs) myObj;
92 Handle(AIS_InteractiveContext) myContext;
96 //*****************************************************************************
97 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
100 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
103 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
104 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
105 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsConflicting(false)
107 SetAutoHilight(Standard_False);
108 myPntArray = new Graphic3d_ArrayOfPoints(1);
109 myPntArray->AddVertex(0., 0., 0.);
112 //*********************************************************************************
113 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
115 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
116 // Empty memory in position manager
117 aMgr->deleteConstraint(this);
121 #pragma warning( disable : 4996 )
124 //*********************************************************************************
125 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
127 if (myIconsMap.count(iconName()) == 1) {
128 return myIconsMap[iconName()];
130 // Load icon for the presentation
132 char* anEnv = getenv("SHAPER_ROOT_DIR");
134 aFile = std::string(anEnv) +
135 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
137 anEnv = getenv("OPENPARTS_ROOT_DIR");
139 aFile = std::string(anEnv) + FSEP + "resources";
144 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
145 if (aPixMap->Load(aFile.c_str())) {
146 myIconsMap[iconName()] = aPixMap;
149 // The icon for constraint is not found
150 static const char aMsg[] = "Error! constraint images are not found";
152 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
153 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
154 return Handle(Image_AlienPixMap)();
157 //*********************************************************************************
158 void SketcherPrs_SymbolPrs::prepareAspect()
160 // Create an aspect with the icon
161 if (myAspect.IsNull()) {
162 Handle(Image_AlienPixMap) aIcon = icon();
164 myAspect = new Graphic3d_AspectMarker3d();
166 myAspect = new Graphic3d_AspectMarker3d(aIcon);
170 //*********************************************************************************
171 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
172 std::string theAttrName) const
174 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
175 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
176 if (aLine.get() == NULL)
178 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
180 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
181 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
182 gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
183 gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
185 // Draw line by two points
186 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
187 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
188 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
189 theGroup->AddPrimitiveArray(aLines);
192 //*********************************************************************************
193 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
194 const SelectMgr_SequenceOfOwner& theOwners)
197 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
198 aSelectionPrs->Clear();
199 drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
201 aSelectionPrs->SetDisplayPriority(9);
202 aSelectionPrs->Display();
203 thePM->Color(this, GetContext()->SelectionStyle());
206 //*********************************************************************************
207 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
208 const Handle(PrsMgr_PresentationManager3d)& thePM,
209 const Handle(Graphic3d_HighlightStyle)& theStyle,
210 const Handle(SelectMgr_EntityOwner)& theOwner)
212 thePM->Color(this, theStyle);
214 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
215 aHilightPrs->Clear();
216 drawLines(aHilightPrs, theStyle->Color());
217 aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
219 if (thePM->IsImmediateModeOn())
220 thePM->AddToImmediateList(aHilightPrs);
223 //*********************************************************************************
224 void SketcherPrs_SymbolPrs::Compute(
225 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
226 const Handle(Prs3d_Presentation)& thePresentation,
227 const Standard_Integer theMode)
232 Handle(AIS_InteractiveContext) aCtx = GetContext();
233 Handle(OpenGl_GraphicDriver) aDriver =
234 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
235 if (aDriver.IsNull())
238 // Update points with default shift value
239 // it updates array of points if the presentation is ready to display, or the array of points
240 // contains the previous values
242 bool aReadyToDisplay = updateIfReadyToDisplay(20);
244 int aNbVertex = myPntArray->VertexNumber();
245 if (myOwner.IsNull()) {
246 myOwner = new SelectMgr_EntityOwner(this);
249 // Create sensitive point for each symbol
251 for (int i = 1; i <= aNbVertex; i++) {
252 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
253 mySPoints.Append(aSP);
256 Handle(OpenGl_Group) aGroup = Handle(OpenGl_Group)::DownCast(thePresentation->NewGroup());
257 aGroup->SetPrimitivesAspect(myAspect);
259 // Recompute boundary box of the group
260 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
263 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
264 aVert = myPntArray->Vertice(i);
265 aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
268 // Pint the group with custom procedure (see Render)
269 SketcherPrs_SymbolArray* aElem =
270 new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
271 aGroup->AddElement(aElem);
273 // Disable frustum culling for this object by marking it as mutable
274 aGroup->Structure()->SetMutable(true);
276 if (!aReadyToDisplay)
277 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
278 "An empty AIS presentation: SketcherPrs_LengthDimension");
282 //*********************************************************************************
283 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
284 const Standard_Integer aMode)
287 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
288 for (int i = 1; i <= mySPoints.Length(); i++)
289 aSelection->Add(mySPoints.Value(i));
293 //*********************************************************************************
294 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
295 const std::vector<int>& theColor)
299 if (!myAspect.IsNull())
300 myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255.,
301 theColor[2] / 255., Quantity_TOC_RGB));
302 myIsConflicting = true;
306 if (!myAspect.IsNull())
307 myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
308 myIsConflicting = false;
312 //*********************************************************************************
313 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
314 const Handle(Prs3d_Presentation)& thePrs) const
316 if (theShape->isEdge()) {
318 std::shared_ptr<GeomAPI_Curve> aCurve =
319 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
320 if (aCurve->isLine()) {
323 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
324 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
326 // The shape is circle or arc
328 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
329 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
331 } else if (theShape->isVertex()) {
333 std::shared_ptr<GeomAPI_Vertex> aVertex =
334 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
335 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
336 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
337 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
341 //*********************************************************************************
342 void SketcherPrs_SymbolPrs::drawListOfShapes(
343 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
344 const Handle(Prs3d_Presentation)& thePrs) const
346 int aNb = theListAttr->size();
351 for (i = 0; i < aNb; i++) {
352 aObj = theListAttr->object(i);
353 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
354 if (aShape.get() != NULL)
355 drawShape(aShape, thePrs);
359 //*********************************************************************************
360 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
362 Select3D_BndBox3d aTmpBox;
363 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
364 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
365 aTmpBox.Combine (anEnt->BoundingBox());
368 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
369 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());