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