1 // Copyright (C) 2014-2021 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>
65 #if OCC_VERSION_HEX > 0x070400
66 #include <StdPrs_ToolTriangulatedShape.hxx>
69 //*******************************************************************************************
71 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
75 //********************************************************************
76 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
77 : ViewerData_AISShape(TopoDS_Shape()),
79 myIsSubstituted(false),
81 myAdditionalSelectionPriority(0)
84 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(theResult);
85 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
86 // Workaround for Sketch subshapes which has no discrete representation
87 // until sketch faces are built and displayed.
88 // Thus, perform discretization of such edges.
89 if (theResult->groupName() == ModelAPI_ResultConstruction::group() &&
90 aShape.ShapeType() == TopAbs_EDGE) {
91 GeomEdgePtr anEdgePtr = GeomEdgePtr(new GeomAPI_Edge(aShapePtr));
92 if (anEdgePtr->isCircle() || anEdgePtr->isArc()) {
93 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
95 Handle(Poly_Polygon3D) aPoly3D = BRep_Tool::Polygon3D(anEdge, aLoc);
96 if (aPoly3D.IsNull()) {
97 double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
98 BRepMesh_IncrementalMesh(aShape, aDeflection);
104 // VSV: bos22744: The AutoHilight mode is swithced off because it produces different
105 // behaviour of selection for simple shape and compound. For example when selection mode
106 // is Vertex the shape is selected by vertex, but compound is selected by whole shape
107 //ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
108 //SetAutoHilight(aResOwner.get() == NULL);
110 // Set own free boundaries aspect in order to have free
111 // and unfree boundaries with different colors
112 Handle(Prs3d_Drawer) aDrawer = Attributes();
113 aDrawer->SetUnFreeBoundaryAspect(
114 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
115 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
116 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
118 Quantity_Color aColor;
121 std::vector<int> aIsoValues;
123 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
125 if (aIsoValues.size() == 0) {
126 aIsoValues.push_back(1);
127 aIsoValues.push_back(1);
131 aIsoValues.push_back(0);
132 aIsoValues.push_back(0);
134 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
135 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
136 aDrawer->SetUIsoAspect(myUIsoAspect);
137 aDrawer->SetVIsoAspect(myVIsoAspect);
139 if (aDrawer->HasOwnPointAspect())
140 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
142 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
144 aDrawer = DynamicHilightAttributes();
145 if (aDrawer.IsNull()) {
146 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
147 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
148 aDrawer->SetUIsoAspect(myUIsoAspect);
149 aDrawer->SetVIsoAspect(myVIsoAspect);
150 SetDynamicHilightAttributes(aDrawer);
154 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
155 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
156 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
157 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
158 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
160 ModuleBase_Tools::setPointBallHighlighting(this);
162 // Define colors for wireframe mode
163 setEdgesDefaultColor();
165 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
166 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
167 Attributes()->UpdatePreviousDeviationCoefficient();
170 //********************************************************************
171 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
173 myAdditionalSelectionPriority = thePriority;
176 //********************************************************************
177 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
179 ViewerData_AISShape::SetColor(theColor);
180 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
181 setEdgesDefaultColor();
182 myUIsoAspect->SetColor(theColor);
183 myVIsoAspect->SetColor(theColor);
186 void ModuleBase_ResultPrs::setEdgesDefaultColor()
188 if (myResult.get()) {
189 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
190 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
192 Handle(Prs3d_Drawer) aDrawer = Attributes();
193 aDrawer->SetFaceBoundaryDraw(Standard_True);
194 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
197 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
198 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
199 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
201 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
202 aDrawer->SetFreeBoundaryDraw(Standard_True);
203 aDrawer->SetWireDraw(Standard_True);
209 //********************************************************************
210 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
212 TopoDS_Compound aCompound;
213 BRep_Builder aBBuilder;
214 aBBuilder.MakeCompound (aCompound);
216 myHiddenSubShapes = theShapes;
217 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
218 myVisibleCompound = aCompound;
220 aBBuilder.MakeCompound (aCompound);
221 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
222 for (; aIt.More(); aIt.Next()) {
223 aBBuilder.Add(aCompound, aIt.Value());
225 myHiddenCompound = aCompound;
228 //********************************************************************
229 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
231 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
234 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
235 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
236 for (; aShapeIt.More(); aShapeIt.Next()) {
237 if (theShape.IsSame(aShapeIt.Value()))
244 //********************************************************************
245 bool ModuleBase_ResultPrs::hasSubShapeVisible(
246 const TopoDS_ListOfShape& theShapesToSkip)
248 TopoDS_Compound aCompound;
249 BRep_Builder aBuilder;
250 aBuilder.MakeCompound (aCompound);
251 TopoDS_ListOfShape aShapesToSkip;
252 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
253 aShapesToSkip.Append(aHiddenCopy);
254 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
255 aShapesToSkip.Append(anIt.Value());
257 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
258 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
261 //********************************************************************
262 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
264 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
267 myTransparency = theTransparency;
268 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
272 //********************************************************************
273 void ModuleBase_ResultPrs::Compute(
274 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
275 const Handle(Prs3d_Presentation)& thePresentation,
276 const Standard_Integer theMode)
278 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
279 bool aReadyToDisplay = aShapePtr.get();
280 if (aReadyToDisplay) {
281 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
282 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
283 if (!myOriginalShape.IsNull()) {
284 Set(myOriginalShape);
285 myIsSubstituted = false;
288 else { // convert shape into SHELL
289 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
290 Set(myVisibleCompound);
291 myIsSubstituted = true;
293 aReadyToDisplay = false;
296 // change deviation coefficient to provide more precise circle
298 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
304 // visualize hidden sub-shapes transparent
305 if (myResult.get()) {
306 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
308 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
309 aReadyToDisplay = true;
312 if (!aReadyToDisplay) {
313 Events_InfoMessage("ModuleBase_ResultPrs",
314 "An empty AIS presentation: ModuleBase_ResultPrs").send();
315 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
316 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
321 //********************************************************************
322 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
323 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
324 const TopoDS_ListOfShape& theHiddenSubShapes)
326 switch (theShape.ShapeType()) {
327 case TopAbs_COMPSOLID:
328 case TopAbs_COMPOUND: {
329 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
330 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
335 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
336 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
341 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
342 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
347 if (theHiddenSubShapes.Contains(theShape))
348 return; // remove hidden shape
349 theBuilder.Add(theCompound, theShape);
353 case TopAbs_VERTEX: {
354 theBuilder.Add(theCompound, theShape);
361 //********************************************************************
362 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
363 const Standard_Integer theMode)
365 if (appendVertexSelection(aSelection, theMode))
368 if (theMode > TopAbs_SHAPE) {
369 // In order to avoid using custom selection modes
370 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
371 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
376 // TODO: OCCT issue should be created for the COMPOUND processing
377 // before it is fixed, the next workaround in necessary
378 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
379 const TopoDS_Shape& aShape = Shape();
380 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
381 // do not activate in compound mode shapes which do not contain compounds
382 if (!aCompExp.More())
386 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
387 // // Limit selection area only by actual object (Shape)
388 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
389 // if (aCompSolid.get()) {
390 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
391 // if (aShapePtr.get()) {
392 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
393 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
394 // /// It is important to have priority for the shape of comp solid result less than priority
395 // /// for the presentation shape which is a sub-result.
396 // /// Reason is to select the sub-objects before: #1592
397 // aPriority = aPriority - 1;
398 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
400 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
401 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
402 // aDeflection, myDrawer->HLRAngle(), 9, 500);
404 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
405 // Handle(SelectMgr_EntityOwner) anOwner =
406 // Handle(SelectMgr_EntityOwner)
407 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
408 // anOwner->Set(this);
414 AIS_Shape::ComputeSelection(aSelection, theMode);
416 if (myAdditionalSelectionPriority > 0) {
417 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
418 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
421 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
422 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
423 if (!aBasicsOwner.IsNull())
424 aBasicsOwner->SetPriority(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
429 //********************************************************************
430 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
431 const Standard_Integer /*theMode*/)
433 if (Shape().ShapeType() == TopAbs_VERTEX) {
434 const TopoDS_Shape& aShape = Shape();
436 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
437 #if OCC_VERSION_HEX > 0x070400
438 double aDeflection = StdPrs_ToolTriangulatedShape::GetDeflection(aShape, myDrawer);
440 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
442 /// The cause of this method is the last parameter of BRep owner setting into True.
443 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
444 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
445 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
446 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
447 aDeflection, myDrawer->HLRAngle(), 9, 500);
450 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
451 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
454 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
455 Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
456 anOwner->SetSelectable(this);
464 //********************************************************************
465 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
466 const SelectMgr_SequenceOfOwner& theOwners)
468 Handle(SelectMgr_EntityOwner) anOwner;
469 Handle(ModuleBase_BRepOwner) aCompOwner;
470 for (int i = 1; i <= theOwners.Length(); i++) {
471 anOwner = theOwners.Value(i);
472 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
473 if (aCompOwner.IsNull()) {
474 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
477 TopoDS_Shape aShape = aCompOwner->Shape();
478 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
479 aSelectionPrs->Clear();
481 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
483 aSelectionPrs->SetDisplayPriority(9);
484 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
485 aSelectionPrs->Display();
486 thePM->Color(this, GetContext()->SelectionStyle());
491 //********************************************************************
492 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
493 const Handle(Prs3d_Drawer)& theStyle,
494 const Handle(SelectMgr_EntityOwner)& theOwner)
496 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
500 TopoDS_Shape aShape = aOwner->Shape();
501 if (!aShape.IsNull()) {
502 thePM->Color(this, theStyle);
504 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
505 aHilightPrs->Clear();
507 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
508 aHilightPrs->Highlight(theStyle);
510 if (thePM->IsImmediateModeOn())
511 thePM->AddToImmediateList(aHilightPrs);
516 //********************************************************************
517 void ModuleBase_ResultPrs::updateIsoLines()
519 std::vector<int> aIsoValues;
521 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
523 if (aIsoValues.size() == 0) {
524 aIsoValues.push_back(1);
525 aIsoValues.push_back(1);
529 if (aIsoValues.size() == 0) {
530 aIsoValues.push_back(0);
531 aIsoValues.push_back(0);
538 myUIsoAspect->SetNumber(aIsoValues[0]);
539 myVIsoAspect->SetNumber(aIsoValues[1]);