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->SetUnFreeBoundaryAspect(
83 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
84 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
85 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
87 Quantity_Color aColor;
90 std::vector<int> aIsoValues;
92 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
94 if (aIsoValues.size() == 0) {
95 aIsoValues.push_back(1);
96 aIsoValues.push_back(1);
100 aIsoValues.push_back(0);
101 aIsoValues.push_back(0);
103 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
104 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
105 aDrawer->SetUIsoAspect(myUIsoAspect);
106 aDrawer->SetVIsoAspect(myVIsoAspect);
108 if (aDrawer->HasOwnPointAspect())
109 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
111 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
113 aDrawer = DynamicHilightAttributes();
114 if (aDrawer.IsNull()) {
115 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
116 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
117 aDrawer->SetUIsoAspect(myUIsoAspect);
118 aDrawer->SetVIsoAspect(myVIsoAspect);
119 SetDynamicHilightAttributes(aDrawer);
123 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
124 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
125 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
126 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
127 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
129 ModuleBase_Tools::setPointBallHighlighting(this);
131 // Define colors for wireframe mode
132 setEdgesDefaultColor();
135 //********************************************************************
136 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
138 myAdditionalSelectionPriority = thePriority;
141 //********************************************************************
142 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
144 ViewerData_AISShape::SetColor(theColor);
145 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
146 setEdgesDefaultColor();
147 myUIsoAspect->SetColor(theColor);
148 myVIsoAspect->SetColor(theColor);
151 void ModuleBase_ResultPrs::setEdgesDefaultColor()
153 if (myResult.get()) {
154 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
155 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
157 Handle(Prs3d_Drawer) aDrawer = Attributes();
158 aDrawer->SetFaceBoundaryDraw(Standard_True);
159 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
162 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
163 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
164 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
166 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
167 aDrawer->SetFreeBoundaryDraw(Standard_True);
168 aDrawer->SetWireDraw(Standard_True);
174 //********************************************************************
175 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
177 bool isModified = false;
179 TopoDS_Compound aCompound;
180 BRep_Builder aBBuilder;
181 aBBuilder.MakeCompound (aCompound);
183 myHiddenSubShapes = theShapes;
184 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
185 myVisibleCompound = aCompound;
187 aBBuilder.MakeCompound (aCompound);
188 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
189 for (; aIt.More(); aIt.Next()) {
190 aBBuilder.Add(aCompound, aIt.Value());
192 myHiddenCompound = aCompound;
195 //********************************************************************
196 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
198 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
201 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
202 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
203 for (; aShapeIt.More(); aShapeIt.Next()) {
204 if (theShape.IsSame(aShapeIt.Value()))
211 //********************************************************************
212 bool ModuleBase_ResultPrs::hasSubShapeVisible(
213 const TopoDS_ListOfShape& theShapesToSkip)
215 TopoDS_Compound aCompound;
216 BRep_Builder aBuilder;
217 aBuilder.MakeCompound (aCompound);
218 TopoDS_ListOfShape aShapesToSkip;
219 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
220 aShapesToSkip.Append(aHiddenCopy);
221 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
222 aShapesToSkip.Append(anIt.Value());
224 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
225 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
228 //********************************************************************
229 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
231 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
234 myTransparency = theTransparency;
235 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
239 //********************************************************************
240 void ModuleBase_ResultPrs::Compute(
241 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
242 const Handle(Prs3d_Presentation)& thePresentation,
243 const Standard_Integer theMode)
245 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
246 bool aReadyToDisplay = aShapePtr.get();
247 if (aReadyToDisplay) {
248 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
249 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
250 if (!myOriginalShape.IsNull()) {
251 Set(myOriginalShape);
252 myIsSubstituted = false;
255 else { // convert shape into SHELL
256 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
257 Set(myVisibleCompound);
258 myIsSubstituted = true;
260 aReadyToDisplay = false;
264 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
270 // visualize hidden sub-shapes transparent
271 if (myResult.get()) {
272 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
274 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
275 aReadyToDisplay = true;
278 if (!aReadyToDisplay) {
279 Events_InfoMessage("ModuleBase_ResultPrs",
280 "An empty AIS presentation: ModuleBase_ResultPrs").send();
281 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
282 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
287 //********************************************************************
288 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
289 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
290 const TopoDS_ListOfShape& theHiddenSubShapes)
292 switch (theShape.ShapeType()) {
293 case TopAbs_COMPSOLID:
294 case TopAbs_COMPOUND: {
295 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
296 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
301 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
302 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
307 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
308 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
313 if (theHiddenSubShapes.Contains(theShape))
314 return; // remove hidden shape
315 theBuilder.Add(theCompound, theShape);
319 case TopAbs_VERTEX: {
320 theBuilder.Add(theCompound, theShape);
327 //********************************************************************
328 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
329 const Standard_Integer theMode)
331 if (appendVertexSelection(aSelection, theMode))
334 if (theMode > TopAbs_SHAPE) {
335 // In order to avoid using custom selection modes
336 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
337 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
342 // TODO: OCCT issue should be created for the COMPOUND processing
343 // before it is fixed, the next workaround in necessary
344 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
345 const TopoDS_Shape& aShape = Shape();
346 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
347 // do not activate in compound mode shapes which do not contain compounds
348 if (!aCompExp.More())
352 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
353 // // Limit selection area only by actual object (Shape)
354 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
355 // if (aCompSolid.get()) {
356 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
357 // if (aShapePtr.get()) {
358 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
359 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
360 // /// It is important to have priority for the shape of comp solid result less than priority
361 // /// for the presentation shape which is a sub-result.
362 // /// Reason is to select the sub-objects before: #1592
363 // aPriority = aPriority - 1;
364 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
366 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
367 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
368 // aDeflection, myDrawer->HLRAngle(), 9, 500);
370 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
371 // Handle(SelectMgr_EntityOwner) anOwner =
372 // Handle(SelectMgr_EntityOwner)
373 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
374 // anOwner->Set(this);
380 AIS_Shape::ComputeSelection(aSelection, theMode);
382 if (myAdditionalSelectionPriority > 0) {
383 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
384 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
387 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
388 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
389 if (!aBasicsOwner.IsNull())
390 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
395 //********************************************************************
396 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
397 const Standard_Integer theMode)
399 if (Shape().ShapeType() == TopAbs_VERTEX) {
400 const TopoDS_Shape& aShape = Shape();
402 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
403 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
405 /// The cause of this method is the last parameter of BRep owner setting into True.
406 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
407 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
408 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
409 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
410 aDeflection, myDrawer->HLRAngle(), 9, 500);
413 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
414 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
417 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
418 Handle(SelectMgr_EntityOwner) anOwner =
419 Handle(SelectMgr_EntityOwner)
420 ::DownCast(anEntity->BaseSensitive()->OwnerId());
429 //********************************************************************
430 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
431 const SelectMgr_SequenceOfOwner& theOwners)
433 Handle(SelectMgr_EntityOwner) anOwner;
434 Handle(ModuleBase_BRepOwner) aCompOwner;
435 for (int i = 1; i <= theOwners.Length(); i++) {
436 anOwner = theOwners.Value(i);
437 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
438 if (aCompOwner.IsNull()) {
439 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
442 TopoDS_Shape aShape = aCompOwner->Shape();
443 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
444 aSelectionPrs->Clear();
446 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
448 aSelectionPrs->SetDisplayPriority(9);
449 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
450 aSelectionPrs->Display();
451 thePM->Color(this, GetContext()->SelectionStyle());
456 //********************************************************************
457 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
458 const Handle(Prs3d_Drawer)& theStyle,
459 const Handle(SelectMgr_EntityOwner)& theOwner)
461 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
465 TopoDS_Shape aShape = aOwner->Shape();
466 if (!aShape.IsNull()) {
467 thePM->Color(this, theStyle);
469 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
470 aHilightPrs->Clear();
472 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
473 aHilightPrs->Highlight(theStyle);
475 if (thePM->IsImmediateModeOn())
476 thePM->AddToImmediateList(aHilightPrs);
481 //********************************************************************
482 void ModuleBase_ResultPrs::updateIsoLines()
484 std::vector<int> aIsoValues;
486 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
488 if (aIsoValues.size() == 0) {
489 aIsoValues.push_back(1);
490 aIsoValues.push_back(1);
494 if (aIsoValues.size() == 0) {
495 aIsoValues.push_back(0);
496 aIsoValues.push_back(0);
503 myUIsoAspect->SetNumber(aIsoValues[0]);
504 myVIsoAspect->SetNumber(aIsoValues[1]);