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_XYZ.h>
23 #include <GeomDataAPI_Point2D.h>
24 #include <GeomDataAPI_Dir.h>
26 #include <Config_PropManager.h>
28 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
32 void SketchPlugin_ConstraintRadius::initAttributes()
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());
39 void SketchPlugin_ConstraintRadius::execute()
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());
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()));
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());
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);
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());
76 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
78 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
81 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
82 double aRadius = circleRadius(aCyrcFeature);
87 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
88 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
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);
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());
106 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
108 static const double kErrorResult = -1.;
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()));
117 theCirc = ModelAPI_Feature::feature(anAttr->object());
118 std::string aKind = theCirc ? theCirc->getKind() : "";
119 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
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();
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());
138 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
143 AISObjectPtr anAIS = thePrevious;
145 anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch()->coordinatePlane());
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]);
155 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
157 std::shared_ptr<ModelAPI_Data> aData = data();
158 if (!aData->isValid())
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());
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();
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();
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();
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);
190 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
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);