1 // Copyright (C) 2014-2020 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>
24 #include <GeomAPI_Edge.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_AttributeIntArray.h>
32 #include "ModuleBase_Tools.h"
33 #include "ModuleBase_BRepOwner.h"
35 #include <Events_InfoMessage.h>
36 #include <Events_Loop.h>
37 #include <Config_PropManager.h>
39 #include <AIS_ColoredDrawer.hxx>
40 #include <AIS_InteractiveContext.hxx>
41 #include <AIS_Selection.hxx>
42 #include <BOPTools_AlgoTools3D.hxx>
43 #include <BRep_Builder.hxx>
44 #include <Graphic3d_AspectMarker3d.hxx>
45 #include <Prs3d_Drawer.hxx>
47 #include <Prs3d_PointAspect.hxx>
48 #include <Prs3d_IsoAspect.hxx>
49 #include <Prs3d_ShadingAspect.hxx>
50 #include <Prs3d_PlaneAspect.hxx>
51 #include <SelectMgr_SequenceOfOwner.hxx>
52 #include <SelectMgr_EntityOwner.hxx>
53 #include <SelectMgr_SelectionManager.hxx>
54 #include <StdPrs_WFShape.hxx>
55 #include <StdPrs_ShadedShape.hxx>
56 #include <StdSelect_BRepSelectionTool.hxx>
57 #include <TColStd_ListIteratorOfListOfInteger.hxx>
58 #include <TopExp_Explorer.hxx>
60 #include <TopoDS_Builder.hxx>
61 #include <TopoDS_Edge.hxx>
62 #include <BRepMesh_IncrementalMesh.hxx>
63 #include <Standard_Version.hxx>
65 #if OCC_VERSION_HEX > 0x070400
66 #include <StdPrs_ToolTriangulatedShape.hxx>
69 //*******************************************************************************************
71 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
75 //********************************************************************
76 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
77 : ViewerData_AISShape(TopoDS_Shape()),
79 myIsSubstituted(false),
81 myAdditionalSelectionPriority(0)
84 GeomShapePtr aShapePtr = ModelAPI_Tools::shape(theResult);
85 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
86 // Workaround for Sketch subshapes which has no discrete representation
87 // until sketch faces are built and displayed.
88 // Thus, perform discretization of such edges.
89 if (theResult->groupName() == ModelAPI_ResultConstruction::group() &&
90 aShape.ShapeType() == TopAbs_EDGE) {
91 GeomEdgePtr anEdgePtr = GeomEdgePtr(new GeomAPI_Edge(aShapePtr));
92 if (anEdgePtr->isCircle() || anEdgePtr->isArc()) {
93 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
95 Handle(Poly_Polygon3D) aPoly3D = BRep_Tool::Polygon3D(anEdge, aLoc);
96 if (aPoly3D.IsNull()) {
97 double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
98 BRepMesh_IncrementalMesh(aShape, aDeflection);
104 // Activate individual repaintng if this is a part of compsolid
105 ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
106 SetAutoHilight(aResOwner.get() == NULL);
108 // Set own free boundaries aspect in order to have free
109 // and unfree boundaries with different colors
110 Handle(Prs3d_Drawer) aDrawer = Attributes();
111 aDrawer->SetUnFreeBoundaryAspect(
112 new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
113 aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
114 aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
116 Quantity_Color aColor;
119 std::vector<int> aIsoValues;
121 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
123 if (aIsoValues.size() == 0) {
124 aIsoValues.push_back(1);
125 aIsoValues.push_back(1);
129 aIsoValues.push_back(0);
130 aIsoValues.push_back(0);
132 myUIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[0]);
133 myVIsoAspect = new Prs3d_IsoAspect(aColor, Aspect_TOL_SOLID, 1, aIsoValues[1]);
134 aDrawer->SetUIsoAspect(myUIsoAspect);
135 aDrawer->SetVIsoAspect(myVIsoAspect);
137 if (aDrawer->HasOwnPointAspect())
138 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
140 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
142 aDrawer = DynamicHilightAttributes();
143 if (aDrawer.IsNull()) {
144 if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
145 aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
146 aDrawer->SetUIsoAspect(myUIsoAspect);
147 aDrawer->SetVIsoAspect(myVIsoAspect);
148 SetDynamicHilightAttributes(aDrawer);
152 myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
153 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
154 aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
155 aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
156 myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
158 ModuleBase_Tools::setPointBallHighlighting(this);
160 // Define colors for wireframe mode
161 setEdgesDefaultColor();
163 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
164 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
165 Attributes()->UpdatePreviousDeviationCoefficient();
168 //********************************************************************
169 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
171 myAdditionalSelectionPriority = thePriority;
174 //********************************************************************
175 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
177 ViewerData_AISShape::SetColor(theColor);
178 myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
179 setEdgesDefaultColor();
180 myUIsoAspect->SetColor(theColor);
181 myVIsoAspect->SetColor(theColor);
184 void ModuleBase_ResultPrs::setEdgesDefaultColor()
186 if (myResult.get()) {
187 AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
188 bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
190 Handle(Prs3d_Drawer) aDrawer = Attributes();
191 aDrawer->SetFaceBoundaryDraw(Standard_True);
192 aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
195 aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
196 aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
197 aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
199 aDrawer->SetUnFreeBoundaryDraw(Standard_True);
200 aDrawer->SetFreeBoundaryDraw(Standard_True);
201 aDrawer->SetWireDraw(Standard_True);
207 //********************************************************************
208 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
210 TopoDS_Compound aCompound;
211 BRep_Builder aBBuilder;
212 aBBuilder.MakeCompound (aCompound);
214 myHiddenSubShapes = theShapes;
215 collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
216 myVisibleCompound = aCompound;
218 aBBuilder.MakeCompound (aCompound);
219 TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
220 for (; aIt.More(); aIt.Next()) {
221 aBBuilder.Add(aCompound, aIt.Value());
223 myHiddenCompound = aCompound;
226 //********************************************************************
227 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
229 if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
232 // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
233 TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
234 for (; aShapeIt.More(); aShapeIt.Next()) {
235 if (theShape.IsSame(aShapeIt.Value()))
242 //********************************************************************
243 bool ModuleBase_ResultPrs::hasSubShapeVisible(
244 const TopoDS_ListOfShape& theShapesToSkip)
246 TopoDS_Compound aCompound;
247 BRep_Builder aBuilder;
248 aBuilder.MakeCompound (aCompound);
249 TopoDS_ListOfShape aShapesToSkip;
250 TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
251 aShapesToSkip.Append(aHiddenCopy);
252 for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
253 aShapesToSkip.Append(anIt.Value());
255 collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
256 return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
259 //********************************************************************
260 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
262 if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
265 myTransparency = theTransparency;
266 myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
270 //********************************************************************
271 void ModuleBase_ResultPrs::Compute(
272 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
273 const Handle(Prs3d_Presentation)& thePresentation,
274 const Standard_Integer theMode)
276 std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
277 bool aReadyToDisplay = aShapePtr.get();
278 if (aReadyToDisplay) {
279 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
280 if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
281 if (!myOriginalShape.IsNull()) {
282 Set(myOriginalShape);
283 myIsSubstituted = false;
286 else { // convert shape into SHELL
287 bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
288 Set(myVisibleCompound);
289 myIsSubstituted = true;
291 aReadyToDisplay = false;
294 // change deviation coefficient to provide more precise circle
296 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
302 // visualize hidden sub-shapes transparent
303 if (myResult.get()) {
304 if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
306 StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
307 aReadyToDisplay = true;
310 if (!aReadyToDisplay) {
311 Events_InfoMessage("ModuleBase_ResultPrs",
312 "An empty AIS presentation: ModuleBase_ResultPrs").send();
313 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
314 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
319 //********************************************************************
320 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
321 TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
322 const TopoDS_ListOfShape& theHiddenSubShapes)
324 switch (theShape.ShapeType()) {
325 case TopAbs_COMPSOLID:
326 case TopAbs_COMPOUND: {
327 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
328 collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
333 for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
334 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
339 for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
340 collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
345 if (theHiddenSubShapes.Contains(theShape))
346 return; // remove hidden shape
347 theBuilder.Add(theCompound, theShape);
351 case TopAbs_VERTEX: {
352 theBuilder.Add(theCompound, theShape);
359 //********************************************************************
360 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
361 const Standard_Integer theMode)
363 if (appendVertexSelection(aSelection, theMode))
366 if (theMode > TopAbs_SHAPE) {
367 // In order to avoid using custom selection modes
368 if (theMode == ModuleBase_ResultPrs::Sel_Result) {
369 AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
374 // TODO: OCCT issue should be created for the COMPOUND processing
375 // before it is fixed, the next workaround in necessary
376 if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
377 const TopoDS_Shape& aShape = Shape();
378 TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
379 // do not activate in compound mode shapes which do not contain compounds
380 if (!aCompExp.More())
384 // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
385 // // Limit selection area only by actual object (Shape)
386 // ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
387 // if (aCompSolid.get()) {
388 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
389 // if (aShapePtr.get()) {
390 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
391 // int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
392 // /// It is important to have priority for the shape of comp solid result less than priority
393 // /// for the presentation shape which is a sub-result.
394 // /// Reason is to select the sub-objects before: #1592
395 // aPriority = aPriority - 1;
396 // double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
398 // Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
399 // StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
400 // aDeflection, myDrawer->HLRAngle(), 9, 500);
402 // for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
403 // Handle(SelectMgr_EntityOwner) anOwner =
404 // Handle(SelectMgr_EntityOwner)
405 // ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
406 // anOwner->Set(this);
412 AIS_Shape::ComputeSelection(aSelection, theMode);
414 if (myAdditionalSelectionPriority > 0) {
415 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
416 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
419 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
420 Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
421 if (!aBasicsOwner.IsNull())
422 aBasicsOwner->SetPriority(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
427 //********************************************************************
428 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
429 const Standard_Integer /*theMode*/)
431 if (Shape().ShapeType() == TopAbs_VERTEX) {
432 const TopoDS_Shape& aShape = Shape();
434 int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
435 #if OCC_VERSION_HEX > 0x070400
436 double aDeflection = StdPrs_ToolTriangulatedShape::GetDeflection(aShape, myDrawer);
438 double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
440 /// The cause of this method is the last parameter of BRep owner setting into True.
441 /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
442 /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
443 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
444 StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
445 aDeflection, myDrawer->HLRAngle(), 9, 500);
448 NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
449 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
452 Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
453 Handle(SelectMgr_EntityOwner) anOwner = anEntity->BaseSensitive()->OwnerId();
454 anOwner->SetSelectable(this);
462 //********************************************************************
463 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
464 const SelectMgr_SequenceOfOwner& theOwners)
466 Handle(SelectMgr_EntityOwner) anOwner;
467 Handle(ModuleBase_BRepOwner) aCompOwner;
468 for (int i = 1; i <= theOwners.Length(); i++) {
469 anOwner = theOwners.Value(i);
470 aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
471 if (aCompOwner.IsNull()) {
472 thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
475 TopoDS_Shape aShape = aCompOwner->Shape();
476 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
477 aSelectionPrs->Clear();
479 StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
481 aSelectionPrs->SetDisplayPriority(9);
482 aSelectionPrs->Highlight(GetContext()->SelectionStyle());
483 aSelectionPrs->Display();
484 thePM->Color(this, GetContext()->SelectionStyle());
489 //********************************************************************
490 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
491 const Handle(Prs3d_Drawer)& theStyle,
492 const Handle(SelectMgr_EntityOwner)& theOwner)
494 Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
498 TopoDS_Shape aShape = aOwner->Shape();
499 if (!aShape.IsNull()) {
500 thePM->Color(this, theStyle);
502 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
503 aHilightPrs->Clear();
505 StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
506 aHilightPrs->Highlight(theStyle);
508 if (thePM->IsImmediateModeOn())
509 thePM->AddToImmediateList(aHilightPrs);
514 //********************************************************************
515 void ModuleBase_ResultPrs::updateIsoLines()
517 std::vector<int> aIsoValues;
519 ModelAPI_Tools::getIsoLines(myResult, isIsoVisible, aIsoValues);
521 if (aIsoValues.size() == 0) {
522 aIsoValues.push_back(1);
523 aIsoValues.push_back(1);
527 if (aIsoValues.size() == 0) {
528 aIsoValues.push_back(0);
529 aIsoValues.push_back(0);
536 myUIsoAspect->SetNumber(aIsoValues[0]);
537 myVIsoAspect->SetNumber(aIsoValues[1]);