Salome HOME
Set dimensions arrows independent on zoom
[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 static const double MyTextHeight = 20;
23
24 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Radius, AIS_RadiusDimension);
25 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Radius, AIS_RadiusDimension);
26
27 SketcherPrs_Radius::SketcherPrs_Radius(SketchPlugin_Constraint* theConstraint, 
28                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane)
29 : AIS_RadiusDimension(MyDefCirc), myConstraint(theConstraint), myPlane(thePlane)
30 {
31   myAspect = new Prs3d_DimensionAspect();
32   myAspect->MakeArrows3d(false);
33   myAspect->MakeText3d(false);
34   myAspect->MakeTextShaded(false);
35   myAspect->MakeUnitsDisplayed(false);
36   myAspect->TextAspect()->SetHeight(MyTextHeight);
37   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
38   
39   SetDimensionAspect(myAspect);
40   SetSelToleranceForText2d(MyTextHeight);
41 }
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   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
58
59   // Get circle
60   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
61     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
62     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
63   if (!anAttr) return;
64
65   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
66   double aRadius = 1;
67   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
68   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
69     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
70         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
71     AttributeDoublePtr aCircRadius = 
72       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
73       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
74     aRadius = aCircRadius->value();
75   } else { // arc
76     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
77         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
78     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
79       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
80       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
81     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
82   }
83   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
84   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->norm();
85
86   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
87   //gp_Circ aCircle(gp_Ax2(aCenter->impl<gp_Pnt>(), aNormal->impl<gp_Dir>()), aRadius);
88     
89   std::shared_ptr<GeomAPI_Pnt> anAnchor = aCircle.project(aFlyoutPnt);
90   std::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
91   anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
92   std::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
93   const double aDelta = 1e-3;
94   anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
95   anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
96   anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
97
98   SetMeasuredGeometry(aCircle.impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
99   SetCustomValue(aRadius);
100
101   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
102 }