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 <GeomDataAPI_Point2D.h>
17 #include <GeomDataAPI_Dir.h>
19 #include <AIS_InteractiveObject.hxx>
20 #include <AIS_RadiusDimension.hxx>
22 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
26 void SketchPlugin_ConstraintRadius::initAttributes()
28 data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
29 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
30 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
33 void SketchPlugin_ConstraintRadius::execute()
35 if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
36 !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
38 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
39 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
40 FeaturePtr aFeature = aRef->feature();
43 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
44 if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
45 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
46 (aData->attribute(CIRCLE_ATTR_RADIUS));
48 aRadius = anAttribute->value();
50 else if (aFeature->getKind() == SKETCH_ARC_KIND) {
51 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
52 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
53 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
54 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
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(CONSTRAINT_ATTR_VALUE));
60 aValueAttr->setValue(aRadius);
65 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
66 Handle_AIS_InteractiveObject thePrevious)
68 Handle(AIS_InteractiveObject) anAIS = thePrevious;
72 boost::shared_ptr<ModelAPI_Data> aData = data();
73 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
74 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
77 FeaturePtr aFeature = anAttr->feature();
78 std::string aKind = aFeature ? aFeature->getKind() : "";
79 if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
82 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
83 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
84 double aValue = aValueAttr->value();
87 boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr =
88 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
89 if (!aAnchorAttr->isInitialized())
91 boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
92 boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
94 aData = aFeature->data();
95 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
97 if (aKind == SKETCH_CIRCLE_KIND) {
98 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
99 AttributeDoublePtr aCircRadius =
100 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
101 aRadius = aCircRadius->value();
103 else if (aKind == SKETCH_ARC_KIND) {
104 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
105 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
106 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
107 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
111 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
113 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
114 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
115 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
117 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
119 anAnchor = aCircle->project(anAnchor);
120 // TODO: a bug in AIS_RadiusDimension:
121 // The anchor point can't be myCirc.Location() - an exception is raised.
122 // But we need exactly this case...
123 // We want to show a radius dimension starting from the circle centre and
124 // ending at the user-defined point.
125 // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
126 gp_Pnt anAPnt = anAnchor->impl<gp_Pnt>();
127 double aDelta = 1/1000.0;
128 if (anAnchor->x() != 0)
129 anAnchor->setX(anAnchor->x() + aDelta);
130 if (anAnchor->y() != 0)
131 anAnchor->setY(anAnchor->y() + aDelta);
132 if (anAnchor->z() != 0)
133 anAnchor->setZ(anAnchor->z() + aDelta);
137 Handle(AIS_RadiusDimension) aDimAIS =
138 new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
139 aDimAIS->SetCustomValue(aValue);
141 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
142 anAspect->MakeArrows3d (Standard_False);
143 anAspect->MakeText3d(false);
144 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
145 anAspect->MakeTextShaded(false);
146 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
147 aDimAIS->SetDimensionAspect (anAspect);
148 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
154 // update presentation
155 Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
156 if (!aDimAIS.IsNull())
158 aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
159 aDimAIS->SetCustomValue(aValue);
160 aDimAIS->Redisplay(Standard_True);
166 void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
168 boost::shared_ptr<ModelAPI_Data> aData = data();
169 if (!aData->isValid())
172 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
173 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
174 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);