Salome HOME
Issue #780: Make Arrow Tail size and extension line size adaptive to current viewer...
[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 static const gp_Circ MyDefCirc(gp_Ax2(gp_Pnt(0,0,0), gp_Dir(0,0,1)), 1);
22
23 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
24 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
25
26 SketcherPrs_Radius::SketcherPrs_Radius(ModelAPI_Feature* theConstraint, 
27                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
28 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
29 {
30   myAspect = new Prs3d_DimensionAspect();
31   myAspect->MakeArrows3d(false);
32   myAspect->MakeText3d(false);
33   myAspect->MakeTextShaded(false);
34   myAspect->MakeUnitsDisplayed(false);
35   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
36   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
37   
38   SetDimensionAspect(myAspect);
39   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
40 }
41
42
43 void SketcherPrs_Radius::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
44                                  const Handle(Prs3d_Presentation)& thePresentation, 
45                                  const Standard_Integer theMode)
46 {
47   DataPtr aData = myConstraint->data();
48
49   // Flyout point
50   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
51     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
52     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
53   if (!aFlyoutAttr->isInitialized())
54     return; // can not create a good presentation
55
56   // Get circle
57   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
58     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
59     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
60   if (!anAttr) return;
61
62   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
63   double aRadius = 1;
64   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
65   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
66     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
67         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
68     AttributeDoublePtr aCircRadius = 
69       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
70       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
71     aRadius = aCircRadius->value();
72   } else { // arc
73     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
74         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
75     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
76       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
77       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
78     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
79   }
80   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
81   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->norm();
82
83   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
84     
85   std::shared_ptr<GeomAPI_Pnt> anAnchor = SketcherPrs_Tools::getAnchorPoint(myConstraint, myPlane);
86
87   SetMeasuredGeometry(aCircle.impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
88   SetCustomValue(aRadius);
89
90   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
91   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
92
93   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
94 }
95
96 void SketcherPrs_Radius::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
97                                                    const Standard_Integer theMode)
98 {
99   Standard_Integer aMode;
100   switch (theMode) {
101   case 0: // we should use selection of all objects
102     aMode = 0;
103     break;
104   case SketcherPrs_Tools::Sel_Dimension_All:
105     aMode = 0;
106     break;
107   case SketcherPrs_Tools::Sel_Dimension_Line:
108     aMode = 1;
109     break;
110   case SketcherPrs_Tools::Sel_Dimension_Text:
111     aMode = 2;
112     break;
113   default: {
114     // there are own selection modes, so the others should be ignored
115     // otherwise, the text selection appears in the viewer
116     return; 
117   }
118   }
119   AIS_RadiusDimension::ComputeSelection(aSelection, aMode);
120 }