]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
31 }
32
33 void SketchPlugin_ConstraintRadius::execute()
34 {
35
36 }
37
38 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
39   Handle_AIS_InteractiveObject thePrevious)
40 {
41   Handle(AIS_InteractiveObject) anAIS = thePrevious;
42   if (!sketch())
43     return anAIS;
44
45   boost::shared_ptr<ModelAPI_Data> aData = data();
46   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
47     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
48   if (!anAttr)
49     return anAIS;
50   FeaturePtr aFeature = anAttr->feature();
51   std::string aKind = aFeature ? aFeature->getKind() : "";
52   if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
53     return anAIS;
54
55   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
56           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
57   double aValue = aValueAttr->value();
58
59   // an anchor point
60   boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr = 
61     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
62   boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
63   boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
64
65   aData = aFeature->data();
66   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
67   double aRadius;
68   if (aKind == SKETCH_CIRCLE_KIND) {
69     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
70     AttributeDoublePtr aCircRadius = 
71       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
72     aRadius = aCircRadius->value();
73   }
74   else if (aKind == SKETCH_ARC_KIND) {
75     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
76     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
77       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
78     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
79   }
80
81   // a circle
82   boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
83
84   boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
85     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
86   boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
87
88   boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
89
90   if (anAIS.IsNull())
91   {
92     Handle(AIS_RadiusDimension) aDimAIS = 
93       new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
94     aDimAIS->SetCustomValue(aValue);
95
96     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
97     anAspect->MakeArrows3d (Standard_False);
98     anAspect->MakeText3d(false);
99     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
100     anAspect->MakeTextShaded(false);
101     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
102     aDimAIS->SetDimensionAspect (anAspect);
103     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
104
105     anAIS = aDimAIS;
106   }
107   else
108   {
109     // update presentation
110     Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
111     if (!aDimAIS.IsNull())
112     {
113       aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
114       aDimAIS->SetCustomValue(aValue);
115       aDimAIS->Redisplay(Standard_True);
116     }
117   }
118   return anAIS;
119 }
120