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 <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_Data.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Circ.h>
18 #include <GeomAPI_Circ2d.h>
19 #include <GeomAPI_Dir.h>
20 #include <GeomAPI_XYZ.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <GeomDataAPI_Dir.h>
24 #include <Config_PropManager.h>
26 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
30 void SketchPlugin_ConstraintRadius::initAttributes()
32 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
33 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
34 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
37 void SketchPlugin_ConstraintRadius::execute()
39 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
40 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
41 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
44 std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
45 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
46 AttributeDoublePtr anAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
47 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
49 aRadius = anAttribute->value();
50 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
51 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
52 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
53 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
54 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
55 if (aCenterAttr && aStartAttr)
56 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
58 //std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
59 // ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
60 //if(!aValueAttr->isInitialized()) {
61 // aValueAttr->setValue(aRadius);
66 bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
68 if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
71 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
72 double aRadius = circleRadius(aCyrcFeature);
77 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
78 GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
80 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
81 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
82 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
83 double aShift = aRadius * 1.1;
84 std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
85 std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
86 std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
87 aFlyoutAttr->setValue(aFPnt);
89 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
90 GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
91 aFlyoutAttr->setValue(aStartAttr->pnt());
96 double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
98 static const double kErrorResult = -1.;
102 std::shared_ptr<ModelAPI_Data> aData = data();
103 std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
104 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
107 theCirc = ModelAPI_Feature::feature(anAttr->object());
108 std::string aKind = theCirc ? theCirc->getKind() : "";
109 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
112 DataPtr aCircData = theCirc->data();
113 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
114 if (aKind == SketchPlugin_Circle::ID()) {
115 AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
116 aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
117 return aCircRadius->value();
119 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
120 aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
121 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
122 GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
123 return aCenterAttr->pnt()->distance(aStartAttr->pnt());
128 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
130 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
131 double aRadius = circleRadius(aCyrcFeature);
133 return thePrevious; // can not create a good presentation
136 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
137 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
138 if (!aFlyoutAttr->isInitialized() && !compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))
139 return thePrevious; // can not create a good presentation
140 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
143 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
144 if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
145 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146 aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
148 aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
149 aCyrcFeature->data()->attribute(SketchPlugin_Arc::CENTER_ID()));
151 std::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
152 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
153 sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
154 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
155 std::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
158 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
159 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
160 if (aValueAttr->isInitialized())
161 aRadius = aValueAttr->value();
162 AISObjectPtr anAIS = thePrevious;
164 anAIS = AISObjectPtr(new GeomAPI_AISObject);
165 anAIS->createRadius(aCircle, aFlyoutPnt, aRadius);
167 // Set color from preferences
168 std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
169 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
173 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
175 std::shared_ptr<ModelAPI_Data> aData = data();
176 if (!aData->isValid())
179 std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
180 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
181 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
184 std::string aCenterAttrName;
185 if (aFeature->getKind() == SketchPlugin_Circle::ID())
186 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
187 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
188 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
189 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
190 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
191 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
193 // The specified delta applied on the circle curve,
194 // so it will be scaled due to distance between flyout and center points
195 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
196 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
197 std::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
199 std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
200 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
201 double aScale = aFlyout->distance(aCenter) / aRadius->value();
203 std::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
204 aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
205 aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
206 aFlyout = aCircle->project(aFlyout);
208 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
211 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
212 if (theID == SketchPlugin_Constraint::ENTITY_A()) {
213 std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
214 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
215 if (!aValueAttr->isInitialized()) { // only if it is not initialized, try to compute the current value
216 std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
217 double aRadius = circleRadius(aCyrcFeature);
218 if (aRadius > 0) { // set as value the radius of updated reference to another circle
219 aValueAttr->setValue(aRadius);