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 <AIS_InteractiveObject.hxx>
23 #include <AIS_RadiusDimension.hxx>
25 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
29 void SketchPlugin_ConstraintRadius::initAttributes()
31 data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
32 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
33 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
36 void SketchPlugin_ConstraintRadius::execute()
38 if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
39 !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
41 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
42 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
43 FeaturePtr aFeature = aRef->feature();
46 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
47 if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
48 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
49 (aData->attribute(CIRCLE_ATTR_RADIUS));
51 aRadius = anAttribute->value();
53 else if (aFeature->getKind() == SKETCH_ARC_KIND) {
54 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
55 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
56 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
57 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
58 if (aCenterAttr && aStartAttr)
59 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
61 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
62 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
63 aValueAttr->setValue(aRadius);
68 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
69 Handle_AIS_InteractiveObject thePrevious)
71 Handle(AIS_InteractiveObject) anAIS = thePrevious;
75 boost::shared_ptr<ModelAPI_Data> aData = data();
76 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
77 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
80 FeaturePtr aFeature = anAttr->feature();
81 std::string aKind = aFeature ? aFeature->getKind() : "";
82 if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
85 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
86 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
87 double aValue = aValueAttr->value();
90 boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr =
91 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
92 if (!aAnchorAttr->isInitialized())
94 boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
95 boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
97 aData = aFeature->data();
98 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
100 if (aKind == SKETCH_CIRCLE_KIND) {
101 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
102 AttributeDoublePtr aCircRadius =
103 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
104 aRadius = aCircRadius->value();
106 else if (aKind == SKETCH_ARC_KIND) {
107 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
108 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
109 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
110 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
114 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
116 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
117 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
118 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
120 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
122 anAnchor = aCircle->project(anAnchor);
123 // TODO: a bug in AIS_RadiusDimension:
124 // The anchor point can't be myCirc.Location() - an exception is raised.
125 // But we need exactly this case...
126 // We want to show a radius dimension starting from the circle centre and
127 // ending at the user-defined point.
128 // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
129 boost::shared_ptr<GeomAPI_XYZ> anAnchorXYZ = anAnchor->xyz();
130 anAnchorXYZ = anAnchorXYZ->decreased(aCenter->xyz());
131 boost::shared_ptr<GeomAPI_Dir> aDeltaDir(new GeomAPI_Dir(anAnchorXYZ));
132 const double aDelta = 1e-3;
133 anAnchor->setX(anAnchor->x() + aDelta * aDeltaDir->x());
134 anAnchor->setY(anAnchor->y() + aDelta * aDeltaDir->y());
135 anAnchor->setZ(anAnchor->z() + aDelta * aDeltaDir->z());
139 Handle(AIS_RadiusDimension) aDimAIS =
140 new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
141 aDimAIS->SetCustomValue(aValue);
143 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
144 anAspect->MakeArrows3d (Standard_False);
145 anAspect->MakeText3d(false);
146 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
147 anAspect->MakeTextShaded(false);
148 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
149 aDimAIS->SetDimensionAspect (anAspect);
150 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
156 // update presentation
157 Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
158 if (!aDimAIS.IsNull())
160 aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
161 aDimAIS->SetCustomValue(aValue);
162 aDimAIS->Redisplay(Standard_True);
168 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
170 boost::shared_ptr<ModelAPI_Data> aData = data();
171 if (!aData->isValid())
174 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
175 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
176 FeaturePtr aFeature = aRef->feature();
179 std::string aCenterAttrName;
180 if (aFeature->getKind() == SKETCH_CIRCLE_KIND)
181 aCenterAttrName = CIRCLE_ATTR_CENTER;
182 else if (aFeature->getKind() == SKETCH_ARC_KIND)
183 aCenterAttrName = ARC_ATTR_CENTER;
184 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
185 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
186 boost::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
188 // The specified delta applied on the circle curve,
189 // so it will be scaled due to distance between flyout and center points
190 boost::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
191 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
192 boost::shared_ptr<GeomAPI_Pnt2d> aFlyout = aFlyoutAttr->pnt();
194 boost::shared_ptr<ModelAPI_AttributeDouble> aRadius =
195 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
196 double aScale = aFlyout->distance(aCenter) / aRadius->value();
198 boost::shared_ptr<GeomAPI_Circ2d> aCircle(new GeomAPI_Circ2d(aCenter, aFlyout));
199 aFlyout->setX(aFlyout->x() + aScale * theDeltaX);
200 aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
201 aFlyout = aCircle->project(aFlyout);
203 aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());