Salome HOME
645ecbf6949b5219187616f4f4ba8246a7673287
[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   if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
36       !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
37
38     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
39       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
40     FeaturePtr aFeature = aRef->feature();
41     if (aFeature) {
42       double aRadius = 0;
43       boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
44       if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
45         AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
46                                             (aData->attribute(CIRCLE_ATTR_RADIUS));
47         if (anAttribute)
48           aRadius = anAttribute->value();
49       }
50       else if (aFeature->getKind() == SKETCH_ARC_KIND) {
51         boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
52           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
53         boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
54           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
55         if (aCenterAttr && aStartAttr)
56           aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
57       }
58       boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
59         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
60       aValueAttr->setValue(aRadius);
61     }
62   }
63 }
64
65 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
66   Handle_AIS_InteractiveObject thePrevious)
67 {
68   Handle(AIS_InteractiveObject) anAIS = thePrevious;
69   if (!sketch())
70     return anAIS;
71
72   boost::shared_ptr<ModelAPI_Data> aData = data();
73   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
74     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
75   if (!anAttr)
76     return anAIS;
77   FeaturePtr aFeature = anAttr->feature();
78   std::string aKind = aFeature ? aFeature->getKind() : "";
79   if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
80     return anAIS;
81
82   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
83           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
84   double aValue = aValueAttr->value();
85
86   // an anchor point
87   boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr = 
88     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
89   if (!aAnchorAttr->isInitialized())
90     return anAIS;
91   boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
92   boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
93
94   aData = aFeature->data();
95   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
96   double aRadius;
97   if (aKind == SKETCH_CIRCLE_KIND) {
98     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
99     AttributeDoublePtr aCircRadius = 
100       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
101     aRadius = aCircRadius->value();
102   }
103   else if (aKind == SKETCH_ARC_KIND) {
104     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
105     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
106       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
107     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
108   }
109
110   // a circle
111   boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
112
113   boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
114     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
115   boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
116
117   boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
118
119   anAnchor = aCircle->project(anAnchor);
120   // TODO: a bug in AIS_RadiusDimension:
121   // The anchor point can't be myCirc.Location() - an exception is raised.
122   // But we need exactly this case...
123   // We want to show a radius dimension starting from the circle centre and 
124   // ending at the user-defined point.
125   // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
126   gp_Pnt anAPnt = anAnchor->impl<gp_Pnt>();
127   double aDelta = 1/1000.0;
128   if (anAnchor->x() != 0)
129     anAnchor->setX(anAnchor->x() + aDelta);
130   if (anAnchor->y() != 0)
131     anAnchor->setY(anAnchor->y() + aDelta);
132   if (anAnchor->z() != 0)
133     anAnchor->setZ(anAnchor->z() + aDelta);
134
135   if (anAIS.IsNull())
136   {
137     Handle(AIS_RadiusDimension) aDimAIS = 
138       new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
139     aDimAIS->SetCustomValue(aValue);
140
141     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
142     anAspect->MakeArrows3d (Standard_False);
143     anAspect->MakeText3d(false);
144     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
145     anAspect->MakeTextShaded(false);
146     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
147     aDimAIS->SetDimensionAspect (anAspect);
148     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
149
150     anAIS = aDimAIS;
151   }
152   else
153   {
154     // update presentation
155     Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
156     if (!aDimAIS.IsNull())
157     {
158       aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
159       aDimAIS->SetCustomValue(aValue);
160       aDimAIS->Redisplay(Standard_True);
161     }
162   }
163   return anAIS;
164 }
165