]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
Move generation of AIS presentation into SketchPlugin
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintRadius.cpp
1 // File:    SketchPlugin_ConstraintRadius.cpp
2 // Created: 26 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_ConstraintRadius.h"
6
7 #include <SketchPlugin_Arc.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_Point.h>
10
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_Data.h>
13
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_Circ.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <GeomDataAPI_Dir.h>
18
19 #include <AIS_InteractiveObject.hxx>
20 #include <AIS_RadiusDimension.hxx>
21
22 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
23 {
24 }
25
26 void SketchPlugin_ConstraintRadius::initAttributes()
27 {
28   data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
29   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
30
31   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE, ModelAPI_AttributeDouble::type());
32   data()->addAttribute(SKETCH_CONSTRAINT_ATTR_CIRCLE_POINT, GeomDataAPI_Point2D::type());
33 }
34
35 void SketchPlugin_ConstraintRadius::execute()
36 {
37 }
38
39 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
40   Handle_AIS_InteractiveObject thePrevious)
41 {
42   Handle(AIS_InteractiveObject) anAIS = thePrevious;
43   if (!sketch())
44     return anAIS;
45
46   boost::shared_ptr<ModelAPI_Data> aData = data();
47   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
48     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
49   if (!anAttr)
50     return anAIS;
51   FeaturePtr aFeature = anAttr->feature();
52   std::string aKind = aFeature ? aFeature->getKind() : "";
53   if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
54     return anAIS;
55
56   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
57           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
58   double aValue = aValueAttr->value();
59
60   // an anchor point
61   boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr = 
62     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SKETCH_CONSTRAINT_ATTR_CIRCLE_POINT));
63   boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
64   boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
65
66   aData = aFeature->data();
67   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
68   double aRadius;
69   if (aKind == SKETCH_CIRCLE_KIND) {
70     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
71     AttributeDoublePtr aCircRadius = 
72       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
73     aRadius = aCircRadius->value();
74   }
75   else if (aKind == SKETCH_ARC_KIND) {
76     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
77     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
78       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
79     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
80   }
81
82   // a circle
83   boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
84
85   boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
86     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
87   boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
88
89   boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
90
91   if (anAIS.IsNull())
92   {
93     Handle(AIS_RadiusDimension) aDimAIS = 
94       new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
95     aDimAIS->SetCustomValue(aValue);
96
97     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
98     anAspect->MakeArrows3d (Standard_False);
99     anAspect->MakeText3d(false);
100     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
101     anAspect->MakeTextShaded(false);
102     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
103     aDimAIS->SetDimensionAspect (anAspect);
104     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
105
106     anAIS = aDimAIS;
107   }
108   else
109   {
110     // update presentation
111     Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
112     if (!aDimAIS.IsNull())
113     {
114       aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
115       aDimAIS->SetCustomValue(aValue);
116       aDimAIS->Redisplay(Standard_True);
117     }
118   }
119   return anAIS;
120 }
121