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 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
26 void SketchPlugin_ConstraintRadius::initAttributes()
28 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
29 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
30 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
33 void SketchPlugin_ConstraintRadius::execute()
35 if (data()->attribute(SketchPlugin_Constraint::ENTITY_A())->isInitialized() &&
36 !data()->attribute(SketchPlugin_Constraint::VALUE())->isInitialized()) {
38 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
39 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
40 FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(aRef->object());
43 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
44 if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
45 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
46 (aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
48 aRadius = anAttribute->value();
50 else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
51 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
52 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
53 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
54 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
55 if (aCenterAttr && aStartAttr)
56 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
58 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
59 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
60 aValueAttr->setValue(aRadius);
65 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintRadius::getAISObject(
66 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
71 boost::shared_ptr<ModelAPI_Data> aData = data();
72 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
73 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
76 FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(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 =
83 boost::dynamic_pointer_cast<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>(aData->attribute(SketchPlugin_Circle::CENTER_ID()));
94 AttributeDoublePtr aCircRadius =
95 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Circle::RADIUS_ID()));
96 aRadius = aCircRadius->value();
98 else if (aKind == SketchPlugin_Arc::ID()) {
99 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::CENTER_ID()));
100 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
101 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Arc::START_ID()));
102 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
105 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
106 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
107 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
108 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
109 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
112 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
113 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
114 double aValue = aRadius;
115 if (aValueAttr && aValueAttr->isInitialized())
116 aValue = aValueAttr->value();
118 boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
120 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
121 anAIS->createRadius(aCircle, aFlyoutPnt, aValue);
125 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
127 boost::shared_ptr<ModelAPI_Data> aData = data();
128 if (!aData->isValid())
131 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
132 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
133 FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(aRef->object());
136 std::string aCenterAttrName;
137 if (aFeature->getKind() == SketchPlugin_Circle::ID())
138 aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
139 else if (aFeature->getKind() == SketchPlugin_Arc::ID())
140 aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
141 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
142 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
143 boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
145 // The specified delta applied on the circle curve,
146 // so it will be scaled due to distance between flyout and center points
147 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
148 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
149 boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
151 boost::shared_ptr<ModelAPI_AttributeDouble> aRadius =
152 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
153 double aScale = aFlyout->distance(aCenter) / aRadius->value();
155 boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
156 aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
157 aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
158 aFlyout = aCircle->project(aFlyout);
160 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());