1 // Copyright (C) 2014-2020 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>
64 //*******************************************************************************************
66 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
70 //********************************************************************
71 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
72 : ViewerData_AISShape(TopoDS_Shape()),
74 myIsSubstituted(false),
76 myAdditionalSelectionPriority(0)
79 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(theResult);
80 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
81 // Workaround for Sketch subshapes which has no discrete representation
82 // until sketch faces are built and displayed.
83 // Thus, perform discretization of such edges.
84 if (theResult->groupName() == ModelAPI_ResultConstruction::group() &&
85 aShape.ShapeType() == TopAbs_EDGE) {
86 GeomEdgePtr anEdgePtr = GeomEdgePtr(new GeomAPI_Edge(aShapePtr));
87 if (anEdgePtr->isCircle() || anEdgePtr->isArc()) {
88 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
90 Handle(Poly_Polygon3D) aPoly3D = BRep_Tool::Polygon3D(anEdge, aLoc);
91 if (aPoly3D.IsNull()) {
92 double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
93 BRepMesh_IncrementalMesh(aShape, aDeflection);
99 // Activate individual repaintng if this is a part of compsolid
100 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
101 SetAutoHilight(aResOwner.get() == NULL);
103 // Set own free boundaries aspect in order to have free
104 // and unfree boundaries with different colors
105 Handle(Prs3d_Drawer) aDrawer = Attributes();
106 aDrawer->SetUnFreeBoundaryAspect(
107 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
108 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
109 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
111 Quantity_Color aColor;
114 std::vector<int> aIsoValues;
116 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
118 if (aIsoValues.size() == 0) {
119 aIsoValues.push_back(1);
120 aIsoValues.push_back(1);
124 aIsoValues.push_back(0);
125 aIsoValues.push_back(0);
127 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
128 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
129 aDrawer->SetUIsoAspect(myUIsoAspect);
130 aDrawer->SetVIsoAspect(myVIsoAspect);
132 if (aDrawer->HasOwnPointAspect())
133 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
135 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
137 aDrawer = DynamicHilightAttributes();
138 if (aDrawer.IsNull()) {
139 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
140 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
141 aDrawer->SetUIsoAspect(myUIsoAspect);
142 aDrawer->SetVIsoAspect(myVIsoAspect);
143 SetDynamicHilightAttributes(aDrawer);
147 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
148 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
149 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
150 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
151 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
153 ModuleBase_Tools::setPointBallHighlighting(this);
155 // Define colors for wireframe mode
156 setEdgesDefaultColor();
158 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
159 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
160 Attributes()->UpdatePreviousDeviationCoefficient();
163 //********************************************************************
164 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
166 myAdditionalSelectionPriority = thePriority;
169 //********************************************************************
170 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
172 ViewerData_AISShape::SetColor(theColor);
173 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
174 setEdgesDefaultColor();
175 myUIsoAspect->SetColor(theColor);
176 myVIsoAspect->SetColor(theColor);
179 void ModuleBase_ResultPrs::setEdgesDefaultColor()
181 if (myResult.get()) {
182 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
183 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
185 Handle(Prs3d_Drawer) aDrawer = Attributes();
186 aDrawer->SetFaceBoundaryDraw(Standard_True);
187 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
190 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
191 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
192 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
194 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
195 aDrawer->SetFreeBoundaryDraw(Standard_True);
196 aDrawer->SetWireDraw(Standard_True);
202 //********************************************************************
203 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
205 TopoDS_Compound aCompound;
206 BRep_Builder aBBuilder;
207 aBBuilder.MakeCompound (aCompound);
209 myHiddenSubShapes = theShapes;
210 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
211 myVisibleCompound = aCompound;
213 aBBuilder.MakeCompound (aCompound);
214 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
215 for (; aIt.More(); aIt.Next()) {
216 aBBuilder.Add(aCompound, aIt.Value());
218 myHiddenCompound = aCompound;
221 //********************************************************************
222 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
224 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
227 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
228 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
229 for (; aShapeIt.More(); aShapeIt.Next()) {
230 if (theShape.IsSame(aShapeIt.Value()))
237 //********************************************************************
238 bool ModuleBase_ResultPrs::hasSubShapeVisible(
239 const TopoDS_ListOfShape& theShapesToSkip)
241 TopoDS_Compound aCompound;
242 BRep_Builder aBuilder;
243 aBuilder.MakeCompound (aCompound);
244 TopoDS_ListOfShape aShapesToSkip;
245 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
246 aShapesToSkip.Append(aHiddenCopy);
247 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
248 aShapesToSkip.Append(anIt.Value());
250 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
251 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
254 //********************************************************************
255 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
257 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
260 myTransparency = theTransparency;
261 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
265 //********************************************************************
266 void ModuleBase_ResultPrs::Compute(
267 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
268 const Handle(Prs3d_Presentation)& thePresentation,
269 const Standard_Integer theMode)
271 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
272 bool aReadyToDisplay = aShapePtr.get();
273 if (aReadyToDisplay) {
274 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
275 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
276 if (!myOriginalShape.IsNull()) {
277 Set(myOriginalShape);
278 myIsSubstituted = false;
281 else { // convert shape into SHELL
282 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
283 Set(myVisibleCompound);
284 myIsSubstituted = true;
286 aReadyToDisplay = false;
289 // change deviation coefficient to provide more precise circle
291 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
297 // visualize hidden sub-shapes transparent
298 if (myResult.get()) {
299 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
301 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
302 aReadyToDisplay = true;
305 if (!aReadyToDisplay) {
306 Events_InfoMessage("ModuleBase_ResultPrs",
307 "An empty AIS presentation: ModuleBase_ResultPrs").send();
308 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
309 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
314 //********************************************************************
315 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
316 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
317 const TopoDS_ListOfShape& theHiddenSubShapes)
319 switch (theShape.ShapeType()) {
320 case TopAbs_COMPSOLID:
321 case TopAbs_COMPOUND: {
322 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
323 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
328 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
329 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
334 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
335 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
340 if (theHiddenSubShapes.Contains(theShape))
341 return; // remove hidden shape
342 theBuilder.Add(theCompound, theShape);
346 case TopAbs_VERTEX: {
347 theBuilder.Add(theCompound, theShape);
354 //********************************************************************
355 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
356 const Standard_Integer theMode)
358 if (appendVertexSelection(aSelection, theMode))
361 if (theMode > TopAbs_SHAPE) {
362 // In order to avoid using custom selection modes
363 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
364 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
369 // TODO: OCCT issue should be created for the COMPOUND processing
370 // before it is fixed, the next workaround in necessary
371 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
372 const TopoDS_Shape& aShape = Shape();
373 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
374 // do not activate in compound mode shapes which do not contain compounds
375 if (!aCompExp.More())
379 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
380 // // Limit selection area only by actual object (Shape)
381 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
382 // if (aCompSolid.get()) {
383 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
384 // if (aShapePtr.get()) {
385 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
386 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
387 // /// It is important to have priority for the shape of comp solid result less than priority
388 // /// for the presentation shape which is a sub-result.
389 // /// Reason is to select the sub-objects before: #1592
390 // aPriority = aPriority - 1;
391 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
393 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
394 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
395 // aDeflection, myDrawer->HLRAngle(), 9, 500);
397 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
398 // Handle(SelectMgr_EntityOwner) anOwner =
399 // Handle(SelectMgr_EntityOwner)
400 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
401 // anOwner->Set(this);
407 AIS_Shape::ComputeSelection(aSelection, theMode);
409 if (myAdditionalSelectionPriority > 0) {
410 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
411 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
414 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
415 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
416 if (!aBasicsOwner.IsNull())
417 aBasicsOwner->SetPriority(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
422 //********************************************************************
423 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
424 const Standard_Integer /*theMode*/)
426 if (Shape().ShapeType() == TopAbs_VERTEX) {
427 const TopoDS_Shape& aShape = Shape();
429 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
430 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
432 /// The cause of this method is the last parameter of BRep owner setting into True.
433 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
434 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
435 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
436 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
437 aDeflection, myDrawer->HLRAngle(), 9, 500);
440 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
441 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
444 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
445 Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
446 anOwner->SetSelectable(this);
454 //********************************************************************
455 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
456 const SelectMgr_SequenceOfOwner& theOwners)
458 Handle(SelectMgr_EntityOwner) anOwner;
459 Handle(ModuleBase_BRepOwner) aCompOwner;
460 for (int i = 1; i <= theOwners.Length(); i++) {
461 anOwner = theOwners.Value(i);
462 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
463 if (aCompOwner.IsNull()) {
464 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
467 TopoDS_Shape aShape = aCompOwner->Shape();
468 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
469 aSelectionPrs->Clear();
471 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
473 aSelectionPrs->SetDisplayPriority(9);
474 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
475 aSelectionPrs->Display();
476 thePM->Color(this, GetContext()->SelectionStyle());
481 //********************************************************************
482 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
483 const Handle(Prs3d_Drawer)& theStyle,
484 const Handle(SelectMgr_EntityOwner)& theOwner)
486 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
490 TopoDS_Shape aShape = aOwner->Shape();
491 if (!aShape.IsNull()) {
492 thePM->Color(this, theStyle);
494 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
495 aHilightPrs->Clear();
497 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
498 aHilightPrs->Highlight(theStyle);
500 if (thePM->IsImmediateModeOn())
501 thePM->AddToImmediateList(aHilightPrs);
506 //********************************************************************
507 void ModuleBase_ResultPrs::updateIsoLines()
509 std::vector<int> aIsoValues;
511 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
513 if (aIsoValues.size() == 0) {
514 aIsoValues.push_back(1);
515 aIsoValues.push_back(1);
519 if (aIsoValues.size() == 0) {
520 aIsoValues.push_back(0);
521 aIsoValues.push_back(0);
528 myUIsoAspect->SetNumber(aIsoValues[0]);
529 myVIsoAspect->SetNumber(aIsoValues[1]);