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 && anAttr_B && !anAttr_Value->isInitialized())
50 FeaturePtr aFeature_A = anAttr_A->feature();
51 FeaturePtr aFeature_B = anAttr_B->feature();
53 double aValue = 40; // TODO
54 anAttr_Value->setValue(aValue);
58 Handle(AIS_InteractiveObject) SketchPlugin_ConstraintDistance::getAISShape(
59 Handle_AIS_InteractiveObject thePrevious)
64 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
66 DataPtr aData = data();
67 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
68 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
69 if (!aPoint_A || !aPoint_B)
72 // fly out calculation
73 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
74 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
75 boost::shared_ptr<GeomAPI_Pnt2d> aFlyOutPnt = aFlyOutAttr->pnt();
77 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin =
78 boost::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
79 aPoint_B->x(), aPoint_B->y()));
80 boost::shared_ptr<GeomAPI_Pnt2d> aProjectedPoint = aFeatureLin->project(aFlyOutPnt);
81 double aDistance = aFlyOutPnt->distance(aProjectedPoint);
82 if (!aFeatureLin->isRight(aFlyOutPnt))
83 aDistance = -aDistance;
84 double aFlyout = aDistance;
86 //Build dimension here
87 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
88 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
91 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
92 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
93 double aValue = aValueAttr->value();
95 Handle(AIS_InteractiveObject) anAIS = thePrevious;
98 Handle(AIS_LengthDimension) aDimAIS =
99 new AIS_LengthDimension(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
100 aDimAIS->SetCustomValue(aValue);
102 Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
103 anAspect->MakeArrows3d (Standard_False);
104 anAspect->MakeText3d(false);
105 anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
106 anAspect->MakeTextShaded(false);
107 aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
108 aDimAIS->SetDimensionAspect (anAspect);
109 aDimAIS->SetFlyout(aFlyout);
110 aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
115 // update presentation
116 Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
117 if (!aDimAIS.IsNull()) {
118 aDimAIS->SetMeasuredGeometry(aPoint1->impl<gp_Pnt>(), aPoint2->impl<gp_Pnt>(), aPlane->impl<gp_Pln>());
119 aDimAIS->SetCustomValue(aValue);
120 aDimAIS->SetFlyout(aFlyout);
122 aDimAIS->Redisplay(Standard_True);
129 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
130 const std::string& theAttribute)
132 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
138 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
139 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
141 aFeature = anAttr->feature();
143 if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
144 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
145 (aFeature->data()->attribute(POINT_ATTR_COORD));