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<ModelAPI_AttributeRefAttr> anAttr_A =
42 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
43 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr_B =
44 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
46 AttributeDoublePtr anAttr_Value =
47 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
48 if (anAttr_A->isInitialized() && anAttr_B->isInitialized() && !anAttr_Value->isInitialized())
50 FeaturePtr aFeature_A = anAttr_A->feature();
51 FeaturePtr aFeature_B = anAttr_B->feature();
52 if (aFeature_A && aFeature_A) {
53 // calculate the distance
54 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
55 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
56 (aFeature_A->data()->attribute(POINT_ATTR_COORD));
57 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
58 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
59 (aFeature_B->data()->attribute(POINT_ATTR_COORD));
60 if (aPoint_A && aPoint_B) {
61 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
67 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintDistance::getAISShape(
68 Handle_AIS_InteractiveObject thePrevious)
73 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
75 DataPtr aData = data();
76 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
77 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
78 if (!aPoint_A || !aPoint_B)
81 // fly out calculation
82 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
83 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
85 if (aFlyOutAttr->isInitialized()) {
86 boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
88 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin =
89 boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
90 aPoint_B->x(), aPoint_B->y()));
91 boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
92 double aDistance = aFlyOutPnt->distance(aProjectedPoint);
93 if (!aFeatureLin->isRight(aFlyOutPnt))
94 aDistance = -aDistance;
98 //Build dimension here
99 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
100 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
103 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
104 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
105 double aValue = aValueAttr->value();
107 Handle(AIS_InteractiveObject) anAIS = thePrevious;
110 Handle(AIS_LengthDimension) aDimAIS =
111 new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
112 aDimAIS->SetCustomValue(aValue);
114 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
115 anAspect->MakeArrows3d (Standard_False);
116 anAspect->MakeText3d(false);
117 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
118 anAspect->MakeTextShaded(false);
119 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
120 aDimAIS->SetDimensionAspect (anAspect);
121 aDimAIS->SetFlyout(aFlyout);
122 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
127 // update presentation
128 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
129 if (!aDimAIS.IsNull()) {
130 aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
131 aDimAIS->SetCustomValue(aValue);
132 aDimAIS->SetFlyout(aFlyout);
134 aDimAIS->Redisplay(Standard_True);
140 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
142 boost::shared_ptr<ModelAPI_Data> aData = data();
143 if (!aData->isValid())
146 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
147 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
148 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
151 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
152 const std::string& theAttribute)
154 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
160 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
161 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
163 aFeature = anAttr->feature();
165 if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
166 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
167 (aFeature->data()->attribute(POINT_ATTR_COORD));