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 /// Obtain the point object from specified constraint parameter
16 static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
18 const std::string& theAttribute);
21 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
25 void SketchPlugin_ConstraintDistance::initAttributes()
27 data()->addAttribute(CONSTRAINT_ATTR_VALUE, ModelAPI_AttributeDouble::type());
28 data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
29 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
30 data()->addAttribute(CONSTRAINT_ATTR_ENTITY_B, ModelAPI_AttributeRefAttr::type());
33 void SketchPlugin_ConstraintDistance::execute()
35 boost::shared_ptr<ModelAPI_Data> aData = data();
37 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
38 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
39 if (aPoint_A && aPoint_B) {
40 AttributeDoublePtr anAttr_Value =
41 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
43 if (!anAttr_Value->isInitialized()) {
44 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
49 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
50 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
55 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
57 DataPtr aData = data();
58 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_A);
59 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, CONSTRAINT_ATTR_ENTITY_B);
60 if (!aPoint_A || !aPoint_B)
63 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
64 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
66 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
67 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
68 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ?
69 sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) :
70 boost::shared_ptr<GeomAPI_Pnt>();
73 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
74 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
75 double aValue = aValueAttr->value();
77 boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
79 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
80 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
84 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
86 boost::shared_ptr<ModelAPI_Data> aData = data();
87 if (!aData->isValid())
90 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
91 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
92 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
95 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
96 const std::string& theAttribute)
98 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
104 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
105 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
107 aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
109 if (aFeature && aFeature->getKind() == SKETCH_POINT_KIND)
110 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
111 (aFeature->data()->attribute(POINT_ATTR_COORD));
114 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());