1 // File: SketchPlugin_ConstraintDistance.cpp
2 // Created: 23 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_ConstraintDistance.h"
6 #include <SketchPlugin_Point.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
10 #include <GeomAPI_Lin2d.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_Data.h>
18 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
22 //*************************************************************************************
23 void SketchPlugin_ConstraintDistance::initAttributes()
25 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
26 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
27 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
28 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
31 //*************************************************************************************
32 void SketchPlugin_ConstraintDistance::execute()
34 boost::shared_ptr<ModelAPI_Data> aData = data();
35 AttributeDoublePtr anAttr_Value =
36 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
37 aData->attribute(SketchPlugin_Constraint::VALUE()));
39 if (!anAttr_Value->isInitialized()) {
40 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
41 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
42 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
43 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
45 if (aPoint_A && aPoint_B) { // both points
46 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
48 if (!aPoint_A && aPoint_B) { //Line and point
49 boost::shared_ptr<SketchPlugin_Line> aLine =
50 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
52 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
53 } else if (aPoint_A && !aPoint_B) { // Point and line
54 boost::shared_ptr<SketchPlugin_Line> aLine =
55 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
57 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
63 //*************************************************************************************
64 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
65 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
70 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
72 DataPtr aData = data();
73 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
74 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
76 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
77 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
79 if (aPoint_A && aPoint_B) {
80 aPnt_A = aPoint_A->pnt();
81 aPnt_B = aPoint_B->pnt();
82 } else if (!aPoint_A && aPoint_B) {
83 boost::shared_ptr<SketchPlugin_Line> aLine =
84 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
86 aPnt_B = aPoint_B->pnt();
87 aPnt_A = getProjectionPoint(aLine, aPnt_B);
89 } else if (aPoint_A && !aPoint_B) {
90 boost::shared_ptr<SketchPlugin_Line> aLine =
91 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
93 aPnt_A = aPoint_A->pnt();
94 aPnt_B = getProjectionPoint(aLine, aPnt_A);
97 if (!aPnt_A || !aPnt_B)
100 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
101 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
103 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
104 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
105 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ?
106 sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) :
107 boost::shared_ptr<GeomAPI_Pnt>();
110 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
111 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
112 double aValue = aValueAttr->value();
114 boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
116 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
117 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
121 //*************************************************************************************
122 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
124 boost::shared_ptr<ModelAPI_Data> aData = data();
125 if (!aData->isValid())
128 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
129 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
130 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
133 //*************************************************************************************
134 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
135 const std::string& theAttribute)
137 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
143 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
144 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
146 aFeature = ModelAPI_Feature::feature(anAttr->object());
148 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
149 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
150 (aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
151 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
152 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
153 (aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
154 else if (anAttr->attr()) {
155 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
160 //*************************************************************************************
161 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute)
163 boost::shared_ptr<SketchPlugin_Line> aLine;
167 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
168 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
170 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->object());
171 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
172 aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
178 //*************************************************************************************
179 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine,
180 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
182 boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
183 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
184 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185 aData->attribute(SketchPlugin_Line::START_ID()));
186 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
187 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188 aData->attribute(SketchPlugin_Line::END_ID()));
190 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
191 return aLin2d.project(thePoint);