1 // Copyright (C) 2014-2022 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
20 #include "ModuleBase_ResultPrs.h"
21 #include "ModuleBase_IViewer.h"
23 #include <GeomAPI_PlanarEdges.h>
24 #include <GeomAPI_Edge.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_AttributeIntArray.h>
32 #include "ModuleBase_Tools.h"
33 #include "ModuleBase_BRepOwner.h"
35 #include <Events_InfoMessage.h>
36 #include <Events_Loop.h>
37 #include <Config_PropManager.h>
39 #include <AIS_ColoredDrawer.hxx>
40 #include <AIS_InteractiveContext.hxx>
41 #include <AIS_Selection.hxx>
42 #include <BOPTools_AlgoTools3D.hxx>
43 #include <BRep_Builder.hxx>
44 #include <Graphic3d_AspectMarker3d.hxx>
45 #include <Prs3d_Drawer.hxx>
47 #include <Prs3d_PointAspect.hxx>
48 #include <Prs3d_IsoAspect.hxx>
49 #include <Prs3d_ShadingAspect.hxx>
50 #include <Prs3d_PlaneAspect.hxx>
51 #include <SelectMgr_SequenceOfOwner.hxx>
52 #include <SelectMgr_EntityOwner.hxx>
53 #include <SelectMgr_SelectionManager.hxx>
54 #include <StdPrs_WFShape.hxx>
55 #include <StdPrs_ShadedShape.hxx>
56 #include <StdSelect_BRepSelectionTool.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TopExp_Explorer.hxx>
60 #include <TopoDS_Builder.hxx>
61 #include <TopoDS_Edge.hxx>
62 #include <BRepMesh_IncrementalMesh.hxx>
63 #include <Standard_Version.hxx>
64 #include <Prs3d_Arrow.hxx>
65 #include <GeomAdaptor_Curve.hxx>
67 #include <GCPnts_AbscissaPoint.hxx>
69 #if OCC_VERSION_HEX > 0x070400
70 #include <StdPrs_ToolTriangulatedShape.hxx>
73 //*******************************************************************************************
75 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
79 //********************************************************************
80 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
81 : ViewerData_AISShape(TopoDS_Shape()),
83 myIsSubstituted(false),
85 myAdditionalSelectionPriority(0)
88 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(theResult);
89 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
90 // Workaround for Sketch subshapes which has no discrete representation
91 // until sketch faces are built and displayed.
92 // Thus, perform discretization of such edges.
93 if (theResult->groupName() == ModelAPI_ResultConstruction::group() &&
94 aShape.ShapeType() == TopAbs_EDGE) {
95 GeomEdgePtr anEdgePtr = GeomEdgePtr(new GeomAPI_Edge(aShapePtr));
96 if (anEdgePtr->isCircle() || anEdgePtr->isArc()) {
97 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
99 Handle(Poly_Polygon3D) aPoly3D = BRep_Tool::Polygon3D(anEdge, aLoc);
100 if (aPoly3D.IsNull()) {
101 double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
102 BRepMesh_IncrementalMesh(aShape, aDeflection);
108 // VSV: bos22744: The AutoHilight mode is swithced off because it produces different
109 // behaviour of selection for simple shape and compound. For example when selection mode
110 // is Vertex the shape is selected by vertex, but compound is selected by whole shape
111 //ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
112 //SetAutoHilight(aResOwner.get() == NULL);
114 // Set own free boundaries aspect in order to have free
115 // and unfree boundaries with different colors
116 Handle(Prs3d_Drawer) aDrawer = Attributes();
117 aDrawer->SetUnFreeBoundaryAspect(
118 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
119 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
120 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
122 Quantity_Color aColor;
125 std::vector<int> aIsoValues;
127 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
129 if (aIsoValues.size() == 0) {
130 aIsoValues.push_back(1);
131 aIsoValues.push_back(1);
135 aIsoValues.push_back(0);
136 aIsoValues.push_back(0);
138 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
139 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
140 aDrawer->SetUIsoAspect(myUIsoAspect);
141 aDrawer->SetVIsoAspect(myVIsoAspect);
143 if (aDrawer->HasOwnPointAspect())
144 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
146 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
148 aDrawer = DynamicHilightAttributes();
149 if (aDrawer.IsNull()) {
150 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
151 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
152 aDrawer->SetUIsoAspect(myUIsoAspect);
153 aDrawer->SetVIsoAspect(myVIsoAspect);
154 SetDynamicHilightAttributes(aDrawer);
158 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
159 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
160 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
161 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
162 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
164 ModuleBase_Tools::setPointBallHighlighting(this);
166 // Define colors for wireframe mode
167 setEdgesDefaultColor();
169 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
170 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
171 Attributes()->UpdatePreviousDeviationCoefficient();
174 //********************************************************************
175 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
177 myAdditionalSelectionPriority = thePriority;
180 //********************************************************************
181 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
183 ViewerData_AISShape::SetColor(theColor);
184 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
185 setEdgesDefaultColor();
186 myUIsoAspect->SetColor(theColor);
187 myVIsoAspect->SetColor(theColor);
190 void ModuleBase_ResultPrs::setEdgesDefaultColor()
192 if (myResult.get()) {
193 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
194 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
196 Handle(Prs3d_Drawer) aDrawer = Attributes();
197 aDrawer->SetFaceBoundaryDraw(Standard_True);
198 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
201 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
202 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
203 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
205 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
206 aDrawer->SetFreeBoundaryDraw(Standard_True);
207 aDrawer->SetWireDraw(Standard_True);
213 //********************************************************************
214 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
216 TopoDS_Compound aCompound;
217 BRep_Builder aBBuilder;
218 aBBuilder.MakeCompound (aCompound);
220 myHiddenSubShapes = theShapes;
221 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
222 myVisibleCompound = aCompound;
224 aBBuilder.MakeCompound (aCompound);
225 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
226 for (; aIt.More(); aIt.Next()) {
227 aBBuilder.Add(aCompound, aIt.Value());
229 myHiddenCompound = aCompound;
232 //********************************************************************
233 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
235 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
238 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
239 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
240 for (; aShapeIt.More(); aShapeIt.Next()) {
241 if (theShape.IsSame(aShapeIt.Value()))
248 //********************************************************************
249 bool ModuleBase_ResultPrs::hasSubShapeVisible(
250 const TopoDS_ListOfShape& theShapesToSkip)
252 TopoDS_Compound aCompound;
253 BRep_Builder aBuilder;
254 aBuilder.MakeCompound (aCompound);
255 TopoDS_ListOfShape aShapesToSkip;
256 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
257 aShapesToSkip.Append(aHiddenCopy);
258 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
259 aShapesToSkip.Append(anIt.Value());
261 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
262 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
265 //********************************************************************
266 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
268 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
271 myTransparency = theTransparency;
272 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
276 //********************************************************************
277 void ModuleBase_ResultPrs::Compute(
278 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
279 const Handle(Prs3d_Presentation)& thePresentation,
280 const Standard_Integer theMode)
282 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
283 bool aReadyToDisplay = aShapePtr.get();
284 if (aReadyToDisplay) {
285 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
286 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
287 if (!myOriginalShape.IsNull()) {
288 Set(myOriginalShape);
289 myIsSubstituted = false;
292 else { // convert shape into SHELL
293 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
294 Set(myVisibleCompound);
295 myIsSubstituted = true;
297 aReadyToDisplay = false;
300 // change deviation coefficient to provide more precise circle
302 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
307 if (myResult.get() && ModelAPI_Tools::isShowEdgesDirection(myResult))
309 TopExp_Explorer Exp(myshape, TopAbs_EDGE);
310 for (; Exp.More(); Exp.Next()) {
311 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
312 if (anEdgeE.IsNull())
315 // draw curve direction (issue 0021087)
316 anEdgeE.Orientation(TopAbs_FORWARD);
318 TopoDS_Vertex aV1, aV2;
319 TopExp::Vertices(anEdgeE, aV1, aV2);
320 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
321 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
325 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE, fp, lp);
327 if (C.IsNull()) continue;
329 if (anEdgeE.Orientation() == TopAbs_FORWARD)
330 C->D1(lp, aP2, aDirVec);
332 C->D1(fp, aP1, aDirVec);
335 GeomAdaptor_Curve aAdC;
336 aAdC.Load(C, fp, lp);
337 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
339 if (aDist > gp::Resolution()) {
341 if (anEdgeE.Orientation() == TopAbs_FORWARD)
346 Prs3d_Arrow::Draw(thePresentation->CurrentGroup(), aP2, aDir, M_PI / 180.*5., aDist / 10.);
351 // visualize hidden sub-shapes transparent
352 if (myResult.get()) {
353 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
355 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
356 aReadyToDisplay = true;
359 if (!aReadyToDisplay) {
360 Events_InfoMessage("ModuleBase_ResultPrs",
361 "An empty AIS presentation: ModuleBase_ResultPrs").send();
362 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
363 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
368 //********************************************************************
369 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
370 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
371 const TopoDS_ListOfShape& theHiddenSubShapes)
373 switch (theShape.ShapeType()) {
374 case TopAbs_COMPSOLID:
375 case TopAbs_COMPOUND: {
376 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
377 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
382 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
383 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
388 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
389 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
394 if (theHiddenSubShapes.Contains(theShape))
395 return; // remove hidden shape
396 theBuilder.Add(theCompound, theShape);
400 case TopAbs_VERTEX: {
401 theBuilder.Add(theCompound, theShape);
408 //********************************************************************
409 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
410 const Standard_Integer theMode)
412 if (appendVertexSelection(aSelection, theMode))
415 if (theMode > TopAbs_SHAPE) {
416 // In order to avoid using custom selection modes
417 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
418 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
423 // TODO: OCCT issue should be created for the COMPOUND processing
424 // before it is fixed, the next workaround in necessary
425 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
426 const TopoDS_Shape& aShape = Shape();
427 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
428 // do not activate in compound mode shapes which do not contain compounds
429 if (!aCompExp.More())
433 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
434 // // Limit selection area only by actual object (Shape)
435 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
436 // if (aCompSolid.get()) {
437 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
438 // if (aShapePtr.get()) {
439 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
440 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
441 // /// It is important to have priority for the shape of comp solid result less than priority
442 // /// for the presentation shape which is a sub-result.
443 // /// Reason is to select the sub-objects before: #1592
444 // aPriority = aPriority - 1;
445 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
447 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
448 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
449 // aDeflection, myDrawer->HLRAngle(), 9, 500);
451 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
452 // Handle(SelectMgr_EntityOwner) anOwner =
453 // Handle(SelectMgr_EntityOwner)
454 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
455 // anOwner->Set(this);
461 AIS_Shape::ComputeSelection(aSelection, theMode);
463 if (myAdditionalSelectionPriority > 0) {
464 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
465 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
468 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
469 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
470 if (!aBasicsOwner.IsNull())
471 aBasicsOwner->SetPriority(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
476 //********************************************************************
477 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
478 const Standard_Integer /*theMode*/)
480 if (Shape().ShapeType() == TopAbs_VERTEX) {
481 const TopoDS_Shape& aShape = Shape();
483 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
484 #if OCC_VERSION_HEX > 0x070400
485 double aDeflection = StdPrs_ToolTriangulatedShape::GetDeflection(aShape, myDrawer);
487 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
489 /// The cause of this method is the last parameter of BRep owner setting into True.
490 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
491 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
492 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
493 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
494 aDeflection, myDrawer->DeviationAngle(), 9, 500);
497 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
498 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
501 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
502 Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
503 anOwner->SetSelectable(this);
511 //********************************************************************
512 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
513 const SelectMgr_SequenceOfOwner& theOwners)
515 Handle(SelectMgr_EntityOwner) anOwner;
516 Handle(ModuleBase_BRepOwner) aCompOwner;
517 for (int i = 1; i <= theOwners.Length(); i++) {
518 anOwner = theOwners.Value(i);
519 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
520 if (aCompOwner.IsNull()) {
521 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
524 TopoDS_Shape aShape = aCompOwner->Shape();
525 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
526 aSelectionPrs->Clear();
528 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
530 aSelectionPrs->SetDisplayPriority(9);
531 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
532 aSelectionPrs->Display();
533 thePM->Color(this, GetContext()->SelectionStyle());
538 //********************************************************************
539 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
540 const Handle(Prs3d_Drawer)& theStyle,
541 const Handle(SelectMgr_EntityOwner)& theOwner)
543 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
547 TopoDS_Shape aShape = aOwner->Shape();
548 if (!aShape.IsNull()) {
549 thePM->Color(this, theStyle);
551 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
552 aHilightPrs->Clear();
554 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
555 aHilightPrs->Highlight(theStyle);
557 if (thePM->IsImmediateModeOn())
558 thePM->AddToImmediateList(aHilightPrs);
563 //********************************************************************
564 void ModuleBase_ResultPrs::updateIsoLines()
566 std::vector<int> aIsoValues;
568 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
570 if (aIsoValues.size() == 0) {
571 aIsoValues.push_back(1);
572 aIsoValues.push_back(1);
576 if (aIsoValues.size() == 0) {
577 aIsoValues.push_back(0);
578 aIsoValues.push_back(0);
585 myUIsoAspect->SetNumber(aIsoValues[0]);
586 myVIsoAspect->SetNumber(aIsoValues[1]);