1 // File: SketchPlugin_ConstraintLength.cpp
2 // Created: 30 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintLength.h"
6 #include <SketchPlugin_Line.h>
8 #include <GeomDataAPI_Point2D.h>
10 #include <ModelAPI_AttributeDouble.h>
11 #include <ModelAPI_Data.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Lin2d.h>
16 #include <GeomAPI_Pnt2d.h>
18 #include <AIS_LengthDimension.hxx>
22 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
26 void SketchPlugin_ConstraintLength::initAttributes()
28 data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
29 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
30 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
33 void SketchPlugin_ConstraintLength::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<GeomDataAPI_Point2D> aPoint1 =
44 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_START));
45 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
46 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_END));
48 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
50 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
51 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
52 aValueAttr->setValue(aLenght);
57 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintLength::getAISShape(
58 Handle_AIS_InteractiveObject thePrevious)
63 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
65 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
66 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
69 FeaturePtr aFeature = anAttr->feature();
70 if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
73 // fly out calculation
74 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
75 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
76 boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
78 boost::shared_ptr<GeomDataAPI_Point2D> aStartPoint =
79 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_START));
80 boost::shared_ptr<GeomDataAPI_Point2D> anEndPoint =
81 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(LINE_ATTR_END));
83 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
84 (new GeomAPI_Lin2d(aStartPoint->x(), aStartPoint->y(),
85 anEndPoint->x(), anEndPoint->y()));
86 boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
87 double aDistance = aFlyOutPnt->distance(aProjectedPoint);
88 if (!aFeatureLin->isRight(aFlyOutPnt))
89 aDistance = -aDistance;
90 double aFlyout = aDistance;
93 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
94 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
95 double aValue = aValueAttr->value();
97 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
98 if (!aData->isValid())
101 boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
102 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
103 boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
104 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
106 //Build dimension here
107 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPointStart->x(), aPointStart->y());
108 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPointEnd->x(), aPointEnd->y());
110 Handle(AIS_InteractiveObject) anAIS = thePrevious;
113 Handle(AIS_LengthDimension) aDimAIS =
114 new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
115 aDimAIS->SetCustomValue(aValue);
117 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
118 anAspect->MakeArrows3d (Standard_False);
119 anAspect->MakeText3d(false/*is text 3d*/);
120 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
121 anAspect->MakeTextShaded(false/*is test shaded*/);
122 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
123 /*if (isUnitsDisplayed)
125 aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
127 aDimAIS->SetDimensionAspect (anAspect);
128 aDimAIS->SetFlyout(aFlyout);
129 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
135 // update presentation
136 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
137 if (!aDimAIS.IsNull())
139 aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
140 aDimAIS->SetCustomValue(aValue);
141 aDimAIS->SetFlyout(aFlyout);
143 aDimAIS->Redisplay(Standard_True);
149 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
151 boost::shared_ptr<ModelAPI_Data> aData = data();
152 if (!aData->isValid())
155 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
156 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
157 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);