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>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_AttributeIntArray.h>
31 #include "ModuleBase_Tools.h"
32 #include "ModuleBase_BRepOwner.h"
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
37 #include <AIS_ColoredDrawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
39 #include <AIS_Selection.hxx>
40 #include <BOPTools_AlgoTools3D.hxx>
41 #include <BRep_Builder.hxx>
42 #include <Graphic3d_AspectMarker3d.hxx>
43 #include <Prs3d_Drawer.hxx>
45 #include <Prs3d_PointAspect.hxx>
46 #include <Prs3d_IsoAspect.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <Prs3d_PlaneAspect.hxx>
49 #include <SelectMgr_SequenceOfOwner.hxx>
50 #include <SelectMgr_EntityOwner.hxx>
51 #include <SelectMgr_SelectionManager.hxx>
52 #include <StdPrs_WFShape.hxx>
53 #include <StdPrs_ShadedShape.hxx>
54 #include <StdSelect_BRepSelectionTool.hxx>
55 #include <TColStd_ListIteratorOfListOfInteger.hxx>
56 #include <TopExp_Explorer.hxx>
58 #include <TopoDS_Builder.hxx>
60 //*******************************************************************************************
62 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
66 //********************************************************************
67 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
68 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
69 myTransparency(1), myIsSubstituted(false)
71 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
72 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
75 // Activate individual repaintng if this is a part of compsolid
76 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
77 SetAutoHilight(aResOwner.get() == NULL);
79 // Set own free boundaries aspect in order to have free
80 // and unfree boundaries with different colors
81 Handle(Prs3d_Drawer) aDrawer = Attributes();
82 //aDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
83 aDrawer->SetUnFreeBoundaryAspect(
84 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
85 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
86 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
88 Quantity_Color aColor;
91 std::vector<int> aIsoValues;
93 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
95 if (aIsoValues.size() == 0) {
96 aIsoValues.push_back(1);
97 aIsoValues.push_back(1);
101 aIsoValues.push_back(0);
102 aIsoValues.push_back(0);
104 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
105 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
106 aDrawer->SetUIsoAspect(myUIsoAspect);
107 aDrawer->SetVIsoAspect(myVIsoAspect);
109 if (aDrawer->HasOwnPointAspect())
110 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
112 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
114 aDrawer = DynamicHilightAttributes();
115 if (aDrawer.IsNull()) {
116 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
117 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
118 aDrawer->SetUIsoAspect(myUIsoAspect);
119 aDrawer->SetVIsoAspect(myVIsoAspect);
120 SetDynamicHilightAttributes(aDrawer);
123 //if (!aDrawer.IsNull())
124 // aDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
126 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
127 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
128 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
129 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
130 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
132 ModuleBase_Tools::setPointBallHighlighting(this);
134 // Define colors for wireframe mode
135 setEdgesDefaultColor();
138 //********************************************************************
139 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
141 myAdditionalSelectionPriority = thePriority;
144 //********************************************************************
145 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
147 ViewerData_AISShape::SetColor(theColor);
148 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
149 setEdgesDefaultColor();
150 myUIsoAspect->SetColor(theColor);
151 myVIsoAspect->SetColor(theColor);
154 void ModuleBase_ResultPrs::setEdgesDefaultColor()
156 if (myResult.get()) {
157 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
158 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
160 Handle(Prs3d_Drawer) aDrawer = Attributes();
161 aDrawer->SetFaceBoundaryDraw(Standard_True);
162 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
165 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
166 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
167 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
169 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
170 aDrawer->SetFreeBoundaryDraw(Standard_True);
171 aDrawer->SetWireDraw(Standard_True);
177 //********************************************************************
178 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
180 bool isModified = false;
182 TopoDS_Compound aCompound;
183 BRep_Builder aBBuilder;
184 aBBuilder.MakeCompound (aCompound);
186 myHiddenSubShapes = theShapes;
187 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
188 myVisibleCompound = aCompound;
190 aBBuilder.MakeCompound (aCompound);
191 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
192 for (; aIt.More(); aIt.Next()) {
193 aBBuilder.Add(aCompound, aIt.Value());
195 myHiddenCompound = aCompound;
198 //********************************************************************
199 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
201 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
204 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
205 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
206 for (; aShapeIt.More(); aShapeIt.Next()) {
207 if (theShape.IsSame(aShapeIt.Value()))
214 //********************************************************************
215 bool ModuleBase_ResultPrs::hasSubShapeVisible(
216 const TopoDS_ListOfShape& theShapesToSkip)
218 TopoDS_Compound aCompound;
219 BRep_Builder aBuilder;
220 aBuilder.MakeCompound (aCompound);
221 TopoDS_ListOfShape aShapesToSkip;
222 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
223 aShapesToSkip.Append(aHiddenCopy);
224 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
225 aShapesToSkip.Append(anIt.Value());
227 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
228 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
231 //********************************************************************
232 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
234 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
237 myTransparency = theTransparency;
238 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
242 //********************************************************************
243 void ModuleBase_ResultPrs::Compute(
244 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
245 const Handle(Prs3d_Presentation)& thePresentation,
246 const Standard_Integer theMode)
248 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
249 bool aReadyToDisplay = aShapePtr.get();
250 if (aReadyToDisplay) {
251 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
252 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
253 if (!myOriginalShape.IsNull()) {
254 Set(myOriginalShape);
255 myIsSubstituted = false;
258 else { // convert shape into SHELL
259 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
260 Set(myVisibleCompound);
261 myIsSubstituted = true;
263 aReadyToDisplay = false;
266 // change deviation coefficient to provide more precise circle
267 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
268 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
270 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
276 // visualize hidden sub-shapes transparent
277 if (myResult.get()) {
278 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
280 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
281 aReadyToDisplay = true;
284 if (!aReadyToDisplay) {
285 Events_InfoMessage("ModuleBase_ResultPrs",
286 "An empty AIS presentation: ModuleBase_ResultPrs").send();
287 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
288 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
293 //********************************************************************
294 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
295 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
296 const TopoDS_ListOfShape& theHiddenSubShapes)
298 switch (theShape.ShapeType()) {
299 case TopAbs_COMPSOLID:
300 case TopAbs_COMPOUND: {
301 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
302 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
307 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
308 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
313 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
314 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
319 if (theHiddenSubShapes.Contains(theShape))
320 return; // remove hidden shape
321 theBuilder.Add(theCompound, theShape);
325 case TopAbs_VERTEX: {
326 theBuilder.Add(theCompound, theShape);
333 //********************************************************************
334 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
335 const Standard_Integer theMode)
337 if (appendVertexSelection(aSelection, theMode))
340 if (theMode > TopAbs_SHAPE) {
341 // In order to avoid using custom selection modes
342 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
343 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
348 // TODO: OCCT issue should be created for the COMPOUND processing
349 // before it is fixed, the next workaround in necessary
350 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
351 const TopoDS_Shape& aShape = Shape();
352 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
353 // do not activate in compound mode shapes which do not contain compounds
354 if (!aCompExp.More())
358 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
359 // // Limit selection area only by actual object (Shape)
360 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
361 // if (aCompSolid.get()) {
362 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
363 // if (aShapePtr.get()) {
364 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
365 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
366 // /// It is important to have priority for the shape of comp solid result less than priority
367 // /// for the presentation shape which is a sub-result.
368 // /// Reason is to select the sub-objects before: #1592
369 // aPriority = aPriority - 1;
370 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
372 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
373 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
374 // aDeflection, myDrawer->HLRAngle(), 9, 500);
376 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
377 // Handle(SelectMgr_EntityOwner) anOwner =
378 // Handle(SelectMgr_EntityOwner)
379 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
380 // anOwner->Set(this);
386 AIS_Shape::ComputeSelection(aSelection, theMode);
388 if (myAdditionalSelectionPriority > 0) {
389 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
390 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
393 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
394 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
395 if (!aBasicsOwner.IsNull())
396 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
401 //********************************************************************
402 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
403 const Standard_Integer theMode)
405 if (Shape().ShapeType() == TopAbs_VERTEX) {
406 const TopoDS_Shape& aShape = Shape();
408 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
409 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
411 /// The cause of this method is the last parameter of BRep owner setting into True.
412 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
413 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
414 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
415 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
416 aDeflection, myDrawer->HLRAngle(), 9, 500);
419 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
420 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
423 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
424 Handle(SelectMgr_EntityOwner) anOwner =
425 Handle(SelectMgr_EntityOwner)
426 ::DownCast(anEntity->BaseSensitive()->OwnerId());
435 //********************************************************************
436 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
437 const SelectMgr_SequenceOfOwner& theOwners)
439 Handle(SelectMgr_EntityOwner) anOwner;
440 Handle(ModuleBase_BRepOwner) aCompOwner;
441 for (int i = 1; i <= theOwners.Length(); i++) {
442 anOwner = theOwners.Value(i);
443 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
444 if (aCompOwner.IsNull()) {
445 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
448 TopoDS_Shape aShape = aCompOwner->Shape();
449 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
450 aSelectionPrs->Clear();
452 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
454 aSelectionPrs->SetDisplayPriority(9);
455 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
456 aSelectionPrs->Display();
457 thePM->Color(this, GetContext()->SelectionStyle());
462 //********************************************************************
463 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
464 const Handle(Prs3d_Drawer)& theStyle,
465 const Handle(SelectMgr_EntityOwner)& theOwner)
467 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
471 TopoDS_Shape aShape = aOwner->Shape();
472 if (!aShape.IsNull()) {
473 thePM->Color(this, theStyle);
475 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
476 aHilightPrs->Clear();
478 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
479 aHilightPrs->Highlight(theStyle);
481 if (thePM->IsImmediateModeOn())
482 thePM->AddToImmediateList(aHilightPrs);
487 //********************************************************************
488 void ModuleBase_ResultPrs::updateIsoLines()
490 std::vector<int> aIsoValues;
492 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
494 if (aIsoValues.size() == 0) {
495 aIsoValues.push_back(1);
496 aIsoValues.push_back(1);
500 if (aIsoValues.size() == 0) {
501 aIsoValues.push_back(0);
502 aIsoValues.push_back(0);
509 myUIsoAspect->SetNumber(aIsoValues[0]);
510 myVIsoAspect->SetNumber(aIsoValues[1]);