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 <AIS_LengthDimension.hxx>
19 SketchPlugin_ConstraintLength::SketchPlugin_ConstraintLength()
23 void SketchPlugin_ConstraintLength::initAttributes()
25 data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
26 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE, ModelAPI_AttributeDouble::type());
27 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
28 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
31 void SketchPlugin_ConstraintLength::execute()
35 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintLength::getAISShape(
36 Handle_AIS_InteractiveObject thePrevious)
41 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
43 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
44 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
47 FeaturePtr aFeature = anAttr->feature();
48 if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
51 boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr =
52 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
53 double aFlyout = aFlyoutAttr->value();
55 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
56 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
57 double aValue = aValueAttr->value();
59 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
60 if (!aData->isValid())
63 boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
64 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
65 boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
66 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
68 //Build dimension here
69 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPointStart->x(), aPointStart->y());
70 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPointEnd->x(), aPointEnd->y());
72 aPoint1.swap(aPoint2);
74 Handle(AIS_InteractiveObject) anAIS = thePrevious;
77 Handle(AIS_LengthDimension) aDimAIS =
78 new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
79 aDimAIS->SetCustomValue(aValue);
81 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
82 anAspect->MakeArrows3d (Standard_False);
83 anAspect->MakeText3d(false/*is text 3d*/);
84 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
85 anAspect->MakeTextShaded(false/*is test shaded*/);
86 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false/*is units displayed*/);
87 /*if (isUnitsDisplayed)
89 aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
91 aDimAIS->SetDimensionAspect (anAspect);
92 aDimAIS->SetFlyout(aFlyout);
93 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
99 // update presentation
100 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
101 if (!aDimAIS.IsNull())
103 aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
104 aDimAIS->SetCustomValue(aValue);
105 aDimAIS->SetFlyout(aFlyout);
107 aDimAIS->Redisplay(Standard_True);
113 void SketchPlugin_ConstraintLength::move(double theDeltaX, double theDeltaY)
115 boost::shared_ptr<ModelAPI_Data> aData = data();
116 if (!aData->isValid())
119 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
120 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
121 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);