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 = boost::dynamic_pointer_cast<
41 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();
51 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
52 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
53 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
54 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
55 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
56 if (aCenterAttr && aStartAttr)
57 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
59 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
60 ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
61 aValueAttr->setValue(aRadius);
66 AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
71 boost::shared_ptr<ModelAPI_Data> aData = data();
72 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
73 ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
76 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
77 std::string aKind = aFeature ? aFeature->getKind() : "";
78 if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
82 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
83 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
84 if (!aFlyoutAttr->isInitialized())
86 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = sketch()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
89 aData = aFeature->data();
90 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
92 if (aKind == SketchPlugin_Circle::ID()) {
93 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
94 aData->attribute(SketchPlugin_Circle::CENTER_ID()));
95 AttributeDoublePtr aCircRadius = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
96 aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
97 aRadius = aCircRadius->value();
98 } else if (aKind == SketchPlugin_Arc::ID()) {
99 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
101 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
102 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
103 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
106 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
107 boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
108 sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
109 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
110 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
113 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = boost::dynamic_pointer_cast<
114 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
115 double aValue = aRadius;
116 if (aValueAttr && aValueAttr->isInitialized())
117 aValue = aValueAttr->value();
119 AISObjectPtr anAIS = thePrevious;
121 anAIS = AISObjectPtr(new GeomAPI_AISObject);
122 anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
124 // Set color from preferences
125 std::vector<int> aRGB = Config_PropManager::color("Visualization", "radius_color", RADIUS_COLOR);
126 anAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
130 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
132 boost::shared_ptr<ModelAPI_Data> aData = data();
133 if (!aData->isValid())
136 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef = boost::dynamic_pointer_cast<
137 ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
138 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
141 std::string aCenterAttrName;
142 if (aFeature->getKind() == SketchPlugin_Circle::ID())
143 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
144 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
145 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
146 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
147 GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
148 boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
150 // The specified delta applied on the circle curve,
151 // so it will be scaled due to distance between flyout and center points
152 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = boost::dynamic_pointer_cast<
153 GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
154 boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
156 boost::shared_ptr<ModelAPI_AttributeDouble> aRadius = boost::dynamic_pointer_cast<
157 ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
158 double aScale = aFlyout->distance(aCenter) / aRadius->value();
160 boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
161 aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
162 aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
163 aFlyout = aCircle->project(aFlyout);
165 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());