]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
391dbd451e43d63421abdf8d4abd1e3bee1eb2c6
[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 <GeomAPI_Circ2d.h>
17 #include <GeomAPI_Dir.h>
18 #include <GeomAPI_XYZ.h>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomDataAPI_Dir.h>
21
22 #include <AIS_InteractiveObject.hxx>
23 #include <AIS_RadiusDimension.hxx>
24
25 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
26 {
27 }
28
29 void SketchPlugin_ConstraintRadius::initAttributes()
30 {
31   data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
32   data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
33   data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
34 }
35
36 void SketchPlugin_ConstraintRadius::execute()
37 {
38   if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
39       !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
40
41     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
42       boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
43     FeaturePtr aFeature = aRef->feature();
44     if (aFeature) {
45       double aRadius = 0;
46       boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
47       if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
48         AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
49                                             (aData->attribute(CIRCLE_ATTR_RADIUS));
50         if (anAttribute)
51           aRadius = anAttribute->value();
52       }
53       else if (aFeature->getKind() == SKETCH_ARC_KIND) {
54         boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
55           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
56         boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
57           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
58         if (aCenterAttr && aStartAttr)
59           aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
60       }
61       boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
62         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
63       aValueAttr->setValue(aRadius);
64     }
65   }
66 }
67
68 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
69   Handle_AIS_InteractiveObject thePrevious)
70 {
71   Handle(AIS_InteractiveObject) anAIS = thePrevious;
72   if (!sketch())
73     return anAIS;
74
75   boost::shared_ptr<ModelAPI_Data> aData = data();
76   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
77     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
78   if (!anAttr)
79     return anAIS;
80   FeaturePtr aFeature = anAttr->feature();
81   std::string aKind = aFeature ? aFeature->getKind() : "";
82   if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
83     return anAIS;
84
85   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
86           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
87   double aValue = aValueAttr->value();
88
89   // an anchor point
90   boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr = 
91     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
92   if (!aAnchorAttr->isInitialized())
93     return anAIS;
94   boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
95   boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
96
97   aData = aFeature->data();
98   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
99   double aRadius;
100   if (aKind == SKETCH_CIRCLE_KIND) {
101     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
102     AttributeDoublePtr aCircRadius = 
103       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
104     aRadius = aCircRadius->value();
105   }
106   else if (aKind == SKETCH_ARC_KIND) {
107     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
108     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
109       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
110     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
111   }
112
113   // a circle
114   boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
115
116   boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
117     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
118   boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
119
120   boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
121
122   anAnchor = aCircle->project(anAnchor);
123   // TODO: a bug in AIS_RadiusDimension:
124   // The anchor point can't be myCirc.Location() - an exception is raised.
125   // But we need exactly this case...
126   // We want to show a radius dimension starting from the circle centre and 
127   // ending at the user-defined point.
128   // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
129   boost::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
130   anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
131   boost::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
132   const double aDelta = 1e-3;
133   anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
134   anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
135   anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
136
137   if (anAIS.IsNull())
138   {
139     Handle(AIS_RadiusDimension) aDimAIS = 
140       new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
141     aDimAIS->SetCustomValue(aValue);
142
143     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
144     anAspect->MakeArrows3d (Standard_False);
145     anAspect->MakeText3d(false);
146     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
147     anAspect->MakeTextShaded(false);
148     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
149     aDimAIS->SetDimensionAspect (anAspect);
150     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
151
152     anAIS = aDimAIS;
153   }
154   else
155   {
156     // update presentation
157     Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
158     if (!aDimAIS.IsNull())
159     {
160       aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
161       aDimAIS->SetCustomValue(aValue);
162       aDimAIS->Redisplay(Standard_True);
163     }
164   }
165   return anAIS;
166 }
167
168 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
169 {
170   boost::shared_ptr<ModelAPI_Data> aData = data();
171   if (!aData->isValid())
172     return;
173
174   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
175     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
176   FeaturePtr aFeature = aRef->feature();
177   if (!aFeature)
178     return;
179   std::string aCenterAttrName;
180   if (aFeature->getKind() == SKETCH_CIRCLE_KIND)
181     aCenterAttrName = CIRCLE_ATTR_CENTER;
182   else if (aFeature->getKind() == SKETCH_ARC_KIND)
183     aCenterAttrName = ARC_ATTR_CENTER;
184   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
185     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
186   boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
187
188   // The specified delta applied on the circle curve, 
189   // so it will be scaled due to distance between flyout and center points
190   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
191     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
192   boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
193
194   boost::shared_ptr<ModelAPI_AttributeDouble> aRadius =
195     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
196   double aScale = aFlyout->distance(aCenter) / aRadius->value();
197
198   boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
199   aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
200   aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
201   aFlyout = aCircle->project(aFlyout);
202
203   aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
204 }