Salome HOME
Fix for a crash: Create sketch, circle, Radius constraint with 0 value.
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Radius.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Radius.cpp
4 // Created:     26 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Radius.h"
8 #include "SketcherPrs_Tools.h"
9
10 #include <SketchPlugin_ConstraintRadius.h>
11 #include <SketchPlugin_Constraint.h>
12 #include <SketchPlugin_Circle.h>
13 #include <SketchPlugin_Arc.h>
14
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Circ.h>
18 #include <GeomAPI_XYZ.h>
19 #include <ModelAPI_AttributeDouble.h>
20
21 #include <gp_Circ.hxx>
22
23 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
24
25 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
26 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
27
28 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
29                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
30 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
31 {
32   myAspect = new Prs3d_DimensionAspect();
33   myAspect->MakeArrows3d(false);
34   myAspect->MakeText3d(false);
35   myAspect->MakeTextShaded(false);
36   myAspect->MakeUnitsDisplayed(false);
37   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
38   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
39   
40   SetDimensionAspect(myAspect);
41   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
42 }
43
44 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
45                                  const Handle(Prs3d_Presentation)& thePresentation, 
46                                  const Standard_Integer theMode)
47 {
48   DataPtr aData = myConstraint->data();
49
50   // Flyout point
51   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
52     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
53     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
54   if (!aFlyoutAttr->isInitialized())
55     return; // can not create a good presentation
56
57   // Get circle
58   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
59     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
60     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
61   if (!anAttr) return;
62
63   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
64   double aRadius = 1;
65   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
66   // it is possible that circle result becomes zero, in this case the presentation should disappear
67   // for example, it happens when circle radius is set to zero
68   if (!aCyrcFeature)
69     return;
70   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
71     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
72         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
73     AttributeDoublePtr aCircRadius = 
74       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
75       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
76     aRadius = aCircRadius->value();
77   } else { // arc
78     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
79         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
80     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
81       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
82       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
83     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
84   }
85   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
86   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->norm();
87
88   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
89     
90   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
91
92   gp_Circ aCirc = aCircle.impl<gp_Circ>();
93   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
94   // anchor point should not coincide to the location point of the circle
95   // OCCT does not process this case.
96   if (anAncorPnt.Distance(aCirc.Location()) < 1e-7)
97     return;
98   SetMeasuredGeometry(aCirc, anAncorPnt);
99   SetCustomValue(aRadius);
100
101   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
102   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
103
104   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
105 }
106
107 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
108                                                    const Standard_Integer theMode)
109 {
110   Standard_Integer aMode;
111   switch (theMode) {
112   case 0: // we should use selection of all objects
113     aMode = 0;
114     break;
115   case SketcherPrs_Tools::Sel_Dimension_All:
116     aMode = 0;
117     break;
118   case SketcherPrs_Tools::Sel_Dimension_Line:
119     aMode = 1;
120     break;
121   case SketcherPrs_Tools::Sel_Dimension_Text:
122     aMode = 2;
123     break;
124   default: {
125     // there are own selection modes, so the others should be ignored
126     // otherwise, the text selection appears in the viewer
127     return; 
128   }
129   }
130   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
131 }