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 <SelectMgr_SequenceOfOwner.hxx>
49 #include <SelectMgr_EntityOwner.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <StdPrs_ShadedShape.hxx>
53 #include <StdSelect_BRepSelectionTool.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <TopoDS_Builder.hxx>
59 //*******************************************************************************************
61 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
65 //********************************************************************
66 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
67 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
68 myTransparency(1), myIsSubstituted(false)
70 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
71 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
74 // Activate individual repaintng if this is a part of compsolid
75 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
76 SetAutoHilight(aResOwner.get() == NULL);
78 // Set own free boundaries aspect in order to have free
79 // and unfree boundaries with different colors
80 Handle(Prs3d_Drawer) aDrawer = Attributes();
81 aDrawer->SetUnFreeBoundaryAspect(
82 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
83 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
84 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
86 aDrawer->VIsoAspect()->SetNumber(0);
87 aDrawer->UIsoAspect()->SetNumber(0);
89 if (aDrawer->HasOwnPointAspect())
90 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
92 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
94 aDrawer = DynamicHilightAttributes();
95 if (aDrawer.IsNull()) {
96 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
97 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
98 aDrawer->VIsoAspect()->SetNumber(0);
99 aDrawer->UIsoAspect()->SetNumber(0);
100 SetDynamicHilightAttributes(aDrawer);
103 aDrawer->VIsoAspect()->SetNumber(0);
104 aDrawer->UIsoAspect()->SetNumber(0);
106 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
107 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
108 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
109 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
110 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
112 ModuleBase_Tools::setPointBallHighlighting(this);
114 // Define colors for wireframe mode
115 setEdgesDefaultColor();
118 //********************************************************************
119 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
121 myAdditionalSelectionPriority = thePriority;
124 //********************************************************************
125 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
127 ViewerData_AISShape::SetColor(theColor);
128 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
129 setEdgesDefaultColor();
132 void ModuleBase_ResultPrs::setEdgesDefaultColor()
134 if (myResult.get()) {
135 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
136 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
138 Handle(Prs3d_Drawer) aDrawer = Attributes();
139 aDrawer->SetFaceBoundaryDraw(Standard_True);
140 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
143 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
144 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
145 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
147 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
148 aDrawer->SetFreeBoundaryDraw(Standard_True);
149 aDrawer->SetWireDraw(Standard_True);
155 //********************************************************************
156 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
158 bool isModified = false;
160 TopoDS_Compound aCompound;
161 BRep_Builder aBBuilder;
162 aBBuilder.MakeCompound (aCompound);
164 myHiddenSubShapes = theShapes;
165 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
166 myVisibleCompound = aCompound;
168 aBBuilder.MakeCompound (aCompound);
169 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
170 for (; aIt.More(); aIt.Next()) {
171 aBBuilder.Add(aCompound, aIt.Value());
173 myHiddenCompound = aCompound;
176 //********************************************************************
177 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
179 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
182 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
183 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
184 for (; aShapeIt.More(); aShapeIt.Next()) {
185 if (theShape.IsSame(aShapeIt.Value()))
192 //********************************************************************
193 bool ModuleBase_ResultPrs::hasSubShapeVisible(
194 const TopoDS_ListOfShape& theShapesToSkip)
196 TopoDS_Compound aCompound;
197 BRep_Builder aBuilder;
198 aBuilder.MakeCompound (aCompound);
199 TopoDS_ListOfShape aShapesToSkip;
200 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
201 aShapesToSkip.Append(aHiddenCopy);
202 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
203 aShapesToSkip.Append(anIt.Value());
205 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
206 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
209 //********************************************************************
210 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
212 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
215 myTransparency = theTransparency;
216 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
220 //********************************************************************
221 void ModuleBase_ResultPrs::Compute(
222 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
223 const Handle(Prs3d_Presentation)& thePresentation,
224 const Standard_Integer theMode)
226 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
227 bool aReadyToDisplay = aShapePtr.get();
228 if (aReadyToDisplay) {
229 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
230 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
231 if (!myOriginalShape.IsNull()) {
232 Set(myOriginalShape);
233 myIsSubstituted = false;
236 else { // convert shape into SHELL
237 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
238 Set(myVisibleCompound);
239 myIsSubstituted = true;
241 aReadyToDisplay = false;
245 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
251 // visualize hidden sub-shapes transparent
252 if (myResult.get()) {
253 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
255 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
256 aReadyToDisplay = true;
259 if (!aReadyToDisplay) {
260 Events_InfoMessage("ModuleBase_ResultPrs",
261 "An empty AIS presentation: ModuleBase_ResultPrs").send();
262 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
263 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
268 //********************************************************************
269 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
270 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
271 const TopoDS_ListOfShape& theHiddenSubShapes)
273 switch (theShape.ShapeType()) {
274 case TopAbs_COMPSOLID:
275 case TopAbs_COMPOUND: {
276 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
277 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
282 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
283 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
288 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
289 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
294 if (theHiddenSubShapes.Contains(theShape))
295 return; // remove hidden shape
296 theBuilder.Add(theCompound, theShape);
300 case TopAbs_VERTEX: {
301 theBuilder.Add(theCompound, theShape);
308 //********************************************************************
309 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
310 const Standard_Integer theMode)
312 if (appendVertexSelection(aSelection, theMode))
315 if (theMode > TopAbs_SHAPE) {
316 // In order to avoid using custom selection modes
317 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
318 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
323 // TODO: OCCT issue should be created for the COMPOUND processing
324 // before it is fixed, the next workaround in necessary
325 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
326 const TopoDS_Shape& aShape = Shape();
327 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
328 // do not activate in compound mode shapes which do not contain compounds
329 if (!aCompExp.More())
333 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
334 // // Limit selection area only by actual object (Shape)
335 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
336 // if (aCompSolid.get()) {
337 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
338 // if (aShapePtr.get()) {
339 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
340 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
341 // /// It is important to have priority for the shape of comp solid result less than priority
342 // /// for the presentation shape which is a sub-result.
343 // /// Reason is to select the sub-objects before: #1592
344 // aPriority = aPriority - 1;
345 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
347 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
348 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
349 // aDeflection, myDrawer->HLRAngle(), 9, 500);
351 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
352 // Handle(SelectMgr_EntityOwner) anOwner =
353 // Handle(SelectMgr_EntityOwner)
354 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
355 // anOwner->Set(this);
361 AIS_Shape::ComputeSelection(aSelection, theMode);
363 if (myAdditionalSelectionPriority > 0) {
364 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
365 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
368 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
369 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
370 if (!aBasicsOwner.IsNull())
371 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
376 //********************************************************************
377 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
378 const Standard_Integer theMode)
380 if (Shape().ShapeType() == TopAbs_VERTEX) {
381 const TopoDS_Shape& aShape = Shape();
383 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
384 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
386 /// The cause of this method is the last parameter of BRep owner setting into True.
387 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
388 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
389 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
390 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
391 aDeflection, myDrawer->HLRAngle(), 9, 500);
394 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
395 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
398 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
399 Handle(SelectMgr_EntityOwner) anOwner =
400 Handle(SelectMgr_EntityOwner)
401 ::DownCast(anEntity->BaseSensitive()->OwnerId());
410 //********************************************************************
411 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
412 const SelectMgr_SequenceOfOwner& theOwners)
414 Handle(SelectMgr_EntityOwner) anOwner;
415 Handle(ModuleBase_BRepOwner) aCompOwner;
416 for (int i = 1; i <= theOwners.Length(); i++) {
417 anOwner = theOwners.Value(i);
418 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
419 if (aCompOwner.IsNull()) {
420 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
423 TopoDS_Shape aShape = aCompOwner->Shape();
424 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
425 aSelectionPrs->Clear();
427 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
429 aSelectionPrs->SetDisplayPriority(9);
430 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
431 aSelectionPrs->Display();
432 thePM->Color(this, GetContext()->SelectionStyle());
437 //********************************************************************
438 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
439 const Handle(Prs3d_Drawer)& theStyle,
440 const Handle(SelectMgr_EntityOwner)& theOwner)
442 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
446 TopoDS_Shape aShape = aOwner->Shape();
447 if (!aShape.IsNull()) {
448 thePM->Color(this, theStyle);
450 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
451 aHilightPrs->Clear();
453 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
454 aHilightPrs->Highlight(theStyle);
456 if (thePM->IsImmediateModeOn())
457 thePM->AddToImmediateList(aHilightPrs);