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