1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketcherPrs_SymbolPrs.h"
22 #include "SketcherPrs_Tools.h"
23 #include "SketcherPrs_PositionMgr.h"
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Vertex.h>
27 #include <GeomAPI_Curve.h>
29 #include <Events_InfoMessage.h>
31 #include <Graphic3d_ArrayOfSegments.hxx>
32 #include <Graphic3d_BndBox4f.hxx>
34 #include <SelectMgr_Selection.hxx>
35 #include <Select3D_SensitivePoint.hxx>
36 #include <TopLoc_Location.hxx>
37 #include <AIS_InteractiveContext.hxx>
38 #include <V3d_Viewer.hxx>
39 #include <Prs3d_Root.hxx>
40 #include <Geom_CartesianPoint.hxx>
41 #include <GeomAdaptor_Curve.hxx>
42 #include <StdPrs_DeflectionCurve.hxx>
43 #include <StdPrs_Point.hxx>
44 #include <StdPrs_Curve.hxx>
45 #include <Prs3d_LineAspect.hxx>
47 #include <OpenGl_Element.hxx>
48 #include <OpenGl_GraphicDriver.hxx>
49 #include <OpenGl_Context.hxx>
50 #include <OpenGl_View.hxx>
51 #include <OpenGl_Group.hxx>
59 /// Step between icons
60 static const double MyDist = 0.02;
63 //**************************************************************
64 //! Redefinition of OpenGl_Element
65 class SketcherPrs_SymbolArray: public OpenGl_PrimitiveArray
68 SketcherPrs_SymbolArray(const OpenGl_GraphicDriver* theDriver,
69 const Handle(SketcherPrs_SymbolPrs)& theObj, const Handle(AIS_InteractiveContext)& theCtx)
70 :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
71 theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj),
74 virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
76 ModelAPI_Feature* aConstraint = myObj->feature();
77 if (aConstraint->data().get() && aConstraint->data()->isValid()) {
78 Handle(OpenGl_View) aView = theWorkspace->View();
79 double aScale = aView->Camera()->Scale();
80 // Update points coordinate taking the viewer scale into account
81 myObj->updateIfReadyToDisplay(MyDist * aScale, myObj->myIsCustomColor);
84 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
85 Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
86 myVboAttribs->init(aCtx, 0, aAttr->NbElements,
87 aAttr->Data(), GL_NONE, aAttr->Stride);
91 myAttribs = myObj->myPntArray->Attributes();
92 myIndices = myObj->myPntArray->Indices();
93 myBounds = myObj->myPntArray->Bounds();
96 OpenGl_PrimitiveArray::Render(theWorkspace);
98 // Update selection position only if there is no selected object
99 // because it can corrupt selection of other objects
100 if ((myContext->NbCurrents() == 0) && (myContext->NbSelected() == 0)) {
101 myContext->MainSelector()->RebuildSensitivesTree(myObj);
102 myContext->MainSelector()->RebuildObjectsTree (false);
107 Handle(SketcherPrs_SymbolPrs) myObj;
108 Handle(AIS_InteractiveContext) myContext;
112 //*****************************************************************************
113 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
116 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
119 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
120 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
121 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsCustomColor(false)
123 SetAutoHilight(Standard_False);
126 //*********************************************************************************
127 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
129 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
130 // Empty memory in position manager
131 aMgr->deleteConstraint(this);
133 Handle(Prs3d_Presentation) aSelPrs =
134 GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
135 if (!aSelPrs.IsNull()) {
136 if (!aSelPrs->Groups().IsEmpty()) {
141 Handle(Prs3d_Presentation) aHilightPrs =
142 GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
143 if (!aHilightPrs.IsNull()) {
144 if (!aHilightPrs->Groups().IsEmpty()) {
145 aHilightPrs->Clear();
147 aHilightPrs->Erase();
152 #pragma warning( disable : 4996 )
155 //*********************************************************************************
156 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
158 if (myIconsMap.count(iconName()) == 1) {
159 return myIconsMap[iconName()];
161 // Load icon for the presentation
163 char* anEnv = getenv("SHAPER_ROOT_DIR");
165 aFile = std::string(anEnv) +
166 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
168 anEnv = getenv("OPENPARTS_ROOT_DIR");
170 aFile = std::string(anEnv) + FSEP + "resources";
175 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
176 if (aPixMap->Load(aFile.c_str())) {
177 myIconsMap[iconName()] = aPixMap;
180 // The icon for constraint is not found
181 static const char aMsg[] = "Error! constraint images are not found";
183 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
184 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
185 return Handle(Image_AlienPixMap)();
188 //*********************************************************************************
189 void SketcherPrs_SymbolPrs::prepareAspect()
191 // Create an aspect with the icon
192 if (myAspect.IsNull()) {
193 Handle(Image_AlienPixMap) aIcon = icon();
195 myAspect = new Graphic3d_AspectMarker3d();
197 myAspect = new Graphic3d_AspectMarker3d(aIcon);
199 myAspect->SetColor(myCustomColor);
203 //*********************************************************************************
204 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
205 std::string theAttrName) const
207 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
208 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
209 if (aLine.get() == NULL)
211 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
213 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
214 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
215 gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
216 gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
218 // Draw line by two points
219 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
220 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
221 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
222 theGroup->AddPrimitiveArray(aLines);
225 //*********************************************************************************
226 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
227 const SelectMgr_SequenceOfOwner& theOwners)
229 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
230 aSelectionPrs->Clear();
231 drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
233 aSelectionPrs->SetDisplayPriority(9);
234 aSelectionPrs->Display();
235 thePM->Color(this, GetContext()->SelectionStyle());
238 //*********************************************************************************
239 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
240 const Handle(PrsMgr_PresentationManager3d)& thePM,
241 const Handle(Graphic3d_HighlightStyle)& theStyle,
242 const Handle(SelectMgr_EntityOwner)& theOwner)
244 thePM->Color(this, theStyle);
246 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
247 aHilightPrs->Clear();
248 drawLines(aHilightPrs, theStyle->Color());
249 aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
251 if (thePM->IsImmediateModeOn())
252 thePM->AddToImmediateList(aHilightPrs);
255 //*********************************************************************************
256 void SketcherPrs_SymbolPrs::Compute(
257 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
258 const Handle(Prs3d_Presentation)& thePresentation,
259 const Standard_Integer theMode)
264 Handle(AIS_InteractiveContext) aCtx = GetContext();
265 Handle(OpenGl_GraphicDriver) aDriver =
266 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
267 if (aDriver.IsNull())
270 // Update points with default shift value
271 // it updates array of points if the presentation is ready to display, or the array of points
272 // contains the previous values
274 bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
276 int aNbVertex = myPntArray->VertexNumber();
277 if (myOwner.IsNull()) {
278 myOwner = new SelectMgr_EntityOwner(this);
281 // Create sensitive point for each symbol
283 for (int i = 1; i <= aNbVertex; i++) {
284 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
285 mySPoints.Append(aSP);
287 myPntArray->SetVertexColor(i, myCustomColor);
290 Handle(OpenGl_Group) aGroup =
291 Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
292 aGroup->SetPrimitivesAspect(myAspect);
294 // Recompute boundary box of the group
295 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
298 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
299 aVert = myPntArray->Vertice(i);
300 aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
303 // Pint the group with custom procedure (see Render)
304 SketcherPrs_SymbolArray* aElem =
305 new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
306 aGroup->AddElement(aElem);
308 // Disable frustum culling for this object by marking it as mutable
309 aGroup->Structure()->SetMutable(true);
311 if (!aReadyToDisplay)
312 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
313 "An empty AIS presentation: SketcherPrs_LengthDimension");
317 //*********************************************************************************
318 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
319 const Standard_Integer aMode)
322 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
323 for (int i = 1; i <= mySPoints.Length(); i++)
324 aSelection->Add(mySPoints.Value(i));
328 //*********************************************************************************
329 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
331 myIsCustomColor = !theColor.empty();
333 myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
334 theColor[2] / 255., Quantity_TOC_RGB);
336 myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
338 if (!myAspect.IsNull())
339 myAspect->SetColor (myCustomColor);
342 //*********************************************************************************
343 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
344 const Handle(Prs3d_Presentation)& thePrs,
345 Quantity_Color theColor) const
347 int aColNam = theColor.Name();
348 //cout<<"### SketcherPrs_SymbolPrs::drawShape "<<theColor.Name()<<endl;
349 Handle(Graphic3d_AspectLine3d) aLineAspect =
350 new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
352 Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
353 Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
354 aLinesStyle->SetAspect(aLineAspect);
355 myDrawer->SetLineAspect(aLinesStyle);
357 if (theShape->isEdge()) {
359 std::shared_ptr<GeomAPI_Curve> aCurve =
360 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
361 if (aCurve->isLine()) {
364 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
365 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
367 // The shape is circle or arc
369 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
370 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
372 } else if (theShape->isVertex()) {
374 std::shared_ptr<GeomAPI_Vertex> aVertex =
375 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
376 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
377 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
378 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
381 aLinesStyle->SetAspect(aOldStyle);
382 myDrawer->SetLineAspect(aLinesStyle);
385 //*********************************************************************************
386 void SketcherPrs_SymbolPrs::drawListOfShapes(
387 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
388 const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
390 int aNb = theListAttr->size();
395 for (i = 0; i < aNb; i++) {
396 aObj = theListAttr->object(i);
397 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
398 if (aShape.get() != NULL)
399 drawShape(aShape, thePrs, theColor);
403 //*********************************************************************************
404 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
406 Select3D_BndBox3d aTmpBox;
407 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
408 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
409 aTmpBox.Combine (anEnt->BoundingBox());
412 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
413 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());