]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
bugfix for libSketchSolver.so: removing unresolved symbols from the SketchPlugin
[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 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
68 {
69   if (!sketch())
70     return thePrevious;
71
72   boost::shared_ptr<ModelAPI_Data> aData = data();
73   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
74     boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
75   if (!anAttr)
76     return thePrevious;
77   FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
78   std::string aKind = aFeature ? aFeature->getKind() : "";
79   if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
80     return thePrevious;
81
82   // Flyout point
83   boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
84     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
85   if (!aFlyoutAttr->isInitialized())
86     return thePrevious;
87   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
88
89   // Prepare a circle
90   aData = aFeature->data();
91   boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
92   double aRadius;
93   if (aKind == SketchPlugin_Circle::ID()) {
94     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Circle::CENTER_ID()));
95     AttributeDoublePtr aCircRadius = 
96       boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
97     aRadius = aCircRadius->value();
98   }
99   else if (aKind == SketchPlugin_Arc::ID()) {
100     aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
101     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
102       boost::dynamic_pointer_cast<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 = 
108     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(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 = 
114     boost::dynamic_pointer_cast<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 =
137     boost::dynamic_pointer_cast<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 = 
147     boost::dynamic_pointer_cast<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 =
153     boost::dynamic_pointer_cast<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 =
157     boost::dynamic_pointer_cast<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 }