]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git into BR_PYTHON_PLUGIN
[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   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::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     std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
43     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
44       AttributeDoublePtr anAttribute = std::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       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
50           GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
51       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::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     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::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   std::shared_ptr<ModelAPI_Data> aData = data();
70   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::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   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
81       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
82   std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt;
83   if (aFlyoutAttr->isInitialized()) {
84     aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
85   } 
86
87   // Prepare a circle
88   aData = aFeature->data();
89   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
90   double aRadius;
91   if (aKind == SketchPlugin_Circle::ID()) {
92     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93         aData->attribute(SketchPlugin_Circle::CENTER_ID()));
94     AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
95         aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
96     aRadius = aCircRadius->value();
97     if (!aFlyoutPnt) {
98       double aShift = aRadius * 1.1;
99       std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
100       std::shared_ptr<GeomAPI_Pnt2d> aFPnt = 
101         std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
102       aFlyoutAttr->setValue(aFPnt);
103       aFlyoutPnt = sketch()->to3D(aFPnt->x(), aFPnt->y());
104     }
105   } else if (aKind == SketchPlugin_Arc::ID()) {
106     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
107         aData->attribute(SketchPlugin_Arc::CENTER_ID()));
108     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
109         GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
110     aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
111     if (!aFlyoutPnt) {
112       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
113         GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));      
114       aFlyoutAttr->setValue(aStartAttr->pnt());
115       aFlyoutPnt = sketch()->to3D(aStartAttr->pnt()->x(), aStartAttr->pnt()->y());
116     }
117   }
118
119   std::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
120   std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
121       sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
122   std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
123   std::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
124
125   // Value
126   std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
127       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
128   double aValue = aRadius;
129   if (aValueAttr && aValueAttr->isInitialized())
130     aValue = aValueAttr->value();
131
132   AISObjectPtr anAIS = thePrevious;
133   if (!anAIS)
134     anAIS = AISObjectPtr(new GeomAPI_AISObject);
135   anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
136
137   // Set color from preferences
138   std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
139   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
140   return anAIS;
141 }
142
143 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
144 {
145   std::shared_ptr<ModelAPI_Data> aData = data();
146   if (!aData->isValid())
147     return;
148
149   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
150       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
151   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
152   if (!aFeature)
153     return;
154   std::string aCenterAttrName;
155   if (aFeature->getKind() == SketchPlugin_Circle::ID())
156     aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
157   else if (aFeature->getKind() == SketchPlugin_Arc::ID())
158     aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
159   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
160       GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
161   std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
162
163   // The specified delta applied on the circle curve, 
164   // so it will be scaled due to distance between flyout and center points
165   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
166       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
167   std::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
168
169   std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
170       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
171   double aScale = aFlyout->distance(aCenter) / aRadius->value();
172
173   std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
174   aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
175   aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
176   aFlyout = aCircle->project(aFlyout);
177
178   aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
179 }