Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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
9 #include <SketchPlugin_ConstraintRadius.h>
10 #include <SketchPlugin_Constraint.h>
11 #include <SketchPlugin_Circle.h>
12 #include <SketchPlugin_Arc.h>
13
14 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Pnt2D.h>
16 #include <GeomAPI_Circ.h>
17 #include <GeomAPI_XYZ.h>
18 #include <ModelAPI_AttributeDouble.h>
19
20 const int CONSTRAINT_TEXT_HEIGHT = 20;  /// the text height of the constraint
21 const int CONSTRAINT_TEXT_SELECTION_TOLERANCE = 20;  /// the text selection tolerance
22
23 static 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(SketchPlugin_Constraint* 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   
38   SetDimensionAspect(myAspect);
39   SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
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   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
57
58   // Get circle
59   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
60     std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>
61     (aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
62   if (!anAttr) return;
63
64   std::shared_ptr<ModelAPI_Feature> aCyrcFeature = ModelAPI_Feature::feature(anAttr->object());
65   double aRadius = 1;
66   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
67   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
68     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
69         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
70     AttributeDoublePtr aCircRadius = 
71       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
72       aCyrcFeature->data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
73     aRadius = aCircRadius->value();
74   } else { // arc
75     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
76         aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
77     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
78       std::dynamic_pointer_cast<GeomDataAPI_Point2D>
79       (aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
80     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
81   }
82   std::shared_ptr<GeomAPI_Pnt> aCenter = myPlane->to3D(aCenterAttr->x(), aCenterAttr->y());
83   std::shared_ptr<GeomAPI_Dir> aNormal = myPlane->norm();
84
85   GeomAPI_Circ aCircle(aCenter, aNormal, aRadius);
86   //gp_Circ aCircle(gp_Ax2(aCenter->impl<gp_Pnt>(), aNormal->impl<gp_Dir>()), aRadius);
87     
88   std::shared_ptr<GeomAPI_Pnt> anAnchor = aCircle.project(aFlyoutPnt);
89   std::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
90   anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
91   std::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
92   const double aDelta = 1e-3;
93   anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
94   anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
95   anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
96
97   SetMeasuredGeometry(aCircle.impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
98   SetCustomValue(aRadius);
99
100   myAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
101   myAspect->ArrowAspect()->SetLength(CONSTRAINT_TEXT_HEIGHT * 2.);
102   AIS_RadiusDimension::Compute(thePresentationManager, thePresentation, theMode);
103 }