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;
91 ModelAPI_Tools::getIsoLines(myResult, aIsoValues);
92 if (aIsoValues.size() == 0) {
93 aIsoValues.push_back(1);
94 aIsoValues.push_back(1);
96 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
97 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
98 aDrawer->SetUIsoAspect(myUIsoAspect);
99 aDrawer->SetVIsoAspect(myVIsoAspect);
101 aDrawer->SetIsoOnPlane(false);
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 aDrawer = DynamicHilightAttributes();
109 if (aDrawer.IsNull()) {
110 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
111 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
112 Handle(Prs3d_IsoAspect) aUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, 0);
113 Handle(Prs3d_IsoAspect) aVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, 0);
114 aDrawer->SetUIsoAspect(aUIsoAspect);
115 aDrawer->SetVIsoAspect(aVIsoAspect);
116 SetDynamicHilightAttributes(aDrawer);
119 aDrawer->VIsoAspect()->SetNumber(0);
120 aDrawer->UIsoAspect()->SetNumber(0);
122 aDrawer->SetIsoOnPlane(false);
124 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
125 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
126 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
127 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
128 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
130 ModuleBase_Tools::setPointBallHighlighting(this);
132 // Define colors for wireframe mode
133 setEdgesDefaultColor();
136 //********************************************************************
137 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
139 myAdditionalSelectionPriority = thePriority;
142 //********************************************************************
143 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
145 ViewerData_AISShape::SetColor(theColor);
146 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
147 setEdgesDefaultColor();
148 myUIsoAspect->SetColor(theColor);
149 myVIsoAspect->SetColor(theColor);
152 void ModuleBase_ResultPrs::setEdgesDefaultColor()
154 if (myResult.get()) {
155 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
156 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
158 Handle(Prs3d_Drawer) aDrawer = Attributes();
159 aDrawer->SetFaceBoundaryDraw(Standard_True);
160 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
163 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
164 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
165 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
167 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
168 aDrawer->SetFreeBoundaryDraw(Standard_True);
169 aDrawer->SetWireDraw(Standard_True);
175 //********************************************************************
176 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
178 bool isModified = false;
180 TopoDS_Compound aCompound;
181 BRep_Builder aBBuilder;
182 aBBuilder.MakeCompound (aCompound);
184 myHiddenSubShapes = theShapes;
185 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
186 myVisibleCompound = aCompound;
188 aBBuilder.MakeCompound (aCompound);
189 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
190 for (; aIt.More(); aIt.Next()) {
191 aBBuilder.Add(aCompound, aIt.Value());
193 myHiddenCompound = aCompound;
196 //********************************************************************
197 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
199 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
202 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
203 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
204 for (; aShapeIt.More(); aShapeIt.Next()) {
205 if (theShape.IsSame(aShapeIt.Value()))
212 //********************************************************************
213 bool ModuleBase_ResultPrs::hasSubShapeVisible(
214 const TopoDS_ListOfShape& theShapesToSkip)
216 TopoDS_Compound aCompound;
217 BRep_Builder aBuilder;
218 aBuilder.MakeCompound (aCompound);
219 TopoDS_ListOfShape aShapesToSkip;
220 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
221 aShapesToSkip.Append(aHiddenCopy);
222 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
223 aShapesToSkip.Append(anIt.Value());
225 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
226 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
229 //********************************************************************
230 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
232 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
235 myTransparency = theTransparency;
236 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
240 //********************************************************************
241 void ModuleBase_ResultPrs::Compute(
242 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
243 const Handle(Prs3d_Presentation)& thePresentation,
244 const Standard_Integer theMode)
246 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
247 bool aReadyToDisplay = aShapePtr.get();
248 if (aReadyToDisplay) {
249 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
250 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
251 if (!myOriginalShape.IsNull()) {
252 Set(myOriginalShape);
253 myIsSubstituted = false;
256 else { // convert shape into SHELL
257 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
258 Set(myVisibleCompound);
259 myIsSubstituted = true;
261 aReadyToDisplay = false;
265 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
271 // visualize hidden sub-shapes transparent
272 if (myResult.get()) {
273 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
275 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
276 aReadyToDisplay = true;
279 if (!aReadyToDisplay) {
280 Events_InfoMessage("ModuleBase_ResultPrs",
281 "An empty AIS presentation: ModuleBase_ResultPrs").send();
282 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
283 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
288 //********************************************************************
289 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
290 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
291 const TopoDS_ListOfShape& theHiddenSubShapes)
293 switch (theShape.ShapeType()) {
294 case TopAbs_COMPSOLID:
295 case TopAbs_COMPOUND: {
296 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
297 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
302 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
303 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
308 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
309 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
314 if (theHiddenSubShapes.Contains(theShape))
315 return; // remove hidden shape
316 theBuilder.Add(theCompound, theShape);
320 case TopAbs_VERTEX: {
321 theBuilder.Add(theCompound, theShape);
328 //********************************************************************
329 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
330 const Standard_Integer theMode)
332 if (appendVertexSelection(aSelection, theMode))
335 if (theMode > TopAbs_SHAPE) {
336 // In order to avoid using custom selection modes
337 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
338 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
343 // TODO: OCCT issue should be created for the COMPOUND processing
344 // before it is fixed, the next workaround in necessary
345 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
346 const TopoDS_Shape& aShape = Shape();
347 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
348 // do not activate in compound mode shapes which do not contain compounds
349 if (!aCompExp.More())
353 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
354 // // Limit selection area only by actual object (Shape)
355 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
356 // if (aCompSolid.get()) {
357 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
358 // if (aShapePtr.get()) {
359 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
360 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
361 // /// It is important to have priority for the shape of comp solid result less than priority
362 // /// for the presentation shape which is a sub-result.
363 // /// Reason is to select the sub-objects before: #1592
364 // aPriority = aPriority - 1;
365 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
367 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
368 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
369 // aDeflection, myDrawer->HLRAngle(), 9, 500);
371 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
372 // Handle(SelectMgr_EntityOwner) anOwner =
373 // Handle(SelectMgr_EntityOwner)
374 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
375 // anOwner->Set(this);
381 AIS_Shape::ComputeSelection(aSelection, theMode);
383 if (myAdditionalSelectionPriority > 0) {
384 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
385 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
388 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
389 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
390 if (!aBasicsOwner.IsNull())
391 aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
396 //********************************************************************
397 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
398 const Standard_Integer theMode)
400 if (Shape().ShapeType() == TopAbs_VERTEX) {
401 const TopoDS_Shape& aShape = Shape();
403 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
404 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
406 /// The cause of this method is the last parameter of BRep owner setting into True.
407 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
408 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
409 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
410 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
411 aDeflection, myDrawer->HLRAngle(), 9, 500);
414 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
415 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
418 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
419 Handle(SelectMgr_EntityOwner) anOwner =
420 Handle(SelectMgr_EntityOwner)
421 ::DownCast(anEntity->BaseSensitive()->OwnerId());
430 //********************************************************************
431 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
432 const SelectMgr_SequenceOfOwner& theOwners)
434 Handle(SelectMgr_EntityOwner) anOwner;
435 Handle(ModuleBase_BRepOwner) aCompOwner;
436 for (int i = 1; i <= theOwners.Length(); i++) {
437 anOwner = theOwners.Value(i);
438 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
439 if (aCompOwner.IsNull()) {
440 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
443 TopoDS_Shape aShape = aCompOwner->Shape();
444 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
445 aSelectionPrs->Clear();
447 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
449 aSelectionPrs->SetDisplayPriority(9);
450 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
451 aSelectionPrs->Display();
452 thePM->Color(this, GetContext()->SelectionStyle());
457 //********************************************************************
458 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
459 const Handle(Prs3d_Drawer)& theStyle,
460 const Handle(SelectMgr_EntityOwner)& theOwner)
462 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
466 TopoDS_Shape aShape = aOwner->Shape();
467 if (!aShape.IsNull()) {
468 thePM->Color(this, theStyle);
470 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
471 aHilightPrs->Clear();
473 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
474 aHilightPrs->Highlight(theStyle);
476 if (thePM->IsImmediateModeOn())
477 thePM->AddToImmediateList(aHilightPrs);