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 <ModelAPI_Tools.h>
31 #include <Events_InfoMessage.h>
33 #include <Graphic3d_ArrayOfSegments.hxx>
34 #include <Graphic3d_BndBox4f.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <Select3D_SensitivePoint.hxx>
38 #include <TopLoc_Location.hxx>
39 #include <AIS_InteractiveContext.hxx>
40 #include <V3d_Viewer.hxx>
41 #include <Prs3d_Root.hxx>
42 #include <Geom_CartesianPoint.hxx>
43 #include <GeomAdaptor_Curve.hxx>
44 #include <StdPrs_DeflectionCurve.hxx>
45 #include <StdPrs_Point.hxx>
46 #include <StdPrs_Curve.hxx>
47 #include <Prs3d_LineAspect.hxx>
49 #include <OpenGl_Element.hxx>
50 #include <OpenGl_GraphicDriver.hxx>
51 #include <OpenGl_Context.hxx>
52 #include <OpenGl_View.hxx>
53 #include <OpenGl_Group.hxx>
61 /// Step between icons
62 static const double MyDist = 0.02;
65 //**************************************************************
66 //! Redefinition of OpenGl_Element
67 class SketcherPrs_SymbolArray: public OpenGl_PrimitiveArray
70 SketcherPrs_SymbolArray(const OpenGl_GraphicDriver* theDriver,
71 const Handle(SketcherPrs_SymbolPrs)& theObj, const Handle(AIS_InteractiveContext)& theCtx)
72 :OpenGl_PrimitiveArray(theDriver, theObj->myPntArray->Type(), theObj->myPntArray->Indices(),
73 theObj->myPntArray->Attributes(), theObj->myPntArray->Bounds()), myObj(theObj),
76 virtual void Render(const Handle(OpenGl_Workspace)& theWorkspace) const
78 ModelAPI_Feature* aConstraint = myObj->feature();
79 if (aConstraint->data().get() && aConstraint->data()->isValid()) {
80 Handle(OpenGl_View) aView = theWorkspace->View();
81 double aScale = aView->Camera()->Scale();
82 // Update points coordinate taking the viewer scale into account
83 myObj->updateIfReadyToDisplay(MyDist * aScale, myObj->myIsCustomColor);
86 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
87 Handle(Graphic3d_Buffer) aAttr = myObj->myPntArray->Attributes();
88 myVboAttribs->init(aCtx, 0, aAttr->NbElements,
89 aAttr->Data(), GL_NONE, aAttr->Stride);
93 myAttribs = myObj->myPntArray->Attributes();
94 myIndices = myObj->myPntArray->Indices();
95 myBounds = myObj->myPntArray->Bounds();
98 OpenGl_PrimitiveArray::Render(theWorkspace);
100 // Update selection position only if there is no selected object
101 // because it can corrupt selection of other objects
102 if ((myContext->NbCurrents() == 0) && (myContext->NbSelected() == 0)) {
103 myContext->MainSelector()->RebuildSensitivesTree(myObj);
104 myContext->MainSelector()->RebuildObjectsTree (false);
109 Handle(SketcherPrs_SymbolPrs) myObj;
110 Handle(AIS_InteractiveContext) myContext;
114 //*****************************************************************************
115 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
118 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
121 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
122 ModelAPI_CompositeFeature* theSketcher,
123 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
124 : AIS_InteractiveObject(), myConstraint(theConstraint),
125 myPlane(thePlane), myIsCustomColor(false),
126 mySketcher(theSketcher)
128 SetAutoHilight(Standard_False);
131 //*********************************************************************************
132 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
134 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
135 // Empty memory in position manager
136 aMgr->deleteConstraint(this);
138 Handle(Prs3d_Presentation) aSelPrs =
139 GetSelectPresentation(Handle(PrsMgr_PresentationManager3d)());
140 if (!aSelPrs.IsNull()) {
141 if (!aSelPrs->Groups().IsEmpty()) {
146 Handle(Prs3d_Presentation) aHilightPrs =
147 GetHilightPresentation(Handle(PrsMgr_PresentationManager3d)());
148 if (!aHilightPrs.IsNull()) {
149 if (!aHilightPrs->Groups().IsEmpty()) {
150 aHilightPrs->Clear();
152 aHilightPrs->Erase();
157 #pragma warning( disable : 4996 )
160 //*********************************************************************************
161 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
163 if (myIconsMap.count(iconName()) == 1) {
164 return myIconsMap[iconName()];
166 // Load icon for the presentation
168 char* anEnv = getenv("SHAPER_ROOT_DIR");
170 aFile = std::string(anEnv) +
171 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
173 anEnv = getenv("OPENPARTS_ROOT_DIR");
175 aFile = std::string(anEnv) + FSEP + "resources";
180 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
181 if (aPixMap->Load(aFile.c_str())) {
182 myIconsMap[iconName()] = aPixMap;
185 // The icon for constraint is not found
186 static const char aMsg[] = "Error! constraint images are not found";
188 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
189 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
190 return Handle(Image_AlienPixMap)();
193 //*********************************************************************************
194 void SketcherPrs_SymbolPrs::prepareAspect()
196 // Create an aspect with the icon
197 if (myAspect.IsNull()) {
198 Handle(Image_AlienPixMap) aIcon = icon();
200 myAspect = new Graphic3d_AspectMarker3d();
202 myAspect = new Graphic3d_AspectMarker3d(aIcon);
204 myAspect->SetColor(myCustomColor);
208 //*********************************************************************************
209 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
210 std::string theAttrName) const
212 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
213 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
214 if (aLine.get() == NULL)
216 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
218 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
219 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
220 gp_Pnt aP1 = aPnt1->impl<gp_Pnt>();
221 gp_Pnt aP2 = aPnt2->impl<gp_Pnt>();
223 // Draw line by two points
224 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
225 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
226 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
227 theGroup->AddPrimitiveArray(aLines);
230 //*********************************************************************************
231 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
232 const SelectMgr_SequenceOfOwner& theOwners)
234 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
235 aSelectionPrs->Clear();
236 drawLines(aSelectionPrs, GetContext()->SelectionStyle()->Color());
238 aSelectionPrs->SetDisplayPriority(9);
239 aSelectionPrs->Display();
240 thePM->Color(this, GetContext()->SelectionStyle());
243 //*********************************************************************************
244 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
245 const Handle(PrsMgr_PresentationManager3d)& thePM,
246 const Handle(Graphic3d_HighlightStyle)& theStyle,
247 const Handle(SelectMgr_EntityOwner)& theOwner)
249 thePM->Color(this, theStyle);
251 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
252 aHilightPrs->Clear();
253 drawLines(aHilightPrs, theStyle->Color());
254 aHilightPrs->SetZLayer(Graphic3d_ZLayerId_Topmost);
256 if (thePM->IsImmediateModeOn())
257 thePM->AddToImmediateList(aHilightPrs);
260 //*********************************************************************************
261 void SketcherPrs_SymbolPrs::Compute(
262 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
263 const Handle(Prs3d_Presentation)& thePresentation,
264 const Standard_Integer theMode)
269 Handle(AIS_InteractiveContext) aCtx = GetContext();
270 Handle(OpenGl_GraphicDriver) aDriver =
271 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
272 if (aDriver.IsNull())
275 // Update points with default shift value
276 // it updates array of points if the presentation is ready to display, or the array of points
277 // contains the previous values
279 bool aReadyToDisplay = updateIfReadyToDisplay(20, myIsCustomColor);
281 int aNbVertex = myPntArray->VertexNumber();
282 if (myOwner.IsNull()) {
283 myOwner = new SelectMgr_EntityOwner(this);
286 // Create sensitive point for each symbol
288 for (int i = 1; i <= aNbVertex; i++) {
289 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
290 mySPoints.Append(aSP);
292 myPntArray->SetVertexColor(i, myCustomColor);
295 Handle(OpenGl_Group) aGroup =
296 Handle(OpenGl_Group)::DownCast(Prs3d_Root::CurrentGroup (thePresentation));
297 aGroup->SetPrimitivesAspect(myAspect);
299 // Recompute boundary box of the group
300 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
303 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
304 aVert = myPntArray->Vertice(i);
305 aBnd.Add(Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
308 // Pint the group with custom procedure (see Render)
309 SketcherPrs_SymbolArray* aElem =
310 new SketcherPrs_SymbolArray((OpenGl_GraphicDriver*)aDriver->This(), this, GetContext());
311 aGroup->AddElement(aElem);
313 // Disable frustum culling for this object by marking it as mutable
314 aGroup->Structure()->SetMutable(true);
316 if (!aReadyToDisplay)
317 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
318 "An empty AIS presentation: SketcherPrs_LengthDimension");
322 //*********************************************************************************
323 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
324 const Standard_Integer aMode)
327 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
328 for (int i = 1; i <= mySPoints.Length(); i++)
329 aSelection->Add(mySPoints.Value(i));
333 //*********************************************************************************
334 void SketcherPrs_SymbolPrs::SetCustomColor(const std::vector<int>& theColor)
336 myIsCustomColor = !theColor.empty();
338 myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
339 theColor[2] / 255., Quantity_TOC_RGB);
341 myCustomColor = Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB);
343 if (!myAspect.IsNull())
344 myAspect->SetColor (myCustomColor);
347 //*********************************************************************************
348 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
349 const Handle(Prs3d_Presentation)& thePrs,
350 Quantity_Color theColor) const
352 int aColNam = theColor.Name();
353 //cout<<"### SketcherPrs_SymbolPrs::drawShape "<<theColor.Name()<<endl;
354 Handle(Graphic3d_AspectLine3d) aLineAspect =
355 new Graphic3d_AspectLine3d(theColor, Aspect_TOL_SOLID, 2);
357 Handle(Prs3d_LineAspect) aLinesStyle = myDrawer->LineAspect();
358 Handle(Graphic3d_AspectLine3d) aOldStyle = aLinesStyle->Aspect();
359 aLinesStyle->SetAspect(aLineAspect);
360 myDrawer->SetLineAspect(aLinesStyle);
362 if (theShape->isEdge()) {
364 std::shared_ptr<GeomAPI_Curve> aCurve =
365 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
366 if (aCurve->isLine()) {
369 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
370 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
372 // The shape is circle or arc
374 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
375 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
377 } else if (theShape->isVertex()) {
379 std::shared_ptr<GeomAPI_Vertex> aVertex =
380 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
381 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
382 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
383 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
386 aLinesStyle->SetAspect(aOldStyle);
387 myDrawer->SetLineAspect(aLinesStyle);
390 //*********************************************************************************
391 void SketcherPrs_SymbolPrs::drawListOfShapes(
392 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
393 const Handle(Prs3d_Presentation)& thePrs, Quantity_Color theColor) const
395 int aNb = theListAttr->size();
400 for (i = 0; i < aNb; i++) {
401 aObj = theListAttr->object(i);
402 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
403 if (aShape.get() != NULL)
404 drawShape(aShape, thePrs, theColor);
408 //*********************************************************************************
409 void SketcherPrs_SymbolPrs::BoundingBox(Bnd_Box& theBndBox)
411 Select3D_BndBox3d aTmpBox;
412 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
413 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
414 aTmpBox.Combine (anEnt->BoundingBox());
417 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
418 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());