1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "PartSet_ResultSketchPrs.h"
22 #include "PartSet_Tools.h"
23 #include "ModuleBase_Tools.h"
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <GeomAPI_PlanarEdges.h>
31 #include <Events_InfoMessage.h>
32 #include <Events_Loop.h>
34 #include <SketchPlugin_SketchEntity.h>
36 #include <SketcherPrs_Tools.h>
38 #include <Config_PropManager.h>
40 #include <BRep_Builder.hxx>
41 #include <Prs3d_Drawer.hxx>
43 #include <Prs3d_PointAspect.hxx>
44 #include <TopoDS_Builder.hxx>
45 #include <SelectMgr_SelectionManager.hxx>
46 #include <StdPrs_WFShape.hxx>
47 #include <StdSelect_BRepSelectionTool.hxx>
48 #include <Graphic3d_AspectLine3d.hxx>
49 #include <Prs3d_LineAspect.hxx>
50 #include <Graphic3d_AspectMarker3d.hxx>
55 #include <TopTools_IndexedMapOfShape.hxx>
59 //*******************************************************************************************
61 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
63 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
64 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
66 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
68 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
70 Handle(Prs3d_Drawer) aDrawer = Attributes();
71 if (aDrawer->HasOwnPointAspect())
72 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
74 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
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);
83 void PartSet_ResultSketchPrs::Compute(
84 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
85 const Handle(Prs3d_Presentation)& thePresentation,
86 const Standard_Integer theMode)
88 thePresentation->Clear();
90 TopoDS_Shape aResultShape;
91 TopoDS_Compound anAuxiliaryCompound;
92 NCollection_List<TopoDS_Shape> aFaceList;
93 fillShapes(aResultShape, anAuxiliaryCompound, mySketchFaceList);
95 bool aReadyToDisplay = !aResultShape.IsNull() || !anAuxiliaryCompound.IsNull();
97 if (aReadyToDisplay) {
98 if (!aResultShape.IsNull()) {
99 myOriginalShape = aResultShape;
100 if (!myOriginalShape.IsNull())
101 Set(myOriginalShape);
103 myAuxiliaryCompound = anAuxiliaryCompound;
106 setAuxiliaryPresentationStyle(false);
108 // change deviation coefficient to provide more precise circle
109 //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
110 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
112 if (!myAuxiliaryCompound.IsNull()) {
113 setAuxiliaryPresentationStyle(true);
115 Handle(Prs3d_Drawer) aDrawer = Attributes();
116 StdPrs_WFShape::Add(thePresentation, myAuxiliaryCompound, aDrawer);
119 if (!aReadyToDisplay) {
120 Events_InfoMessage("PartSet_ResultSketchPrs",
121 "An empty AIS presentation: PartSet_ResultSketchPrs").send();
122 static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
123 ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
128 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
130 TopTools_IndexedMapOfShape aSubShapes;
131 TopExp::MapShapes (theShape, theType, aSubShapes);
134 isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
135 int anExtent = aSubShapes.Extent();
136 for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
138 const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
144 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
145 const Standard_Integer theMode)
150 aMode != SketcherPrs_Tools::Sel_Sketch_Face &&
151 aMode != SketcherPrs_Tools::Sel_Sketch_Wire)
152 // In order to avoid using custom selection modes
155 bool aShapeIsChanged = false;
156 double aPrevDeviation = Attributes()->DeviationCoefficient();
157 if (aMode == SketcherPrs_Tools::Sel_Sketch_Face ||
158 aMode == SketcherPrs_Tools::Sel_Sketch_Wire) {
159 aMode = (aMode == SketcherPrs_Tools::Sel_Sketch_Face) ? AIS_Shape::SelectionMode(TopAbs_FACE)
160 : AIS_Shape::SelectionMode(TopAbs_WIRE);
162 const TopoDS_Shape& aShape = Shape();
163 debugInfo(aShape, TopAbs_VERTEX); // 24
164 debugInfo(aShape, TopAbs_EDGE); // 12
165 debugInfo(aShape, TopAbs_WIRE); // 0
166 debugInfo(aShape, TopAbs_FACE); // 0
168 BRep_Builder aBuilder;
169 TopoDS_Compound aComp;
170 aBuilder.MakeCompound(aComp);
171 aBuilder.Add(aComp, myOriginalShape);
173 for(NCollection_List<TopoDS_Shape>::Iterator anIt(mySketchFaceList); anIt.More(); anIt.Next()) {
174 const TopoDS_Shape& aFace = anIt.Value();
175 aBuilder.Add(aComp, aFace);
178 debugInfo(aComp, TopAbs_VERTEX); // 24
179 debugInfo(aComp, TopAbs_EDGE); // 12
180 debugInfo(aComp, TopAbs_WIRE); // 4
181 debugInfo(aComp, TopAbs_FACE); // 2
184 double aBodyDefDeflection = Config_PropManager::real("Visualization", "body_deflection");
185 Attributes()->SetDeviationCoefficient(aBodyDefDeflection);
186 aShapeIsChanged = true;
189 Set(myOriginalShape);
191 // append auxiliary compound to selection of edges/vertices
192 if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
193 aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
195 bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
196 appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
199 AIS_Shape::ComputeSelection(aSelection, aMode);
201 if (aShapeIsChanged) {
202 Attributes()->SetDeviationCoefficient(aPrevDeviation);
203 Set(myOriginalShape);
207 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
208 const TopoDS_Shape& theShape,
209 const TopAbs_ShapeEnum& theTypeOfSelection)
211 // POP protection against crash in low layers
212 Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
214 StdSelect_BRepSelectionTool::Load(theSelection,
219 myDrawer->HLRAngle(),
220 myDrawer->IsAutoTriangulation());
221 } catch ( Standard_Failure ) {
225 void PartSet_ResultSketchPrs::setAuxiliaryPresentationStyle(const bool isAuxiliary)
227 Standard_Integer aLineStyle = isAuxiliary?
228 SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY() :
229 SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
231 Handle(Prs3d_Drawer) aDrawer = Attributes();
234 Handle(Prs3d_LineAspect) aLineAspect;
236 Aspect_TypeOfLine aType = (Aspect_TypeOfLine)aLineStyle;
237 if (aDrawer->HasOwnLineAspect()) {
238 aLineAspect = aDrawer->LineAspect();
240 if (aDrawer->HasOwnWireAspect()) {
241 aLineAspect = aDrawer->WireAspect();
243 Quantity_Color aCurrentColor = aLineAspect->Aspect()->Color();
244 Aspect_TypeOfLine aPrevLineType = aLineAspect->Aspect()->Type();
245 Standard_Real aCurrentWidth = aLineAspect->Aspect()->Width();
246 bool isChangedLineType = aType != aPrevLineType;
247 if (isChangedLineType) {
248 Handle(Prs3d_LineAspect) aAspect = new Prs3d_LineAspect(aCurrentColor, aType, aCurrentWidth);
249 aDrawer->SetLineAspect(aAspect);
250 aDrawer->SetWireAspect(aAspect);
251 SetAttributes(aDrawer);
255 void PartSet_ResultSketchPrs::fillShapes(TopoDS_Shape& theResultShape,
256 TopoDS_Compound& theAuxiliaryCompound,
257 NCollection_List<TopoDS_Shape>& theFaceList)
259 //if (!aResultShape.IsNull() || !anAuxiliaryCompound.IsNull())
260 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
265 ResultConstructionPtr aConstruction =
266 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
267 if (aConstruction.get()) {
268 int aFacesNum = aConstruction->facesNum();
269 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
270 std::shared_ptr<GeomAPI_Face> aFaceShape = aConstruction->face(aFaceIndex);
271 if (aFaceShape.get()) {
272 TopoDS_Shape aFace = (aFaceShape)->impl<TopoDS_Shape>();
273 theFaceList.Append(aFace);
277 theResultShape = aShapePtr->impl<TopoDS_Shape>();
279 /// find auxiliary shapes
280 FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
281 CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
283 std::list<ResultPtr> anAuxiliaryResults;
284 for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
285 FeaturePtr aFeature = aSketchFeature->subFeature(i);
286 if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
287 std::list<ResultPtr> aResults = aFeature->results();
288 std::list<ResultPtr>::const_iterator aIt;
289 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
290 ResultPtr aResult = *aIt;
291 if (aResult.get() && aResult->shape().get())
292 anAuxiliaryResults.push_back(aResult);
295 else if (PartSet_Tools::isIncludeIntoSketchResult(aFeature)) {
296 // Append not-edges shapes, e.g. center of a circle, an arc, a point feature.
297 // Issue #2535: do not show elements produced by Projection or Intersection features,
298 // which should not be included into the sketch result.
299 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
300 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
301 for (; aResIter != aRes.cend(); aResIter++) {
302 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
303 ModelAPI_ResultConstruction>(*aResIter);
305 std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
306 if (aGeomShape.get()) {
307 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
308 if (aShape.ShapeType() != TopAbs_EDGE)
309 anAuxiliaryResults.push_back(aConstr);
316 if (anAuxiliaryResults.size() > 0) {
317 BRep_Builder aBuilder;
318 //TopoDS_Compound aComp;
319 aBuilder.MakeCompound(theAuxiliaryCompound);
320 std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
321 aLast = anAuxiliaryResults.end();
322 for (; anIt != aLast; anIt++) {
323 ResultPtr aResult = *anIt;
325 GeomShapePtr aGeomShape = aResult->shape();
326 if (aGeomShape.get()) {
327 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
328 if (!aShape.IsNull())
329 aBuilder.Add(theAuxiliaryCompound, aShape);