1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintRadius.cpp
4 // Created: 26 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintRadius.h"
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Point.h>
13 #include <SketcherPrs_Factory.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_Data.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_Circ.h>
20 #include <GeomAPI_Circ2d.h>
21 #include <GeomAPI_Dir.h>
22 #include <GeomAPI_XY.h>
23 #include <GeomAPI_XYZ.h>
24 #include <GeomDataAPI_Point2D.h>
25 #include <GeomDataAPI_Dir.h>
27 #include <Config_PropManager.h>
29 const double tolerance = 1.e-7;
31 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
33 myFlyoutUpdate = false;
36 void SketchPlugin_ConstraintRadius::initAttributes()
38 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
39 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
40 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
43 void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName,
44 std::string& theDefault)
46 theSection = "Visualization";
47 theName = "sketch_dimension_color";
48 theDefault = SKETCH_DIMENSION_COLOR;
51 void SketchPlugin_ConstraintRadius::execute()
53 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
54 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
55 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
58 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
59 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
60 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
61 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
63 aRadius = anAttribute->value();
64 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
65 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
66 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
67 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
68 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
69 if (aCenterAttr && aStartAttr)
70 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
72 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
73 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
74 //if(!aValueAttr->isInitialized()) {
75 // aValueAttr->setValue(aRadius);
78 // the value should to be computed here,
79 // not in the getAISObject in order to change the model value
80 // inside the object transaction. This is important for creating a constraint by preselection.
81 // The display of the presentation in this case happens after the transaction commit
82 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
83 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
84 if (!aFlyoutAttr->isInitialized())
85 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
89 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
91 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
94 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
95 double aRadius = circleRadius(aCyrcFeature);
100 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
101 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
103 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
104 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
105 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
107 double aShift = aRadius * 1.1;
108 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
109 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
110 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
111 aFlyoutAttr->setValue(aFPnt);
113 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
114 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
115 aFlyoutAttr->setValue(aStartAttr->pnt());
120 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
122 static const double kErrorResult = -1.;
126 std::shared_ptr<ModelAPI_Data> aData = data();
127 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
128 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
131 theCirc = ModelAPI_Feature::feature(anAttr->object());
132 std::string aKind = theCirc ? theCirc->getKind() : "";
133 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
136 DataPtr aCircData = theCirc->data();
137 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
138 if (aKind == SketchPlugin_Circle::ID()) {
139 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
140 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
141 return aCircRadius->value();
143 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
144 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
145 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
146 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
147 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
152 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
157 AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
162 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
164 std::shared_ptr<ModelAPI_Data> aData = data();
165 if (!aData->isValid())
168 myFlyoutUpdate = true;
169 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
170 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
171 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
172 myFlyoutUpdate = false;
175 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
176 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
177 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
178 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
179 if (!aValueAttr->isInitialized()) {
180 // only if it is not initialized, try to compute the current value
181 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
182 double aRadius = circleRadius(aCyrcFeature);
183 if (aRadius > 0) { // set as value the radius of updated reference to another circle
184 aValueAttr->setValue(aRadius);
187 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
188 // Recalculate flyout point in local coordinates of the circle (or arc):
189 // coordinates are calculated according to center of the shape
190 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
191 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
194 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
195 attribute(SketchPlugin_Constraint::ENTITY_A()));
196 if (!aRefAttr || !aRefAttr->isObject())
198 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
199 if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
200 aFeature->getKind() != SketchPlugin_Circle::ID()))
203 std::string aCenterAttrName;
204 if (aFeature->getKind() == SketchPlugin_Circle::ID())
205 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
206 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
207 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
208 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
209 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
210 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
211 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
212 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
214 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
215 double aDist = aFlyoutPnt->distance(aCenter);
216 if (aDist < tolerance)
219 myFlyoutUpdate = true;
220 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
221 aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
222 myFlyoutUpdate = false;