1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_ResultSketchPrs.h
4 // Created: 25 March 2015
5 // Author: Natalia ERMOLAEVA
7 #include "PartSet_ResultSketchPrs.h"
8 #include "PartSet_Tools.h"
9 #include "ModuleBase_Tools.h"
11 #include <ModelAPI_Tools.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <GeomAPI_PlanarEdges.h>
15 #include <Events_Error.h>
17 #include <SketchPlugin_SketchEntity.h>
19 #include <Config_PropManager.h>
21 #include <BRep_Builder.hxx>
22 #include <Prs3d_Drawer.hxx>
24 #include <Prs3d_PointAspect.hxx>
25 #include <TopoDS_Builder.hxx>
26 #include <SelectMgr_SelectionManager.hxx>
27 #include <StdPrs_WFDeflectionShape.hxx>
28 #include <StdSelect_BRepSelectionTool.hxx>
29 #include <Graphic3d_AspectLine3d.hxx>
30 #include <Prs3d_LineAspect.hxx>
35 #include <TopTools_IndexedMapOfShape.hxx>
39 //*******************************************************************************************
41 IMPLEMENT_STANDARD_HANDLE(PartSet_ResultSketchPrs, ViewerData_AISShape);
42 IMPLEMENT_STANDARD_RTTIEXT(PartSet_ResultSketchPrs, ViewerData_AISShape);
44 PartSet_ResultSketchPrs::PartSet_ResultSketchPrs(ResultPtr theResult)
45 : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
47 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
49 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
51 Handle(Prs3d_Drawer) aDrawer = Attributes();
52 if (aDrawer->HasOwnPointAspect())
53 aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
55 aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
57 // Activate individual repaintng if this is a part of compsolid
58 ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
59 SetAutoHilight(aCompSolid.get() == NULL);
62 bool PartSet_ResultSketchPrs::isValidShapeType(const TopAbs_ShapeEnum& theBaseType,
63 const TopAbs_ShapeEnum& theCheckedType)
65 bool aValid = theBaseType == theCheckedType;
67 // currently this functionality is for all, as we have no separate wire selection mode
68 // lately it should be corrected to have the following check only for sketch presentations
69 aValid = theBaseType == TopAbs_FACE && theCheckedType == TopAbs_WIRE;
74 void PartSet_ResultSketchPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75 const Handle(Prs3d_Presentation)& thePresentation,
76 const Standard_Integer theMode)
78 thePresentation->Clear();
80 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
82 Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
87 std::vector<int> aColor;
88 aColor = Config_PropManager::color("Visualization", "result_construction_color",
89 ModelAPI_ResultConstruction::DEFAULT_COLOR());
90 Standard_Real anAuxiliaryWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
91 Standard_Integer anAuxiliaryLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
93 Handle(Prs3d_Drawer) aDrawer = Attributes();
94 setColor(aDrawer, Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB));
96 //thePresentation->Clear();
97 setWidth(aDrawer, anAuxiliaryWidth);
99 Handle(Prs3d_LineAspect) aLineAspect;
101 Aspect_TypeOfLine aType = (Aspect_TypeOfLine)anAuxiliaryLineStyle;
102 if (aDrawer->HasOwnLineAspect()) {
103 aLineAspect = aDrawer->LineAspect();
105 if (aDrawer->HasOwnWireAspect()) {
106 aLineAspect = aDrawer->WireAspect();
108 Quantity_Color aCurrentColor;
109 Aspect_TypeOfLine aPrevLineType;
110 Standard_Real aCurrentWidth;
111 aLineAspect->Aspect()->Values(aCurrentColor, aPrevLineType, aCurrentWidth);
112 bool isChangedLineType = aType != aPrevLineType;
113 if (isChangedLineType) {
114 aLineAspect->SetTypeOfLine(aType);
116 // end of set line style
120 ResultConstructionPtr aConstruction =
121 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
122 if (aConstruction.get()) {
123 int aFacesNum = aConstruction->facesNum();
124 for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
125 myFacesList.push_back(aConstruction->face(aFaceIndex));
129 myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
130 if (!myOriginalShape.IsNull()) {
131 Set(myOriginalShape);
133 // change deviation coefficient to provide more precise circle
134 ModuleBase_Tools::setDefaultDeviationCoefficient(myOriginalShape, Attributes());
135 AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
138 Events_Error::throwException("An empty AIS presentation: PartSet_ResultSketchPrs");
141 // create auxiliary shapes
142 FeaturePtr aResultFeature = ModelAPI_Feature::feature(myResult);
143 CompositeFeaturePtr aSketchFeature = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>
145 std::list<ResultPtr> anAuxiliaryResults;
146 /// append auxiliary shapes
147 for (int i = 0; i < aSketchFeature->numberOfSubs(); i++) {
148 FeaturePtr aFeature = aSketchFeature->subFeature(i);
149 if (PartSet_Tools::isAuxiliarySketchEntity(aFeature)) {
150 std::list<ResultPtr> aResults = aFeature->results();
151 std::list<ResultPtr>::const_iterator aIt;
152 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
153 ResultPtr aResult = *aIt;
154 if (aResult.get() && aResult->shape().get())
155 anAuxiliaryResults.push_back(aResult);
159 /// append not-edges shapes, e.g. center of a circle, an arc, a point feature
160 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
161 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
162 for (; aResIter != aRes.cend(); aResIter++) {
163 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
164 ModelAPI_ResultConstruction>(*aResIter);
166 std::shared_ptr<GeomAPI_Shape> aGeomShape = aConstr->shape();
167 if (aGeomShape.get()) {
168 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
169 if (aShape.ShapeType() != TopAbs_EDGE)
170 anAuxiliaryResults.push_back(aConstr);
177 if (anAuxiliaryResults.size() > 0) {
178 Quantity_Color aPrevColor;
180 Standard_Real aPrevWidth = Width();
182 std::vector<int> aColor;
183 aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color",
184 SKETCH_AUXILIARY_COLOR);
185 Standard_Real anAuxiliaryWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
186 Standard_Integer anAuxiliaryLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
188 Handle(Prs3d_Drawer) aDrawer = Attributes();
189 setColor(aDrawer, Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB));
191 //thePresentation->Clear();
192 setWidth(aDrawer, anAuxiliaryWidth);
194 Handle(Prs3d_LineAspect) aLineAspect;
196 Aspect_TypeOfLine aType = (Aspect_TypeOfLine)anAuxiliaryLineStyle;
197 if (aDrawer->HasOwnLineAspect()) {
198 aLineAspect = aDrawer->LineAspect();
200 if (aDrawer->HasOwnWireAspect()) {
201 aLineAspect = aDrawer->WireAspect();
203 Quantity_Color aCurrentColor;
204 Aspect_TypeOfLine aPrevLineType;
205 Standard_Real aCurrentWidth;
206 aLineAspect->Aspect()->Values(aCurrentColor, aPrevLineType, aCurrentWidth);
207 bool isChangedLineType = aType != aPrevLineType;
208 if (isChangedLineType) {
209 aLineAspect->SetTypeOfLine(aType);
211 // end of set line style
213 BRep_Builder aBuilder;
214 TopoDS_Compound aComp;
215 aBuilder.MakeCompound(aComp);
217 std::list<ResultPtr>::const_iterator anIt = anAuxiliaryResults.begin(),
218 aLast = anAuxiliaryResults.end();
219 for (; anIt != aLast; anIt++) {
220 ResultPtr aResult = *anIt;
222 GeomShapePtr aGeomShape = aResult->shape();
223 if (aGeomShape.get()) {
224 const TopoDS_Shape& aShape = aGeomShape->impl<TopoDS_Shape>();
225 if (!aShape.IsNull())
226 aBuilder.Add(aComp, aShape);
230 myAuxiliaryCompound = aComp;
231 StdPrs_WFDeflectionShape::Add(thePresentation, aComp, aDrawer);
233 // restore presentation properties
234 //SetColor(aPrevColor);
235 //setWidth(aDrawer, aPrevWidth);
236 //if (isChangedLineType) {
237 // aLineAspect->SetTypeOfLine(aPrevLineType);
243 void debugInfo(const TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType)
245 TopTools_IndexedMapOfShape aSubShapes;
246 TopExp::MapShapes (theShape, theType, aSubShapes);
248 Standard_Boolean isComesFromDecomposition = !((aSubShapes.Extent() == 1) && (theShape == aSubShapes (1)));
249 int anExtent = aSubShapes.Extent();
250 for (Standard_Integer aShIndex = 1; aShIndex <= aSubShapes.Extent(); ++aShIndex)
252 const TopoDS_Shape& aSubShape = aSubShapes (aShIndex);
258 void PartSet_ResultSketchPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
259 const Standard_Integer aMode)
262 // In order to avoid using custom selection modes
265 if (aMode == AIS_Shape::SelectionMode(TopAbs_FACE)) {
267 const TopoDS_Shape& aShape = Shape();
268 debugInfo(aShape, TopAbs_VERTEX); // 24
269 debugInfo(aShape, TopAbs_EDGE); // 12
270 debugInfo(aShape, TopAbs_WIRE); // 0
271 debugInfo(aShape, TopAbs_FACE); // 0
273 BRep_Builder aBuilder;
274 TopoDS_Compound aComp;
275 aBuilder.MakeCompound(aComp);
276 aBuilder.Add(aComp, myOriginalShape);
277 std::list<std::shared_ptr<GeomAPI_Shape>>::const_iterator aIt;
278 for (aIt = myFacesList.cbegin(); aIt != myFacesList.cend(); ++aIt) {
279 TopoDS_Shape aFace = (*aIt)->impl<TopoDS_Shape>();
280 aBuilder.Add(aComp, aFace);
281 // for sketch presentation in the face mode wires should be selectable also
282 // accoring to #1343 Improvement of Extrusion and Revolution operations
283 appendShapeSelection(aSelection, aFace, TopAbs_WIRE);
286 debugInfo(aComp, TopAbs_VERTEX); // 24
287 debugInfo(aComp, TopAbs_EDGE); // 12
288 debugInfo(aComp, TopAbs_WIRE); // 4
289 debugInfo(aComp, TopAbs_FACE); // 2
293 Set(myOriginalShape);
295 // append auxiliary compound to selection of edges/vertices
296 if (aMode == AIS_Shape::SelectionMode(TopAbs_EDGE) ||
297 aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX)) {
299 bool isVertex = aMode == AIS_Shape::SelectionMode(TopAbs_VERTEX);
300 appendShapeSelection(aSelection, myAuxiliaryCompound, isVertex ? TopAbs_VERTEX : TopAbs_EDGE);
303 AIS_Shape::ComputeSelection(aSelection, aMode);
306 void PartSet_ResultSketchPrs::appendShapeSelection(const Handle(SelectMgr_Selection)& theSelection,
307 const TopoDS_Shape& theShape,
308 const TopAbs_ShapeEnum& theTypeOfSelection)
310 // POP protection against crash in low layers
311 Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
313 StdSelect_BRepSelectionTool::Load(theSelection,
318 myDrawer->HLRAngle(),
319 myDrawer->IsAutoTriangulation());
320 } catch ( Standard_Failure ) {