1 // File: SketchPlugin_ConstraintRadius.cpp
2 // Created: 26 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintRadius.h"
7 #include <SketchPlugin_Arc.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_Point.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_Data.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_Circ.h>
16 #include <GeomAPI_Circ2d.h>
17 #include <GeomAPI_Dir.h>
18 #include <GeomAPI_XYZ.h>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomDataAPI_Dir.h>
22 #include <Config_PropManager.h>
24 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
28 void SketchPlugin_ConstraintRadius::initAttributes()
30 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
32 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
35 void SketchPlugin_ConstraintRadius::execute()
37 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
38 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
39 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
42 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
43 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
44 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
45 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
47 aRadius = anAttribute->value();
48 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
49 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
50 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
51 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
52 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
53 if (aCenterAttr && aStartAttr)
54 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
56 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
57 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
58 aValueAttr->setValue(aRadius);
62 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
67 boost::shared_ptr<ModelAPI_Data> aData = data();
68 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
69 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
72 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
73 std::string aKind = aFeature ? aFeature->getKind() : "";
74 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
78 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
79 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
80 if (!aFlyoutAttr->isInitialized())
82 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
85 aData = aFeature->data();
86 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
88 if (aKind == SketchPlugin_Circle::ID()) {
89 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
90 aData->attribute(SketchPlugin_Circle::CENTER_ID()));
91 AttributeDoublePtr aCircRadius = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
92 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
93 aRadius = aCircRadius->value();
94 } else if (aKind == SketchPlugin_Arc::ID()) {
95 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
96 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
97 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
98 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
99 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
102 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
103 boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
104 sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
105 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
106 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
109 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
110 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
111 double aValue = aRadius;
112 if (aValueAttr && aValueAttr->isInitialized())
113 aValue = aValueAttr->value();
115 AISObjectPtr anAIS = thePrevious;
117 anAIS = AISObjectPtr(new GeomAPI_AISObject);
118 anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
120 // Set color from preferences
121 std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
122 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
126 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
128 boost::shared_ptr<ModelAPI_Data> aData = data();
129 if (!aData->isValid())
132 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
133 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
134 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
137 std::string aCenterAttrName;
138 if (aFeature->getKind() == SketchPlugin_Circle::ID())
139 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
140 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
141 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
142 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
143 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
144 boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
146 // The specified delta applied on the circle curve,
147 // so it will be scaled due to distance between flyout and center points
148 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
149 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
150 boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
152 boost::shared_ptr<ModelAPI_AttributeDouble> aRadius = boost::dynamic_pointer_cast<
153 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
154 double aScale = aFlyout->distance(aCenter) / aRadius->value();
156 boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
157 aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
158 aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
159 aFlyout = aCircle->project(aFlyout);
161 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());