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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "SketcherPrs_SymbolPrs.h"
21 #include "SketcherPrs_Tools.h"
22 #include "SketcherPrs_PositionMgr.h"
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_Curve.h>
28 #include <Events_InfoMessage.h>
30 #include <Graphic3d_ArrayOfSegments.hxx>
31 #include <Graphic3d_BndBox4f.hxx>
33 #include <SelectMgr_Selection.hxx>
34 #include <Select3D_SensitivePoint.hxx>
35 #include <TopLoc_Location.hxx>
36 #include <AIS_InteractiveContext.hxx>
37 #include <V3d_Viewer.hxx>
38 #include <Prs3d_Root.hxx>
39 #include <Geom_CartesianPoint.hxx>
40 #include <GeomAdaptor_Curve.hxx>
41 #include <StdPrs_DeflectionCurve.hxx>
42 #include <StdPrs_Point.hxx>
43 #include <StdPrs_Curve.hxx>
44 #include <Prs3d_LineAspect.hxx>
46 #include <OpenGl_Element.hxx>
47 #include <OpenGl_GraphicDriver.hxx>
48 #include <OpenGl_Context.hxx>
49 #include <OpenGl_View.hxx>
50 #include <OpenGl_Group.hxx>
58 /// Step between icons
59 static const double MyDist = 0.02;
62 //**************************************************************
63 //! Redefinition of OpenGl_Element
64 class SketcherPrs_SymbolArray: public OpenGl_PrimitiveArray
67 SketcherPrs_SymbolArray(const OpenGl_GraphicDriver* theDriver,
68 const Handle(SketcherPrs_SymbolPrs)& theObj, const Handle(AIS_InteractiveContext)& theCtx)
69 :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
70 theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj),
73 virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
75 ModelAPI_Feature* aConstraint = myObj->feature();
76 if (aConstraint->data().get() && aConstraint->data()->isValid()) {
77 Handle(OpenGl_View) aView = theWorkspace->View();
78 double aScale = aView->Camera()->Scale();
79 // Update points coordinate taking the viewer scale into account
80 myObj->updateIfReadyToDisplay(MyDist * aScale, myObj->myIsCustomColor);
83 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
84 Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
85 myVboAttribs->init(aCtx, 0, aAttr->NbElements,
86 aAttr->Data(), GL_NONE, aAttr->Stride);
90 myAttribs = myObj->myPntArray->Attributes();
91 myIndices = myObj->myPntArray->Indices();
92 myBounds = myObj->myPntArray->Bounds();
95 OpenGl_PrimitiveArray::Render(theWorkspace);
97 // Update selection position only if there is no selected object
98 // because it can corrupt selection of other objects
99 if ((myContext->NbCurrents() == 0) && (myContext->NbSelected() == 0)) {
100 myContext->MainSelector()->RebuildSensitivesTree(myObj);
101 myContext->MainSelector()->RebuildObjectsTree (false);
106 Handle(SketcherPrs_SymbolPrs) myObj;
107 Handle(AIS_InteractiveContext) myContext;
111 //*****************************************************************************
112 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
115 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
118 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
119 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
120 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsCustomColor(false)
122 SetAutoHilight(Standard_False);
125 //*********************************************************************************
126 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
128 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
129 // Empty memory in position manager
130 aMgr->deleteConstraint(this);
132 Handle(Prs3d_Presentation) aSelPrs =
133 GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
134 if (!aSelPrs.IsNull()) {
135 if (!aSelPrs->Groups().IsEmpty()) {
140 Handle(Prs3d_Presentation) aHilightPrs =
141 GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
142 if (!aHilightPrs.IsNull()) {
143 if (!aHilightPrs->Groups().IsEmpty()) {
144 aHilightPrs->Clear();
146 aHilightPrs->Erase();
151 #pragma warning( disable : 4996 )
154 //*********************************************************************************
155 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
157 if (myIconsMap.count(iconName()) == 1) {
158 return myIconsMap[iconName()];
160 // Load icon for the presentation
162 char* anEnv = getenv("SHAPER_ROOT_DIR");
164 aFile = std::string(anEnv) +
165 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
167 anEnv = getenv("OPENPARTS_ROOT_DIR");
169 aFile = std::string(anEnv) + FSEP + "resources";
174 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
175 if (aPixMap->Load(aFile.c_str())) {
176 myIconsMap[iconName()] = aPixMap;
179 // The icon for constraint is not found
180 static const char aMsg[] = "Error! constraint images are not found";
182 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
183 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
184 return Handle(Image_AlienPixMap)();
187 //*********************************************************************************
188 void SketcherPrs_SymbolPrs::prepareAspect()
190 // Create an aspect with the icon
191 if (myAspect.IsNull()) {
192 Handle(Image_AlienPixMap) aIcon = icon();
194 myAspect = new Graphic3d_AspectMarker3d();
196 myAspect = new Graphic3d_AspectMarker3d(aIcon);
198 myAspect->SetColor(myCustomColor);
202 //*********************************************************************************
203 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
204 std::string theAttrName) const
206 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
207 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
208 if (aLine.get() == NULL)
210 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
212 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
213 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
214 gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
215 gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
217 // Draw line by two points
218 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
219 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
220 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
221 theGroup->AddPrimitiveArray(aLines);
224 //*********************************************************************************
225 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
226 const SelectMgr_SequenceOfOwner& theOwners)
228 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
229 aSelectionPrs->Clear();
230 drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
232 aSelectionPrs->SetDisplayPriority(9);
233 aSelectionPrs->Display();
234 thePM->Color(this, GetContext()->SelectionStyle());
237 //*********************************************************************************
238 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
239 const Handle(PrsMgr_PresentationManager3d)& thePM,
240 const Handle(Graphic3d_HighlightStyle)& theStyle,
241 const Handle(SelectMgr_EntityOwner)& theOwner)
243 thePM->Color(this, theStyle);
245 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
246 aHilightPrs->Clear();
247 drawLines(aHilightPrs, theStyle->Color());
248 aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
250 if (thePM->IsImmediateModeOn())
251 thePM->AddToImmediateList(aHilightPrs);
254 //*********************************************************************************
255 void SketcherPrs_SymbolPrs::Compute(
256 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
257 const Handle(Prs3d_Presentation)& thePresentation,
258 const Standard_Integer theMode)
263 Handle(AIS_InteractiveContext) aCtx = GetContext();
264 Handle(OpenGl_GraphicDriver) aDriver =
265 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
266 if (aDriver.IsNull())
269 // Update points with default shift value
270 // it updates array of points if the presentation is ready to display, or the array of points
271 // contains the previous values
273 bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
275 int aNbVertex = myPntArray->VertexNumber();
276 if (myOwner.IsNull()) {
277 myOwner = new SelectMgr_EntityOwner(this);
280 // Create sensitive point for each symbol
282 for (int i = 1; i <= aNbVertex; i++) {
283 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
284 mySPoints.Append(aSP);
286 myPntArray->SetVertexColor(i, myCustomColor);
289 Handle(OpenGl_Group) aGroup =
290 Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
291 aGroup->SetPrimitivesAspect(myAspect);
293 // Recompute boundary box of the group
294 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
297 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
298 aVert = myPntArray->Vertice(i);
299 aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
302 // Pint the group with custom procedure (see Render)
303 SketcherPrs_SymbolArray* aElem =
304 new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
305 aGroup->AddElement(aElem);
307 // Disable frustum culling for this object by marking it as mutable
308 aGroup->Structure()->SetMutable(true);
310 if (!aReadyToDisplay)
311 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
312 "An empty AIS presentation: SketcherPrs_LengthDimension");
316 //*********************************************************************************
317 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
318 const Standard_Integer aMode)
321 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
322 for (int i = 1; i <= mySPoints.Length(); i++)
323 aSelection->Add(mySPoints.Value(i));
327 //*********************************************************************************
328 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
330 myIsCustomColor = !theColor.empty();
332 myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
333 theColor[2] / 255., Quantity_TOC_RGB);
335 myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
337 if (!myAspect.IsNull())
338 myAspect->SetColor (myCustomColor);
341 //*********************************************************************************
342 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
343 const Handle(Prs3d_Presentation)& thePrs,
344 Quantity_Color theColor) const
346 int aColNam = theColor.Name();
347 //cout<<"### SketcherPrs_SymbolPrs::drawShape "<<theColor.Name()<<endl;
348 Handle(Graphic3d_AspectLine3d) aLineAspect =
349 new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
351 Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
352 Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
353 aLinesStyle->SetAspect(aLineAspect);
354 myDrawer->SetLineAspect(aLinesStyle);
356 if (theShape->isEdge()) {
358 std::shared_ptr<GeomAPI_Curve> aCurve =
359 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
360 if (aCurve->isLine()) {
363 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
364 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
366 // The shape is circle or arc
368 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
369 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
371 } else if (theShape->isVertex()) {
373 std::shared_ptr<GeomAPI_Vertex> aVertex =
374 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
375 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
376 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
377 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
380 aLinesStyle->SetAspect(aOldStyle);
381 myDrawer->SetLineAspect(aLinesStyle);
384 //*********************************************************************************
385 void SketcherPrs_SymbolPrs::drawListOfShapes(
386 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
387 const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
389 int aNb = theListAttr->size();
394 for (i = 0; i < aNb; i++) {
395 aObj = theListAttr->object(i);
396 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
397 if (aShape.get() != NULL)
398 drawShape(aShape, thePrs, theColor);
402 //*********************************************************************************
403 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
405 Select3D_BndBox3d aTmpBox;
406 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
407 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
408 aTmpBox.Combine (anEnt->BoundingBox());
411 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
412 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());