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 "PartSet_ResultSketchPrs.h"
21 #include "PartSet_Tools.h"
22 #include "ModuleBase_Tools.h"
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_ResultBody.h>
28 #include <GeomAPI_PlanarEdges.h>
30 #include <Events_InfoMessage.h>
31 #include <Events_Loop.h>
33 #include <SketchPlugin_SketchEntity.h>
35 #include <SketcherPrs_Tools.h>
37 #include <Config_PropManager.h>
39 #include <BRep_Builder.hxx>
40 #include <Prs3d_Drawer.hxx>
42 #include <Prs3d_PointAspect.hxx>
43 #include <TopoDS_Builder.hxx>
44 #include <SelectMgr_SelectionManager.hxx>
45 #include <StdPrs_WFShape.hxx>
46 #include <StdSelect_BRepSelectionTool.hxx>
47 #include <Graphic3d_AspectLine3d.hxx>
48 #include <Prs3d_LineAspect.hxx>
49 #include <Graphic3d_AspectMarker3d.hxx>
54 #include <TopTools_IndexedMapOfShape.hxx>
58 //*******************************************************************************************
60 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
62 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
63 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
65 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
67 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
69 Handle(Prs3d_Drawer) aDrawer = Attributes();
70 if (aDrawer->HasOwnPointAspect())
71 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
73 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
74 //aDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
76 // Activate individual repaintng if this is a part of compsolid
77 ResultBodyPtr anOwner = ModelAPI_Tools::bodyOwner(myResult);
78 SetAutoHilight(anOwner.get() == NULL);
80 ModuleBase_Tools::setPointBallHighlighting(this);
82 // change deviation coefficient to provide more precise circle
83 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), DynamicHilightAttributes());
84 ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
87 void PartSet_ResultSketchPrs::Compute(
88 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
89 const Handle(Prs3d_Presentation)& thePresentation,
90 const Standard_Integer theMode)
92 thePresentation->Clear();
94 TopoDS_Shape aResultShape;
95 TopoDS_Compound anAuxiliaryCompound;
96 NCollection_List<TopoDS_Shape> aFaceList;
97 fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
99 bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
101 if (aReadyToDisplay) {
102 if (!aResultShape.IsNull()) {
103 myOriginalShape = aResultShape;
104 if (!myOriginalShape.IsNull())
105 Set(myOriginalShape);
107 myAuxiliaryCompound = anAuxiliaryCompound;
110 setAuxiliaryPresentationStyle(false);
113 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
118 if (!myAuxiliaryCompound.IsNull()) {
119 setAuxiliaryPresentationStyle(true);
121 Handle(Prs3d_Drawer) aDrawer = Attributes();
122 StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
125 if (!aReadyToDisplay) {
126 Events_InfoMessage("PartSet_ResultSketchPrs",
127 "An empty AIS presentation: PartSet_ResultSketchPrs").send();
128 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
129 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
134 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
136 TopTools_IndexedMapOfShape aSubShapes;
137 TopExp::MapShapes (theShape, theType, aSubShapes);
140 isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
141 int anExtent = aSubShapes.Extent();
142 for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
144 const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
150 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
151 const Standard_Integer theMode)
156 aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
157 aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
158 // In order to avoid using custom selection modes
161 bool aShapeIsChanged = false;
162 double aPrevDeviation = Attributes()->DeviationCoefficient();
163 if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
164 aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
165 aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
166 : AIS_Shape::SelectionMode(TopAbs_WIRE);
168 const TopoDS_Shape& aShape = Shape();
169 debugInfo(aShape, TopAbs_VERTEX); // 24
170 debugInfo(aShape, TopAbs_EDGE); // 12
171 debugInfo(aShape, TopAbs_WIRE); // 0
172 debugInfo(aShape, TopAbs_FACE); // 0
174 BRep_Builder aBuilder;
175 TopoDS_Compound aComp;
176 aBuilder.MakeCompound(aComp);
177 aBuilder.Add(aComp, myOriginalShape);
179 for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
180 const TopoDS_Shape& aFace = anIt.Value();
181 aBuilder.Add(aComp, aFace);
184 debugInfo(aComp, TopAbs_VERTEX); // 24
185 debugInfo(aComp, TopAbs_EDGE); // 12
186 debugInfo(aComp, TopAbs_WIRE); // 4
187 debugInfo(aComp, TopAbs_FACE); // 2
190 double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_deflection");
191 Attributes()->SetDeviationCoefficient(aBodyDefDeflection);
192 aShapeIsChanged = true;
195 Set(myOriginalShape);
197 // append auxiliary compound to selection of edges/vertices
198 if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
199 aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
201 bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
202 appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
205 AIS_Shape::ComputeSelection(aSelection, aMode);
207 if (aShapeIsChanged) {
208 Attributes()->SetDeviationCoefficient(aPrevDeviation);
209 Set(myOriginalShape);
213 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
214 const TopoDS_Shape& theShape,
215 const TopAbs_ShapeEnum& theTypeOfSelection)
217 // POP protection against crash in low layers
218 Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
220 StdSelect_BRepSelectionTool::Load(theSelection,
225 myDrawer->HLRAngle(),
226 myDrawer->IsAutoTriangulation());
227 } catch ( Standard_Failure ) {
231 void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxiliary)
233 Standard_Integer aLineStyle = isAuxiliary?
234 SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY() :
235 SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
237 Handle(Prs3d_Drawer) aDrawer = Attributes();
240 Handle(Prs3d_LineAspect) aLineAspect;
242 Aspect_TypeOfLine aType = (Aspect_TypeOfLine)aLineStyle;
243 if (aDrawer->HasOwnLineAspect()) {
244 aLineAspect = aDrawer->LineAspect();
246 if (aDrawer->HasOwnWireAspect()) {
247 aLineAspect = aDrawer->WireAspect();
249 Quantity_Color aCurrentColor = aLineAspect->Aspect()->Color();
250 Aspect_TypeOfLine aPrevLineType = aLineAspect->Aspect()->Type();
251 Standard_Real aCurrentWidth = aLineAspect->Aspect()->Width();
252 bool isChangedLineType = aType != aPrevLineType;
253 if (isChangedLineType) {
254 Handle(Prs3d_LineAspect) aAspect = new Prs3d_LineAspect(aCurrentColor, aType, aCurrentWidth);
255 aDrawer->SetLineAspect(aAspect);
256 aDrawer->SetWireAspect(aAspect);
257 SetAttributes(aDrawer);
261 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
262 TopoDS_Compound& theAuxiliaryCompound,
263 NCollection_List<TopoDS_Shape>& theFaceList)
265 //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
266 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
271 ResultConstructionPtr aConstruction =
272 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
273 if (aConstruction.get()) {
274 int aFacesNum = aConstruction->facesNum();
275 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
276 std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
277 if (aFaceShape.get()) {
278 TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
279 theFaceList.Append(aFace);
283 theResultShape = aShapePtr->impl<TopoDS_Shape>();
285 /// find auxiliary shapes
286 FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
287 CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
289 std::list<ResultPtr> anAuxiliaryResults;
290 int aNumberOfSubs = aSketchFeature->numberOfSubs();
291 for (int i = 0; i < aNumberOfSubs; i++) {
292 FeaturePtr aFeature = aSketchFeature->subFeature(i);
293 if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
294 std::list<ResultPtr> aResults = aFeature->results();
295 std::list<ResultPtr>::const_iterator aIt;
296 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
297 ResultPtr aResult = *aIt;
298 if (aResult.get() && aResult->shape().get())
299 anAuxiliaryResults.push_back(aResult);
302 else if (PartSet_Tools::isIncludeIntoSketchResult(aFeature)) {
303 // Append not-edges shapes, e.g. center of a circle, an arc, a point feature.
304 // Issue #2535: do not show elements produced by Projection or Intersection features,
305 // which should not be included into the sketch result.
306 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
307 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
308 for (; aResIter != aRes.cend(); aResIter++) {
309 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
310 ModelAPI_ResultConstruction>(*aResIter);
312 std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
313 if (aGeomShape.get()) {
314 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
315 if (aShape.ShapeType() != TopAbs_EDGE)
316 anAuxiliaryResults.push_back(aConstr);
323 if (anAuxiliaryResults.size() > 0) {
324 BRep_Builder aBuilder;
325 //TopoDS_Compound aComp;
326 aBuilder.MakeCompound(theAuxiliaryCompound);
327 std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
328 aLast = anAuxiliaryResults.end();
329 for (; anIt != aLast; anIt++) {
330 ResultPtr aResult = *anIt;
332 GeomShapePtr aGeomShape = aResult->shape();
333 if (aGeomShape.get()) {
334 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
335 if (!aShape.IsNull())
336 aBuilder.Add(theAuxiliaryCompound, aShape);