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 if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
44 AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
45 (aFeature->data()->attribute(CIRCLE_ATTR_RADIUS));
47 aRadius = anAttribute->value();
49 else if (aFeature->getKind() == SKETCH_ARC_KIND) {
50 //aLength = PartSet_FeatureArcPrs::radius(theFeature);
52 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
53 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
54 aValueAttr->setValue(aRadius);
59 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
60 Handle_AIS_InteractiveObject thePrevious)
62 Handle(AIS_InteractiveObject) anAIS = thePrevious;
66 boost::shared_ptr<ModelAPI_Data> aData = data();
67 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
68 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
71 FeaturePtr aFeature = anAttr->feature();
72 std::string aKind = aFeature ? aFeature->getKind() : "";
73 if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
76 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
77 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
78 double aValue = aValueAttr->value();
81 boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr =
82 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
83 if (!aAnchorAttr->isInitialized())
85 boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
86 boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
88 aData = aFeature->data();
89 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
91 if (aKind == SKETCH_CIRCLE_KIND) {
92 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
93 AttributeDoublePtr aCircRadius =
94 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
95 aRadius = aCircRadius->value();
97 else if (aKind == SKETCH_ARC_KIND) {
98 aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
99 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
100 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
101 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
105 boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
107 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
108 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
109 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
111 boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
113 anAnchor = aCircle->project(anAnchor);
114 // TODO: a bug in AIS_RadiusDimension:
115 // The anchor point can't be myCirc.Location() - an exception is raised.
116 // But we need exactly this case...
117 // We want to show a radius dimension starting from the circle centre and
118 // ending at the user-defined point.
119 // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
120 gp_Pnt anAPnt = anAnchor->impl<gp_Pnt>();
121 double aDelta = 1/1000.0;
122 if (anAnchor->x() != 0)
123 anAnchor->setX(anAnchor->x() + aDelta);
124 if (anAnchor->y() != 0)
125 anAnchor->setY(anAnchor->y() + aDelta);
126 if (anAnchor->z() != 0)
127 anAnchor->setZ(anAnchor->z() + aDelta);
131 Handle(AIS_RadiusDimension) aDimAIS =
132 new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
133 aDimAIS->SetCustomValue(aValue);
135 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
136 anAspect->MakeArrows3d (Standard_False);
137 anAspect->MakeText3d(false);
138 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
139 anAspect->MakeTextShaded(false);
140 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
141 aDimAIS->SetDimensionAspect (anAspect);
142 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
148 // update presentation
149 Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
150 if (!aDimAIS.IsNull())
152 aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
153 aDimAIS->SetCustomValue(aValue);
154 aDimAIS->Redisplay(Standard_True);