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 if (data()->attribute(SketchPlugin_Constraint::ENTITY_A())->isInitialized() &&
38 !data()->attribute(SketchPlugin_Constraint::VALUE())->isInitialized()) {
40 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
41 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
42 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
45 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
46 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
47 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
48 (aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
50 aRadius = anAttribute->value();
52 else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
53 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
54 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
55 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
56 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
57 if (aCenterAttr && aStartAttr)
58 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
60 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
61 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
62 aValueAttr->setValue(aRadius);
67 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintRadius::getAISObject(
68 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
73 boost::shared_ptr<ModelAPI_Data> aData = data();
74 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
75 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
78 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
79 std::string aKind = aFeature ? aFeature->getKind() : "";
80 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
84 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
85 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
86 if (!aFlyoutAttr->isInitialized())
88 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
91 aData = aFeature->data();
92 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
94 if (aKind == SketchPlugin_Circle::ID()) {
95 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Circle::CENTER_ID()));
96 AttributeDoublePtr aCircRadius =
97 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
98 aRadius = aCircRadius->value();
100 else if (aKind == SketchPlugin_Arc::ID()) {
101 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
102 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
103 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
104 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
107 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
108 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
109 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
110 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
111 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
114 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
115 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
116 double aValue = aRadius;
117 if (aValueAttr && aValueAttr->isInitialized())
118 aValue = aValueAttr->value();
120 boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
122 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
123 anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
125 // Set color from preferences
126 std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
127 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
131 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
133 boost::shared_ptr<ModelAPI_Data> aData = data();
134 if (!aData->isValid())
137 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
138 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
139 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
142 std::string aCenterAttrName;
143 if (aFeature->getKind() == SketchPlugin_Circle::ID())
144 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
145 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
146 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
147 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
148 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
149 boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
151 // The specified delta applied on the circle curve,
152 // so it will be scaled due to distance between flyout and center points
153 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
154 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
155 boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
157 boost::shared_ptr<ModelAPI_AttributeDouble> aRadius =
158 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
159 double aScale = aFlyout->distance(aCenter) / aRadius->value();
161 boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
162 aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
163 aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
164 aFlyout = aCircle->project(aFlyout);
166 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());