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"
22 #include <GeomAPI_PlanarEdges.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <ModelAPI_AttributeIntArray.h>
30 #include "ModuleBase_Tools.h"
31 #include "ModuleBase_BRepOwner.h"
33 #include <Events_InfoMessage.h>
34 #include <Events_Loop.h>
36 #include <AIS_ColoredDrawer.hxx>
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_Selection.hxx>
39 #include <BOPTools_AlgoTools3D.hxx>
40 #include <BRep_Builder.hxx>
41 #include <Graphic3d_AspectMarker3d.hxx>
42 #include <Prs3d_Drawer.hxx>
44 #include <Prs3d_PointAspect.hxx>
45 #include <Prs3d_IsoAspect.hxx>
46 #include <Prs3d_ShadingAspect.hxx>
47 #include <SelectMgr_SequenceOfOwner.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
49 #include <SelectMgr_SelectionManager.hxx>
50 #include <StdPrs_WFShape.hxx>
51 #include <StdPrs_ShadedShape.hxx>
52 #include <StdSelect_BRepSelectionTool.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <TopoDS_Builder.hxx>
58 //*******************************************************************************************
60 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
64 //********************************************************************
65 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
66 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
67 myTransparency(1), myIsSubstituted(false)
69 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
70 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
73 // Activate individual repaintng if this is a part of compsolid
74 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
75 SetAutoHilight(aResOwner.get() == NULL);
80 //********************************************************************
81 ModuleBase_ResultPrs::ModuleBase_ResultPrs(FieldStepPtr theStep)
82 : ViewerData_AISShape(TopoDS_Shape()), myStep(theStep), myAdditionalSelectionPriority(0),
83 myTransparency(1), myIsSubstituted(false)
85 GeomShapePtr aShapePtr = theStep->field()->shape();
86 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
93 //********************************************************************
94 void ModuleBase_ResultPrs::init()
96 // Set own free boundaries aspect in order to have free
97 // and unfree boundaries with different colors
98 Handle(Prs3d_Drawer) aDrawer = Attributes();
99 Handle(Prs3d_LineAspect) aFreeBndAspect =
100 new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
101 aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
103 if (aDrawer->HasOwnPointAspect())
104 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
106 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
108 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
109 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
110 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
111 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
113 ModuleBase_Tools::setPointBallHighlighting(this);
115 // Define colors for wireframe mode
116 setEdgesDefaultColor();
120 //********************************************************************
121 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
123 myAdditionalSelectionPriority = thePriority;
126 //********************************************************************
127 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
129 ViewerData_AISShape::SetColor(theColor);
130 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
131 setEdgesDefaultColor();
134 void ModuleBase_ResultPrs::setEdgesDefaultColor()
136 if (myResult.get()) {
137 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
138 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
141 Handle(Prs3d_Drawer) aDrawer = Attributes();
142 Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
143 //anAspect->SetColor(Quantity_NOC_YELLOW);
144 //aDrawer->SetLineAspect(anAspect);
146 // - unfree boundaries color
147 anAspect = aDrawer->UnFreeBoundaryAspect();
148 anAspect->SetColor(Quantity_NOC_YELLOW);
149 aDrawer->SetUnFreeBoundaryAspect(anAspect);
150 aDrawer->SetUnFreeBoundaryDraw(true);
152 // - free boundaries color
153 anAspect = aDrawer->FreeBoundaryAspect();
154 anAspect->SetColor(Quantity_NOC_GREEN);
155 aDrawer->SetFreeBoundaryAspect(anAspect);
156 aDrawer->SetFreeBoundaryDraw(true);
158 // - standalone edges color
159 anAspect = aDrawer->WireAspect();
160 anAspect->SetColor(Quantity_NOC_RED);
161 aDrawer->SetWireAspect(anAspect);
167 //********************************************************************
168 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
170 bool isModified = false;
172 TopoDS_Compound aCompound;
173 BRep_Builder aBBuilder;
174 aBBuilder.MakeCompound (aCompound);
176 TopoDS_Compound aShownComp;
178 aShownComp = TopoDS::Compound(Shape());
180 // restore hidden shapes if there are not the shapes in parameter container
181 NCollection_List<TopoDS_Shape> aVisibleSubShapes;
182 NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes);
183 for (; aHiddenIt.More(); aHiddenIt.Next()) {
184 if (!theShapes.Contains(aHiddenIt.Value())) {
185 aVisibleSubShapes.Append(aHiddenIt.Value());
188 aBBuilder.Add(aCompound, aHiddenIt.Value());
189 if (!aShownComp.IsNull())
190 aBBuilder.Remove(aShownComp, aHiddenIt.Value());
193 isModified = !aVisibleSubShapes.IsEmpty();
194 NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes);
195 for (; aVisibleIt.More(); aVisibleIt.Next()) {
196 if (myHiddenSubShapes.Contains(aVisibleIt.Value())) {
197 myHiddenSubShapes.Remove(aVisibleIt.Value());
198 if (!aShownComp.IsNull())
199 aBBuilder.Add(aShownComp, aVisibleIt.Value());
202 // append hidden shapes into internal container if there are not these shapes
203 NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes);
204 for (; aShapeIt.More(); aShapeIt.Next()) {
205 if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
208 if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
209 myHiddenSubShapes.Append(aShapeIt.Value());
210 aBBuilder.Add (aCompound, aShapeIt.Value());
211 if (!aShownComp.IsNull())
212 aBBuilder.Remove(aShownComp, aShapeIt.Value());
216 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 for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(myHiddenSubShapes); aShapeIt.More();
230 if (theShape.IsSame(aShapeIt.Value()))
237 //********************************************************************
238 bool ModuleBase_ResultPrs::hasSubShapeVisible(
239 const NCollection_List<TopoDS_Shape>& theShapesToSkip)
241 TopoDS_Compound aCompound;
242 BRep_Builder aBuilder;
243 aBuilder.MakeCompound (aCompound);
244 NCollection_List<TopoDS_Shape> aShapesToSkip;
245 aShapesToSkip.Append(myHiddenSubShapes);
246 for (NCollection_List<TopoDS_Shape>::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 GeomShapePtr ModuleBase_ResultPrs::getOriginalShape() const
268 return myStep->field()->shape();
269 return myResult->shape();
272 //********************************************************************
273 void ModuleBase_ResultPrs::Compute(
274 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
275 const Handle(Prs3d_Presentation)& thePresentation,
276 const Standard_Integer theMode)
278 std::shared_ptr<GeomAPI_Shape> aShapePtr = getOriginalShape();
279 bool aReadyToDisplay = aShapePtr.get();
280 if (aReadyToDisplay) {
281 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
282 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
283 if (!myOriginalShape.IsNull()) {
284 Set(myOriginalShape);
285 myIsSubstituted = false;
288 else { // convert shape into SHELL
289 TopoDS_Compound aCompound;
290 if (!myIsSubstituted) {
291 BRep_Builder aBuilder;
292 aBuilder.MakeCompound(aCompound);
293 collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
296 aCompound = TopoDS::Compound(Shape());
298 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
300 myIsSubstituted = true;
302 aReadyToDisplay = false;
306 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
312 // visualize hidden sub-shapes transparent
313 if (myResult.get()) {
314 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
316 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
317 aReadyToDisplay = true;
320 if (!aReadyToDisplay) {
321 Events_InfoMessage("ModuleBase_ResultPrs",
322 "An empty AIS presentation: ModuleBase_ResultPrs").send();
323 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
324 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
329 //********************************************************************
330 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
331 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
332 const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
334 switch (theShape.ShapeType()) {
335 case TopAbs_COMPSOLID:
336 case TopAbs_COMPOUND: {
337 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
338 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
343 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
344 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
349 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
350 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
355 if (theHiddenSubShapes.Contains(theShape))
356 return; // remove hidden shape
357 theBuilder.Add(theCompound, theShape);
361 case TopAbs_VERTEX: {
362 theBuilder.Add(theCompound, theShape);
369 //********************************************************************
370 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
371 const Standard_Integer theMode)
373 if (appendVertexSelection(aSelection, theMode))
376 if (theMode > TopAbs_SHAPE) {
377 // In order to avoid using custom selection modes
378 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
379 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
384 // TODO: OCCT issue should be created for the COMPOUND processing
385 // before it is fixed, the next workaround in necessary
386 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
387 const TopoDS_Shape& aShape = Shape();
388 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
389 // do not activate in compound mode shapes which do not contain compounds
390 if (!aCompExp.More())
394 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
395 // // Limit selection area only by actual object (Shape)
396 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
397 // if (aCompSolid.get()) {
398 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
399 // if (aShapePtr.get()) {
400 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
401 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
402 // /// It is important to have priority for the shape of comp solid result less than priority
403 // /// for the presentation shape which is a sub-result.
404 // /// Reason is to select the sub-objects before: #1592
405 // aPriority = aPriority - 1;
406 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
408 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
409 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
410 // aDeflection, myDrawer->HLRAngle(), 9, 500);
412 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
413 // Handle(SelectMgr_EntityOwner) anOwner =
414 // Handle(SelectMgr_EntityOwner)
415 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
416 // anOwner->Set(this);
422 AIS_Shape::ComputeSelection(aSelection, theMode);
424 if (myAdditionalSelectionPriority > 0) {
425 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
426 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
429 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
430 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
431 if (!aBasicsOwner.IsNull())
432 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
437 //********************************************************************
438 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
439 const Standard_Integer theMode)
441 if (Shape().ShapeType() == TopAbs_VERTEX) {
442 const TopoDS_Shape& aShape = Shape();
444 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
445 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
447 /// The cause of this method is the last parameter of BRep owner setting into True.
448 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
449 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
450 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
451 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
452 aDeflection, myDrawer->HLRAngle(), 9, 500);
455 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
456 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
459 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
460 Handle(SelectMgr_EntityOwner) anOwner =
461 Handle(SelectMgr_EntityOwner)
462 ::DownCast(anEntity->BaseSensitive()->OwnerId());
471 //********************************************************************
472 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
473 const SelectMgr_SequenceOfOwner& theOwners)
475 Handle(SelectMgr_EntityOwner) anOwner;
476 Handle(ModuleBase_BRepOwner) aCompOwner;
477 for (int i = 1; i <= theOwners.Length(); i++) {
478 anOwner = theOwners.Value(i);
479 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
480 if (aCompOwner.IsNull()) {
481 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
484 TopoDS_Shape aShape = aCompOwner->Shape();
485 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
486 aSelectionPrs->Clear();
488 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
490 aSelectionPrs->SetDisplayPriority(9);
491 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
492 aSelectionPrs->Display();
493 thePM->Color(this, GetContext()->SelectionStyle());
498 //********************************************************************
499 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
500 const Handle(Prs3d_Drawer)& theStyle,
501 const Handle(SelectMgr_EntityOwner)& theOwner)
503 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
507 TopoDS_Shape aShape = aOwner->Shape();
508 if (!aShape.IsNull()) {
509 thePM->Color(this, theStyle);
511 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
512 aHilightPrs->Clear();
514 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
515 aHilightPrs->Highlight(theStyle);
517 if (thePM->IsImmediateModeOn())
518 thePM->AddToImmediateList(aHilightPrs);