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 Quantity_Color aColor;
89 std::vector<int> aIsoValues;
90 ModelAPI_Tools::getIsoLines(myResult, aIsoValues);
91 if (aIsoValues.size() == 0) {
92 aIsoValues.push_back(1);
93 aIsoValues.push_back(1);
95 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
96 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
97 aDrawer->SetUIsoAspect(myUIsoAspect);
98 aDrawer->SetVIsoAspect(myVIsoAspect);
100 if (aDrawer->HasOwnPointAspect())
101 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
103 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
105 aDrawer = DynamicHilightAttributes();
106 if (aDrawer.IsNull()) {
107 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
108 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
109 Handle(Prs3d_IsoAspect) aUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, 0);
110 Handle(Prs3d_IsoAspect) aVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, 0);
111 aDrawer->SetUIsoAspect(aUIsoAspect);
112 aDrawer->SetVIsoAspect(aVIsoAspect);
113 SetDynamicHilightAttributes(aDrawer);
116 aDrawer->VIsoAspect()->SetNumber(0);
117 aDrawer->UIsoAspect()->SetNumber(0);
119 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
120 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
121 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
122 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
123 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
125 ModuleBase_Tools::setPointBallHighlighting(this);
127 // Define colors for wireframe mode
128 setEdgesDefaultColor();
131 //********************************************************************
132 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
134 myAdditionalSelectionPriority = thePriority;
137 //********************************************************************
138 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
140 ViewerData_AISShape::SetColor(theColor);
141 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
142 setEdgesDefaultColor();
143 myUIsoAspect->SetColor(theColor);
144 myVIsoAspect->SetColor(theColor);
147 void ModuleBase_ResultPrs::setEdgesDefaultColor()
149 if (myResult.get()) {
150 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
151 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
153 Handle(Prs3d_Drawer) aDrawer = Attributes();
154 aDrawer->SetFaceBoundaryDraw(Standard_True);
155 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
158 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
159 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
160 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
162 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
163 aDrawer->SetFreeBoundaryDraw(Standard_True);
164 aDrawer->SetWireDraw(Standard_True);
170 //********************************************************************
171 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
173 bool isModified = false;
175 TopoDS_Compound aCompound;
176 BRep_Builder aBBuilder;
177 aBBuilder.MakeCompound (aCompound);
179 myHiddenSubShapes = theShapes;
180 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
181 myVisibleCompound = aCompound;
183 aBBuilder.MakeCompound (aCompound);
184 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
185 for (; aIt.More(); aIt.Next()) {
186 aBBuilder.Add(aCompound, aIt.Value());
188 myHiddenCompound = aCompound;
191 //********************************************************************
192 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
194 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
197 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
198 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
199 for (; aShapeIt.More(); aShapeIt.Next()) {
200 if (theShape.IsSame(aShapeIt.Value()))
207 //********************************************************************
208 bool ModuleBase_ResultPrs::hasSubShapeVisible(
209 const TopoDS_ListOfShape& theShapesToSkip)
211 TopoDS_Compound aCompound;
212 BRep_Builder aBuilder;
213 aBuilder.MakeCompound (aCompound);
214 TopoDS_ListOfShape aShapesToSkip;
215 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
216 aShapesToSkip.Append(aHiddenCopy);
217 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
218 aShapesToSkip.Append(anIt.Value());
220 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
221 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
224 //********************************************************************
225 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
227 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
230 myTransparency = theTransparency;
231 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
235 //********************************************************************
236 void ModuleBase_ResultPrs::Compute(
237 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
238 const Handle(Prs3d_Presentation)& thePresentation,
239 const Standard_Integer theMode)
241 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
242 bool aReadyToDisplay = aShapePtr.get();
243 if (aReadyToDisplay) {
244 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
245 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
246 if (!myOriginalShape.IsNull()) {
247 Set(myOriginalShape);
248 myIsSubstituted = false;
251 else { // convert shape into SHELL
252 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
253 Set(myVisibleCompound);
254 myIsSubstituted = true;
256 aReadyToDisplay = false;
260 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
266 // visualize hidden sub-shapes transparent
267 if (myResult.get()) {
268 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
270 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
271 aReadyToDisplay = true;
274 if (!aReadyToDisplay) {
275 Events_InfoMessage("ModuleBase_ResultPrs",
276 "An empty AIS presentation: ModuleBase_ResultPrs").send();
277 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
278 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
283 //********************************************************************
284 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
285 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
286 const TopoDS_ListOfShape& theHiddenSubShapes)
288 switch (theShape.ShapeType()) {
289 case TopAbs_COMPSOLID:
290 case TopAbs_COMPOUND: {
291 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
292 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
297 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
298 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
303 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
304 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
309 if (theHiddenSubShapes.Contains(theShape))
310 return; // remove hidden shape
311 theBuilder.Add(theCompound, theShape);
315 case TopAbs_VERTEX: {
316 theBuilder.Add(theCompound, theShape);
323 //********************************************************************
324 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
325 const Standard_Integer theMode)
327 if (appendVertexSelection(aSelection, theMode))
330 if (theMode > TopAbs_SHAPE) {
331 // In order to avoid using custom selection modes
332 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
333 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
338 // TODO: OCCT issue should be created for the COMPOUND processing
339 // before it is fixed, the next workaround in necessary
340 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
341 const TopoDS_Shape& aShape = Shape();
342 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
343 // do not activate in compound mode shapes which do not contain compounds
344 if (!aCompExp.More())
348 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
349 // // Limit selection area only by actual object (Shape)
350 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
351 // if (aCompSolid.get()) {
352 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
353 // if (aShapePtr.get()) {
354 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
355 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
356 // /// It is important to have priority for the shape of comp solid result less than priority
357 // /// for the presentation shape which is a sub-result.
358 // /// Reason is to select the sub-objects before: #1592
359 // aPriority = aPriority - 1;
360 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
362 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
363 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
364 // aDeflection, myDrawer->HLRAngle(), 9, 500);
366 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
367 // Handle(SelectMgr_EntityOwner) anOwner =
368 // Handle(SelectMgr_EntityOwner)
369 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
370 // anOwner->Set(this);
376 AIS_Shape::ComputeSelection(aSelection, theMode);
378 if (myAdditionalSelectionPriority > 0) {
379 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
380 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
383 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
384 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
385 if (!aBasicsOwner.IsNull())
386 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
391 //********************************************************************
392 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
393 const Standard_Integer theMode)
395 if (Shape().ShapeType() == TopAbs_VERTEX) {
396 const TopoDS_Shape& aShape = Shape();
398 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
399 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
401 /// The cause of this method is the last parameter of BRep owner setting into True.
402 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
403 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
404 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
405 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
406 aDeflection, myDrawer->HLRAngle(), 9, 500);
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(SelectMgr_EntityOwner) anOwner =
415 Handle(SelectMgr_EntityOwner)
416 ::DownCast(anEntity->BaseSensitive()->OwnerId());
425 //********************************************************************
426 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
427 const SelectMgr_SequenceOfOwner& theOwners)
429 Handle(SelectMgr_EntityOwner) anOwner;
430 Handle(ModuleBase_BRepOwner) aCompOwner;
431 for (int i = 1; i <= theOwners.Length(); i++) {
432 anOwner = theOwners.Value(i);
433 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
434 if (aCompOwner.IsNull()) {
435 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
438 TopoDS_Shape aShape = aCompOwner->Shape();
439 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
440 aSelectionPrs->Clear();
442 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
444 aSelectionPrs->SetDisplayPriority(9);
445 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
446 aSelectionPrs->Display();
447 thePM->Color(this, GetContext()->SelectionStyle());
452 //********************************************************************
453 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
454 const Handle(Prs3d_Drawer)& theStyle,
455 const Handle(SelectMgr_EntityOwner)& theOwner)
457 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
461 TopoDS_Shape aShape = aOwner->Shape();
462 if (!aShape.IsNull()) {
463 thePM->Color(this, theStyle);
465 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
466 aHilightPrs->Clear();
468 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
469 aHilightPrs->Highlight(theStyle);
471 if (thePM->IsImmediateModeOn())
472 thePM->AddToImmediateList(aHilightPrs);