]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintRadius.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintRadius.cpp
4 // Created: 26 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintRadius.h"
8
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Point.h>
12
13 #include <SketcherPrs_Factory.h>
14
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_Data.h>
17
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Circ.h>
20 #include <GeomAPI_Circ2d.h>
21 #include <GeomAPI_Dir.h>
22 #include <GeomAPI_XYZ.h>
23 #include <GeomDataAPI_Point2D.h>
24 #include <GeomDataAPI_Dir.h>
25
26 #include <Config_PropManager.h>
27
28 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
29 {
30 }
31
32 void SketchPlugin_ConstraintRadius::initAttributes()
33 {
34   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
35   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
36   data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
37 }
38
39 void SketchPlugin_ConstraintRadius::execute()
40 {
41   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
42       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
43   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
44   if (aFeature) {
45     double aRadius = 0;
46     std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
47     if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
48       AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
49           aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
50       if (anAttribute)
51         aRadius = anAttribute->value();
52     } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
53       std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
54           GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
55       std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
56           GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
57       if (aCenterAttr && aStartAttr)
58         aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
59     }
60     //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
61     //    ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
62     //if(!aValueAttr->isInitialized()) {
63     //  aValueAttr->setValue(aRadius);
64     //}
65
66     // the value should to be computed here, not in the getAISObject in order to change the model value
67     // inside the object transaction. This is important for creating a constraint by preselection.
68     // The display of the presentation in this case happens after the transaction commit
69     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
70         GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
71     if (!aFlyoutAttr->isInitialized())
72       compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
73   }
74 }
75
76 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
77 {
78   if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
79     return false;
80
81   std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
82   double aRadius = circleRadius(aCyrcFeature);
83   if (aRadius < 0)
84     return false;
85
86   // Flyout point
87   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
88       GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
89   // Prepare a circle
90   if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
91     std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
92         aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
93     double aShift = aRadius * 1.1;
94     std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
95     std::shared_ptr<GeomAPI_Pnt2d> aFPnt = 
96       std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
97     aFlyoutAttr->setValue(aFPnt);
98   } else { // arc
99     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
100       GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));      
101     aFlyoutAttr->setValue(aStartAttr->pnt());
102   }
103   return true;
104 }
105
106 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
107 {
108   static const double kErrorResult = -1.;
109   if (!sketch())
110     return kErrorResult;
111
112   std::shared_ptr<ModelAPI_Data> aData = data();
113   std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
114       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
115   if (!anAttr)
116     return kErrorResult;
117   theCirc = ModelAPI_Feature::feature(anAttr->object());
118   std::string aKind = theCirc ? theCirc->getKind() : "";
119   if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
120     return kErrorResult;
121
122   DataPtr aCircData = theCirc->data();
123   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
124   if (aKind == SketchPlugin_Circle::ID()) {
125     AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
126         aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
127     return aCircRadius->value();
128   } else {
129     aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
130         aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
131     std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
132         GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
133     return aCenterAttr->pnt()->distance(aStartAttr->pnt());
134   }
135   return kErrorResult;
136 }
137
138 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
139 {
140   if (!sketch())
141     return thePrevious;
142
143   AISObjectPtr anAIS = thePrevious;
144   if (!anAIS) {
145     anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane());
146   }
147
148   // Set color from preferences
149   std::vector<int> aRGB = Config_PropManager::color("Visualization", "sketch_dimension_color",
150                                                     SKETCH_DIMENSION_COLOR);
151   anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
152   return anAIS;
153 }
154
155 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
156 {
157   std::shared_ptr<ModelAPI_Data> aData = data();
158   if (!aData->isValid())
159     return;
160
161   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
162       ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
163   FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
164   if (!aFeature)
165     return;
166   std::string aCenterAttrName;
167   if (aFeature->getKind() == SketchPlugin_Circle::ID())
168     aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
169   else if (aFeature->getKind() == SketchPlugin_Arc::ID())
170     aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
171   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
172       GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
173   std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
174
175   // The specified delta applied on the circle curve, 
176   // so it will be scaled due to distance between flyout and center points
177   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
178       GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
179   std::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
180
181   std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
182       ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
183   double aScale = aFlyout->distance(aCenter) / aRadius->value();
184
185   std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
186   aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
187   aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
188   aFlyout = aCircle->project(aFlyout);
189
190   aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
191 }
192
193 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
194   if (theID == SketchPlugin_Constraint::ENTITY_A()) {
195     std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
196         ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
197     if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
198       std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
199       double aRadius = circleRadius(aCyrcFeature);
200       if (aRadius > 0) { // set as value the radius of updated reference to another circle
201         aValueAttr->setValue(aRadius);
202       }
203     }
204   }
205 }