Salome HOME
#1119 Confirmation box for deleting parts
[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 <Events_Error.h>
16
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Circ.h>
20 #include <GeomAPI_XYZ.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 #include <gp_Circ.hxx>
24
25 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
26
27 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
28 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
29
30 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
31                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
32 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
33 {
34   // Set default values of the presentation
35   myAspect = new Prs3d_DimensionAspect();
36   myAspect->MakeArrows3d(false);
37   myAspect->MakeText3d(false);
38   myAspect->MakeTextShaded(false);
39   myAspect->MakeUnitsDisplayed(false);
40   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
41   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
42   
43   SetDimensionAspect(myAspect);
44   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
45 }
46
47 bool SketcherPrs_Radius::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
48                                           const std::shared_ptr<GeomAPI_Ax3>& thePlane)
49 {
50   bool aReadyToDisplay = false;
51
52   DataPtr aData = theConstraint->data();
53
54   // Flyout point
55   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
56     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
57     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
58   if (!aFlyoutAttr->isInitialized()) {
59     return aReadyToDisplay; // can not create a good presentation
60   }
61
62   // Get circle
63   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
64     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
65     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
66   if (!anAttr)
67     return aReadyToDisplay;
68
69   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
70   double aRadius = 1;
71   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
72   // it is possible that circle result becomes zero, in this case the presentation should disappear
73   // for example, it happens when circle radius is set to zero
74   if (!aCyrcFeature)
75     return aReadyToDisplay;
76   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
77     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
78         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
79     AttributeDoublePtr aCircRadius = 
80       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
81       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
82     aRadius = aCircRadius->value();
83   } else { // arc
84     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
85         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
86     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
87       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
88       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
89     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
90   }
91   std::shared_ptr<GeomAPI_Pnt> aCenter = thePlane->to3D(aCenterAttr->x(), aCenterAttr->y());
92   std::shared_ptr<GeomAPI_Dir> aNormal = thePlane->normal();
93
94   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
95     
96   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(theConstraint, thePlane);
97
98   gp_Circ aCirc = aCircle.impl<gp_Circ>();
99   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
100   // anchor point should not coincide to the location point of the circle
101   // OCCT does not process this case.
102
103   aReadyToDisplay = anAncorPnt.Distance(aCirc.Location()) > 1e-7;
104   return aReadyToDisplay;
105 }
106
107 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
108                                  const Handle(Prs3d_Presentation)& thePresentation, 
109                                  const Standard_Integer theMode)
110 {
111   if (!IsReadyToDisplay(myConstraint, myPlane)) {
112     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Radius");
113     return;
114   }
115
116   DataPtr aData = myConstraint->data();
117
118   // Flyout point
119   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
120     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
121     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
122
123   // Get circle
124   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
125     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
126     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
127
128   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
129   double aRadius = 1;
130   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
131   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
132     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
134     AttributeDoublePtr aCircRadius = 
135       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
136       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
137     aRadius = aCircRadius->value();
138   } else { // arc
139     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
140         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
141     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
142       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
143       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
144     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
145   }
146   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
147   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->normal();
148
149   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
150     
151   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
152
153   gp_Circ aCirc = aCircle.impl<gp_Circ>();
154   gp_Pnt anAncorPnt = anAnchor->impl<gp_Pnt>();
155
156   SetMeasuredGeometry(aCirc, anAncorPnt);
157   SetCustomValue(aRadius);
158
159   // Update variable aspect parameters (depending on viewer scale)
160   double anArrowLength = myAspect->ArrowAspect()->Length();
161    // This is not realy correct way to get viewer scale.
162   double aViewerScale = (double) SketcherPrs_Tools::getDefaultArrowSize() / anArrowLength;
163   double aDimensionValue = GetValue();
164   double aTextSize = 0.0;
165   GetValueString(aTextSize);
166
167   if(aTextSize > ((aDimensionValue - 2 * SketcherPrs_Tools::getArrowSize()) * aViewerScale)) {
168     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Left);
169     myAspect->SetArrowOrientation(Prs3d_DAO_External);
170     myAspect->SetExtensionSize(aTextSize / aViewerScale - SketcherPrs_Tools::getArrowSize() / 2.0);
171   } else {
172     myAspect->SetTextHorizontalPosition(Prs3d_DTHP_Center);
173     myAspect->SetArrowOrientation(Prs3d_DAO_Internal);
174   }
175   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
176
177   // The value of vertical aligment is sometimes changed
178   myAspect->TextAspect()->SetVerticalJustification(Graphic3d_VTA_CENTER);
179
180   AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_Constraint::VALUE());
181   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
182
183   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
184 }
185
186 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
187                                                    const Standard_Integer theMode)
188 {
189   // Map the application selection modes to standard ones
190   Standard_Integer aMode;
191   switch (theMode) {
192   case 0: // we should use selection of all objects
193     aMode = 0;
194     break;
195   case SketcherPrs_Tools::Sel_Dimension_All:
196     aMode = 0;
197     break;
198   case SketcherPrs_Tools::Sel_Dimension_Line:
199     aMode = 1;
200     break;
201   case SketcherPrs_Tools::Sel_Dimension_Text:
202     aMode = 2;
203     break;
204   default: {
205     // there are own selection modes, so the others should be ignored
206     // otherwise, the text selection appears in the viewer
207     return; 
208   }
209   }
210   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
211 }