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, not in the getAISObject in order to change the model value
79 // inside the object transaction. This is important for creating a constraint by preselection.
80 // The display of the presentation in this case happens after the transaction commit
81 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
82 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
83 if (!aFlyoutAttr->isInitialized())
84 compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
88 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
90 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
93 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
94 double aRadius = circleRadius(aCyrcFeature);
99 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
100 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
102 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
103 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
105 double aShift = aRadius * 1.1;
106 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
107 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
108 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
109 aFlyoutAttr->setValue(aFPnt);
111 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
112 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
113 aFlyoutAttr->setValue(aStartAttr->pnt());
118 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
120 static const double kErrorResult = -1.;
124 std::shared_ptr<ModelAPI_Data> aData = data();
125 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
126 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
129 theCirc = ModelAPI_Feature::feature(anAttr->object());
130 std::string aKind = theCirc ? theCirc->getKind() : "";
131 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
134 DataPtr aCircData = theCirc->data();
135 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
136 if (aKind == SketchPlugin_Circle::ID()) {
137 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
138 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
139 return aCircRadius->value();
141 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
142 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
143 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
144 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
145 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
150 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
155 AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane(),
160 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
162 std::shared_ptr<ModelAPI_Data> aData = data();
163 if (!aData->isValid())
166 myFlyoutUpdate = true;
167 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
168 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
169 aFlyoutAttr->setValue(aFlyoutAttr->x() + theDeltaX, aFlyoutAttr->y() + theDeltaY);
170 myFlyoutUpdate = false;
173 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
174 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
175 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
176 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
177 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
178 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
179 double aRadius = circleRadius(aCyrcFeature);
180 if (aRadius > 0) { // set as value the radius of updated reference to another circle
181 aValueAttr->setValue(aRadius);
184 } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
185 // Recalculate flyout point in local coordinates of the circle (or arc):
186 // coordinates are calculated according to center of the shape
187 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
188 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
189 attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
191 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
192 attribute(SketchPlugin_Constraint::ENTITY_A()));
193 if (!aRefAttr || !aRefAttr->isObject())
195 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
196 if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
197 aFeature->getKind() != SketchPlugin_Circle::ID()))
200 std::string aCenterAttrName;
201 if (aFeature->getKind() == SketchPlugin_Circle::ID())
202 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
203 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
204 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
205 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
206 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
207 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
208 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
209 ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
211 std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
212 double aDist = aFlyoutPnt->distance(aCenter);
213 if (aDist < tolerance)
216 myFlyoutUpdate = true;
217 std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
218 aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
219 myFlyoutUpdate = false;