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);
67 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
68 Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
69 myVboAttribs->init(aCtx, 0, aAttr->NbElements,
70 aAttr->Data(), GL_NONE, aAttr->Stride);
72 myAttribs = myObj->myPntArray->Attributes();
73 myIndices = myObj->myPntArray->Indices();
74 myBounds = myObj->myPntArray->Bounds();
77 OpenGl_PrimitiveArray::Render(theWorkspace);
81 Handle(SketcherPrs_SymbolPrs) myObj;
85 //*****************************************************************************
86 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
89 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
92 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
93 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
94 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsConflicting(false)
96 SetAutoHilight(Standard_False);
97 myPntArray = new Graphic3d_ArrayOfPoints(1);
98 myPntArray->AddVertex(0., 0., 0.);
101 //*********************************************************************************
102 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
104 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
105 // Empty memory in position manager
106 aMgr->deleteConstraint(this);
110 #pragma warning( disable : 4996 )
113 //*********************************************************************************
114 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
116 if (myIconsMap.count(iconName()) == 1) {
117 return myIconsMap[iconName()];
119 // Load icon for the presentation
121 char* anEnv = getenv("SHAPER_ROOT_DIR");
123 aFile = std::string(anEnv) +
124 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
126 anEnv = getenv("OPENPARTS_ROOT_DIR");
128 aFile = std::string(anEnv) + FSEP + "resources";
133 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
134 if (aPixMap->Load(aFile.c_str())) {
135 myIconsMap[iconName()] = aPixMap;
138 // The icon for constraint is not found
139 static const char aMsg[] = "Error! constraint images are not found";
141 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
142 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
143 return Handle(Image_AlienPixMap)();
146 //*********************************************************************************
147 void SketcherPrs_SymbolPrs::prepareAspect()
149 // Create an aspect with the icon
150 if (myAspect.IsNull()) {
151 Handle(Image_AlienPixMap) aIcon = icon();
153 myAspect = new Graphic3d_AspectMarker3d();
155 myAspect = new Graphic3d_AspectMarker3d(aIcon);
159 //*********************************************************************************
160 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
161 std::string theAttrName) const
163 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
164 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
165 if (aLine.get() == NULL)
167 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
169 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
170 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
171 gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
172 gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
174 // Draw line by two points
175 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
176 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
177 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
178 theGroup->AddPrimitiveArray(aLines);
181 //*********************************************************************************
182 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
183 const SelectMgr_SequenceOfOwner& theOwners)
186 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
187 aSelectionPrs->Clear();
188 drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
190 aSelectionPrs->SetDisplayPriority(9);
191 aSelectionPrs->Display();
192 thePM->Color(this, GetContext()->SelectionStyle());
195 //*********************************************************************************
196 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
197 const Handle(PrsMgr_PresentationManager3d)& thePM,
198 const Handle(Graphic3d_HighlightStyle)& theStyle,
199 const Handle(SelectMgr_EntityOwner)& theOwner)
201 thePM->Color(this, theStyle);
203 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
204 aHilightPrs->Clear();
205 drawLines(aHilightPrs, theStyle->Color());
206 aHilightPrs->SetZLayer (Graphic3d_ZLayerId_Topmost);
208 if (thePM->IsImmediateModeOn())
209 thePM->AddToImmediateList(aHilightPrs);
212 //*********************************************************************************
213 void SketcherPrs_SymbolPrs::Compute(
214 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
215 const Handle(Prs3d_Presentation)& thePresentation,
216 const Standard_Integer theMode)
221 Handle(AIS_InteractiveContext) aCtx = GetContext();
222 Handle(OpenGl_GraphicDriver) aDriver =
223 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
224 if (aDriver.IsNull())
227 // Update points with default shift value
228 // it updates array of points if the presentation is ready to display, or the array of points
229 // contains the previous values
231 bool aReadyToDisplay = updateIfReadyToDisplay(20);
233 int aNbVertex = myPntArray->VertexNumber();
234 if (myOwner.IsNull()) {
235 myOwner = new SelectMgr_EntityOwner(this);
238 // Create sensitive point for each symbol
240 for (int i = 1; i <= aNbVertex; i++) {
241 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
242 mySPoints.Append(aSP);
245 Handle(OpenGl_Group) aGroup = Handle(OpenGl_Group)::DownCast(thePresentation->NewGroup());
246 aGroup->SetPrimitivesAspect(myAspect);
248 // Recompute boundary box of the group
249 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
252 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
253 aVert = myPntArray->Vertice(i);
254 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
257 // Pint the group with custom procedure (see Render)
258 SketcherPrs_SymbolArray* aElem =
259 new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this);
260 aGroup->AddElement(aElem);
262 // Disable frustum culling for this object by marking it as mutable
263 //aGroup->Structure()->SetMutable(true);
265 if (!aReadyToDisplay)
266 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
267 "An empty AIS presentation: SketcherPrs_LengthDimension");
271 //*********************************************************************************
272 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
273 const Standard_Integer aMode)
276 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
277 for (int i = 1; i <= mySPoints.Length(); i++)
278 aSelection->Add(mySPoints.Value(i));
282 //*********************************************************************************
283 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
284 const std::vector<int>& theColor)
288 if (!myAspect.IsNull())
289 myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255.,
290 theColor[2] / 255., Quantity_TOC_RGB));
291 myIsConflicting = true;
295 if (!myAspect.IsNull())
296 myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
297 myIsConflicting = false;
301 //*********************************************************************************
302 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
303 const Handle(Prs3d_Presentation)& thePrs) const
305 if (theShape->isEdge()) {
307 std::shared_ptr<GeomAPI_Curve> aCurve =
308 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
309 if (aCurve->isLine()) {
312 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
313 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
315 // The shape is circle or arc
317 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
318 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
320 } else if (theShape->isVertex()) {
322 std::shared_ptr<GeomAPI_Vertex> aVertex =
323 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
324 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
325 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
326 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
330 //*********************************************************************************
331 void SketcherPrs_SymbolPrs::drawListOfShapes(
332 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
333 const Handle(Prs3d_Presentation)& thePrs) const
335 int aNb = theListAttr->size();
340 for (i = 0; i < aNb; i++) {
341 aObj = theListAttr->object(i);
342 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
343 if (aShape.get() != NULL)
344 drawShape(aShape, thePrs);
348 //*********************************************************************************
349 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
351 Select3D_BndBox3d aTmpBox;
352 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
353 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
354 aTmpBox.Combine (anEnt->BoundingBox());
357 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
358 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());