Salome HOME
Sources formated according to the codeing standards
[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 <Config_PropManager.h>
23
24 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
25 {
26 }
27
28 void SketchPlugin_ConstraintRadius::initAttributes()
29 {
30   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
31   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
32   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
33 }
34
35 void SketchPlugin_ConstraintRadius::execute()
36 {
37   if (data()->attribute(SketchPlugin_Constraint::ENTITY_A())->isInitialized()
38       && !data()->attribute(SketchPlugin_Constraint::VALUE())->isInitialized()) {
39
40     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
41         ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
42     FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
43     if (aFeature) {
44       double aRadius = 0;
45       boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
46       if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
47         AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
48             aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
49         if (anAttribute)
50           aRadius = anAttribute->value();
51       } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
52         boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
53             GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
54         boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
55             GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
56         if (aCenterAttr && aStartAttr)
57           aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
58       }
59       boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
60           ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
61       aValueAttr->setValue(aRadius);
62     }
63   }
64 }
65
66 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
67 {
68   if (!sketch())
69     return thePrevious;
70
71   boost::shared_ptr<ModelAPI_Data> aData = data();
72   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
73       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
74   if (!anAttr)
75     return thePrevious;
76   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
77   std::string aKind = aFeature ? aFeature->getKind() : "";
78   if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
79     return thePrevious;
80
81   // Flyout point
82   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
83       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
84   if (!aFlyoutAttr->isInitialized())
85     return thePrevious;
86   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
87
88   // Prepare a circle
89   aData = aFeature->data();
90   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
91   double aRadius;
92   if (aKind == SketchPlugin_Circle::ID()) {
93     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
94         aData->attribute(SketchPlugin_Circle::CENTER_ID()));
95     AttributeDoublePtr aCircRadius = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
96         aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
97     aRadius = aCircRadius->value();
98   } else if (aKind == SketchPlugin_Arc::ID()) {
99     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100         aData->attribute(SketchPlugin_Arc::CENTER_ID()));
101     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
102         GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
103     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
104   }
105
106   boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
107   boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
108       sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
109   boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
110   boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
111
112   // Value
113   boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
114       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
115   double aValue = aRadius;
116   if (aValueAttr && aValueAttr->isInitialized())
117     aValue = aValueAttr->value();
118
119   AISObjectPtr anAIS = thePrevious;
120   if (!anAIS)
121     anAIS = AISObjectPtr(new GeomAPI_AISObject);
122   anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
123
124   // Set color from preferences
125   std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
126   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
127   return anAIS;
128 }
129
130 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
131 {
132   boost::shared_ptr<ModelAPI_Data> aData = data();
133   if (!aData->isValid())
134     return;
135
136   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
137       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
138   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
139   if (!aFeature)
140     return;
141   std::string aCenterAttrName;
142   if (aFeature->getKind() == SketchPlugin_Circle::ID())
143     aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
144   else if (aFeature->getKind() == SketchPlugin_Arc::ID())
145     aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
146   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
147       GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
148   boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
149
150   // The specified delta applied on the circle curve, 
151   // so it will be scaled due to distance between flyout and center points
152   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
153       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
154   boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
155
156   boost::shared_ptr<ModelAPI_AttributeDouble> aRadius = boost::dynamic_pointer_cast<
157       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
158   double aScale = aFlyout->distance(aCenter) / aRadius->value();
159
160   boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
161   aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
162   aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
163   aFlyout = aCircle->project(aFlyout);
164
165   aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
166 }