1 // Copyright (C) 2014-2019 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()), myResult(theResult), myAdditionalSelectionPriority(0),
73 myTransparency(1), myIsSubstituted(false)
76 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(theResult);
77 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
78 // Workaround for Sketch subshapes which has no discrete representation
79 // until sketch faces are built and displayed.
80 // Thus, perform discretization of such edges.
81 if (theResult->groupName() == ModelAPI_ResultConstruction::group() &&
82 aShape.ShapeType() == TopAbs_EDGE) {
83 GeomEdgePtr anEdgePtr = GeomEdgePtr(new GeomAPI_Edge(aShapePtr));
84 if (anEdgePtr->isCircle() || anEdgePtr->isArc()) {
85 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
87 Handle(Poly_Polygon3D) aPoly3D = BRep_Tool::Polygon3D(anEdge, aLoc);
88 if (aPoly3D.IsNull()) {
89 double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
90 BRepMesh_IncrementalMesh(aShape, aDeflection);
96 // Activate individual repaintng if this is a part of compsolid
97 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
98 SetAutoHilight(aResOwner.get() == NULL);
100 // Set own free boundaries aspect in order to have free
101 // and unfree boundaries with different colors
102 Handle(Prs3d_Drawer) aDrawer = Attributes();
103 aDrawer->SetUnFreeBoundaryAspect(
104 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
105 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
106 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
108 Quantity_Color aColor;
111 std::vector<int> aIsoValues;
113 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
115 if (aIsoValues.size() == 0) {
116 aIsoValues.push_back(1);
117 aIsoValues.push_back(1);
121 aIsoValues.push_back(0);
122 aIsoValues.push_back(0);
124 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
125 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
126 aDrawer->SetUIsoAspect(myUIsoAspect);
127 aDrawer->SetVIsoAspect(myVIsoAspect);
129 if (aDrawer->HasOwnPointAspect())
130 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
132 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
134 aDrawer = DynamicHilightAttributes();
135 if (aDrawer.IsNull()) {
136 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
137 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
138 aDrawer->SetUIsoAspect(myUIsoAspect);
139 aDrawer->SetVIsoAspect(myVIsoAspect);
140 SetDynamicHilightAttributes(aDrawer);
144 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
145 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
146 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
147 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
148 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
150 ModuleBase_Tools::setPointBallHighlighting(this);
152 // Define colors for wireframe mode
153 setEdgesDefaultColor();
155 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
156 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
157 Attributes()->UpdatePreviousDeviationCoefficient();
160 //********************************************************************
161 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
163 myAdditionalSelectionPriority = thePriority;
166 //********************************************************************
167 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
169 ViewerData_AISShape::SetColor(theColor);
170 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
171 setEdgesDefaultColor();
172 myUIsoAspect->SetColor(theColor);
173 myVIsoAspect->SetColor(theColor);
176 void ModuleBase_ResultPrs::setEdgesDefaultColor()
178 if (myResult.get()) {
179 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
180 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
182 Handle(Prs3d_Drawer) aDrawer = Attributes();
183 aDrawer->SetFaceBoundaryDraw(Standard_True);
184 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
187 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
188 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
189 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
191 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
192 aDrawer->SetFreeBoundaryDraw(Standard_True);
193 aDrawer->SetWireDraw(Standard_True);
199 //********************************************************************
200 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
202 bool isModified = false;
204 TopoDS_Compound aCompound;
205 BRep_Builder aBBuilder;
206 aBBuilder.MakeCompound (aCompound);
208 myHiddenSubShapes = theShapes;
209 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
210 myVisibleCompound = aCompound;
212 aBBuilder.MakeCompound (aCompound);
213 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
214 for (; aIt.More(); aIt.Next()) {
215 aBBuilder.Add(aCompound, aIt.Value());
217 myHiddenCompound = aCompound;
220 //********************************************************************
221 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
223 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
226 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
227 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
228 for (; aShapeIt.More(); aShapeIt.Next()) {
229 if (theShape.IsSame(aShapeIt.Value()))
236 //********************************************************************
237 bool ModuleBase_ResultPrs::hasSubShapeVisible(
238 const TopoDS_ListOfShape& theShapesToSkip)
240 TopoDS_Compound aCompound;
241 BRep_Builder aBuilder;
242 aBuilder.MakeCompound (aCompound);
243 TopoDS_ListOfShape aShapesToSkip;
244 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
245 aShapesToSkip.Append(aHiddenCopy);
246 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
247 aShapesToSkip.Append(anIt.Value());
249 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
250 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
253 //********************************************************************
254 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
256 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
259 myTransparency = theTransparency;
260 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
264 //********************************************************************
265 void ModuleBase_ResultPrs::Compute(
266 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
267 const Handle(Prs3d_Presentation)& thePresentation,
268 const Standard_Integer theMode)
270 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
271 bool aReadyToDisplay = aShapePtr.get();
272 if (aReadyToDisplay) {
273 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
274 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
275 if (!myOriginalShape.IsNull()) {
276 Set(myOriginalShape);
277 myIsSubstituted = false;
280 else { // convert shape into SHELL
281 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
282 Set(myVisibleCompound);
283 myIsSubstituted = true;
285 aReadyToDisplay = false;
288 // change deviation coefficient to provide more precise circle
290 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
296 // visualize hidden sub-shapes transparent
297 if (myResult.get()) {
298 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
300 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
301 aReadyToDisplay = true;
304 if (!aReadyToDisplay) {
305 Events_InfoMessage("ModuleBase_ResultPrs",
306 "An empty AIS presentation: ModuleBase_ResultPrs").send();
307 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
308 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
313 //********************************************************************
314 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
315 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
316 const TopoDS_ListOfShape& theHiddenSubShapes)
318 switch (theShape.ShapeType()) {
319 case TopAbs_COMPSOLID:
320 case TopAbs_COMPOUND: {
321 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
322 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
327 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
328 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
333 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
334 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
339 if (theHiddenSubShapes.Contains(theShape))
340 return; // remove hidden shape
341 theBuilder.Add(theCompound, theShape);
345 case TopAbs_VERTEX: {
346 theBuilder.Add(theCompound, theShape);
353 //********************************************************************
354 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
355 const Standard_Integer theMode)
357 if (appendVertexSelection(aSelection, theMode))
360 if (theMode > TopAbs_SHAPE) {
361 // In order to avoid using custom selection modes
362 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
363 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
368 // TODO: OCCT issue should be created for the COMPOUND processing
369 // before it is fixed, the next workaround in necessary
370 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
371 const TopoDS_Shape& aShape = Shape();
372 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
373 // do not activate in compound mode shapes which do not contain compounds
374 if (!aCompExp.More())
378 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
379 // // Limit selection area only by actual object (Shape)
380 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
381 // if (aCompSolid.get()) {
382 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
383 // if (aShapePtr.get()) {
384 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
385 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
386 // /// It is important to have priority for the shape of comp solid result less than priority
387 // /// for the presentation shape which is a sub-result.
388 // /// Reason is to select the sub-objects before: #1592
389 // aPriority = aPriority - 1;
390 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
392 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
393 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
394 // aDeflection, myDrawer->HLRAngle(), 9, 500);
396 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
397 // Handle(SelectMgr_EntityOwner) anOwner =
398 // Handle(SelectMgr_EntityOwner)
399 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
400 // anOwner->Set(this);
406 AIS_Shape::ComputeSelection(aSelection, theMode);
408 if (myAdditionalSelectionPriority > 0) {
409 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
410 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
413 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
414 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
415 if (!aBasicsOwner.IsNull())
416 aBasicsOwner->SetPriority(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
421 //********************************************************************
422 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
423 const Standard_Integer theMode)
425 if (Shape().ShapeType() == TopAbs_VERTEX) {
426 const TopoDS_Shape& aShape = Shape();
428 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
429 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
431 /// The cause of this method is the last parameter of BRep owner setting into True.
432 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
433 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
434 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
435 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
436 aDeflection, myDrawer->HLRAngle(), 9, 500);
439 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
440 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
443 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
444 Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
445 anOwner->SetSelectable(this);
453 //********************************************************************
454 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
455 const SelectMgr_SequenceOfOwner& theOwners)
457 Handle(SelectMgr_EntityOwner) anOwner;
458 Handle(ModuleBase_BRepOwner) aCompOwner;
459 for (int i = 1; i <= theOwners.Length(); i++) {
460 anOwner = theOwners.Value(i);
461 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
462 if (aCompOwner.IsNull()) {
463 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
466 TopoDS_Shape aShape = aCompOwner->Shape();
467 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
468 aSelectionPrs->Clear();
470 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
472 aSelectionPrs->SetDisplayPriority(9);
473 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
474 aSelectionPrs->Display();
475 thePM->Color(this, GetContext()->SelectionStyle());
480 //********************************************************************
481 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
482 const Handle(Prs3d_Drawer)& theStyle,
483 const Handle(SelectMgr_EntityOwner)& theOwner)
485 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
489 TopoDS_Shape aShape = aOwner->Shape();
490 if (!aShape.IsNull()) {
491 thePM->Color(this, theStyle);
493 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
494 aHilightPrs->Clear();
496 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
497 aHilightPrs->Highlight(theStyle);
499 if (thePM->IsImmediateModeOn())
500 thePM->AddToImmediateList(aHilightPrs);
505 //********************************************************************
506 void ModuleBase_ResultPrs::updateIsoLines()
508 std::vector<int> aIsoValues;
510 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
512 if (aIsoValues.size() == 0) {
513 aIsoValues.push_back(1);
514 aIsoValues.push_back(1);
518 if (aIsoValues.size() == 0) {
519 aIsoValues.push_back(0);
520 aIsoValues.push_back(0);
527 myUIsoAspect->SetNumber(aIsoValues[0]);
528 myVIsoAspect->SetNumber(aIsoValues[1]);