1 // File: SketchPlugin_ConstraintDistance.cpp
2 // Created: 23 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintDistance.h"
6 #include <SketchPlugin_Point.h>
8 #include <GeomAPI_Lin2D.h>
9 #include <GeomAPI_Pnt2D.h>
10 #include <GeomDataAPI_Point2D.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_Data.h>
15 #include <AIS_LengthDimension.hxx>
19 /// Obtain the point object from specified constraint parameter
20 static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
22 const std::string& theAttribute);
25 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
29 void SketchPlugin_ConstraintDistance::initAttributes()
31 data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
32 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
33 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
34 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
37 void SketchPlugin_ConstraintDistance::execute()
39 boost::shared_ptr<ModelAPI_Data> aData = data();
41 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
42 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
43 if (aPoint_A && aPoint_B) {
44 AttributeDoublePtr anAttr_Value =
45 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
47 if (!anAttr_Value->isInitialized()) {
48 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
53 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintDistance::getAISShape(
54 Handle_AIS_InteractiveObject thePrevious)
59 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
61 DataPtr aData = data();
62 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
63 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
64 if (!aPoint_A || !aPoint_B)
67 // fly out calculation
68 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
69 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
71 if (aFlyOutAttr->isInitialized()) {
72 boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
74 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin =
75 boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
76 aPoint_B->x(), aPoint_B->y()));
77 boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
78 double aDistance = aFlyOutPnt->distance(aProjectedPoint);
79 if (!aFeatureLin->isRight(aFlyOutPnt))
80 aDistance = -aDistance;
84 //Build dimension here
85 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
86 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
89 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
90 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
91 double aValue = aValueAttr->value();
93 Handle(AIS_InteractiveObject) anAIS = thePrevious;
96 Handle(AIS_LengthDimension) aDimAIS =
97 new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
98 aDimAIS->SetCustomValue(aValue);
100 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
101 anAspect->MakeArrows3d (Standard_False);
102 anAspect->MakeText3d(false);
103 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
104 anAspect->MakeTextShaded(false);
105 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
106 aDimAIS->SetDimensionAspect (anAspect);
107 aDimAIS->SetFlyout(aFlyout);
108 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
113 // update presentation
114 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
115 if (!aDimAIS.IsNull()) {
116 aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
117 aDimAIS->SetCustomValue(aValue);
118 aDimAIS->SetFlyout(aFlyout);
120 aDimAIS->Redisplay(Standard_True);
126 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
128 boost::shared_ptr<ModelAPI_Data> aData = data();
129 if (!aData->isValid())
132 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
133 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
134 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
137 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
138 const std::string& theAttribute)
140 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
146 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
147 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
149 aFeature = anAttr->feature();
151 if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
152 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
153 (aFeature->data()->attribute(POINT_ATTR_COORD));
156 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());