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>
17 /// Obtain the point object from specified constraint parameter
18 static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
20 const std::string& theAttribute);
22 static boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute);
24 static boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine,
25 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint);
28 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
32 //*************************************************************************************
33 void SketchPlugin_ConstraintDistance::initAttributes()
35 data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::type());
36 data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
37 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
38 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
41 //*************************************************************************************
42 void SketchPlugin_ConstraintDistance::execute()
44 boost::shared_ptr<ModelAPI_Data> aData = data();
45 AttributeDoublePtr anAttr_Value =
46 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
47 aData->attribute(SketchPlugin_Constraint::VALUE()));
49 if (!anAttr_Value->isInitialized()) {
50 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A =
51 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
52 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B =
53 getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
55 if (aPoint_A && aPoint_B) { // both points
56 anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
58 if (!aPoint_A && aPoint_B) { //Line and point
59 boost::shared_ptr<SketchPlugin_Line> aLine =
60 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
62 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
63 } else if (aPoint_A && !aPoint_B) { // Point and line
64 boost::shared_ptr<SketchPlugin_Line> aLine =
65 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
67 anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
73 //*************************************************************************************
74 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
75 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
80 boost::shared_ptr<GeomAPI_Pln> aPlane = sketch()->plane();
82 DataPtr aData = data();
83 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
84 boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
86 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
87 boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
89 if (aPoint_A && aPoint_B) {
90 aPnt_A = aPoint_A->pnt();
91 aPnt_B = aPoint_B->pnt();
92 } else if (!aPoint_A && aPoint_B) {
93 boost::shared_ptr<SketchPlugin_Line> aLine =
94 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
96 aPnt_B = aPoint_B->pnt();
97 aPnt_A = getProjectionPoint(aLine, aPnt_B);
99 } else if (aPoint_A && !aPoint_B) {
100 boost::shared_ptr<SketchPlugin_Line> aLine =
101 getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
103 aPnt_A = aPoint_A->pnt();
104 aPnt_B = getProjectionPoint(aLine, aPnt_A);
107 if (!aPnt_A || !aPnt_B)
110 boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr =
111 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
113 boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
114 boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
115 boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ?
116 sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) :
117 boost::shared_ptr<GeomAPI_Pnt>();
120 boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr =
121 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
122 double aValue = aValueAttr->value();
124 boost::shared_ptr<GeomAPI_AISObject> anAIS = thePrevious;
126 anAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject);
127 anAIS->createDistance(aPoint1, aPoint2, aFlyoutPnt, aPlane, aValue);
131 //*************************************************************************************
132 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
134 boost::shared_ptr<ModelAPI_Data> aData = data();
135 if (!aData->isValid())
138 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
139 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
140 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
143 //*************************************************************************************
144 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
145 const std::string& theAttribute)
147 boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
153 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
154 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
156 aFeature = SketchPlugin_Sketch::getFeature(anAttr->object());
158 if (aFeature && aFeature->getKind() == SketchPlugin_Point::ID())
159 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
160 (aFeature->data()->attribute(SketchPlugin_Point::COORD_ID()));
161 else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
162 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
163 (aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
164 else if (anAttr->attr()) {
165 aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
170 //*************************************************************************************
171 boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute)
173 boost::shared_ptr<SketchPlugin_Line> aLine;
177 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
178 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
180 FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(anAttr->object());
181 if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
182 aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
188 //*************************************************************************************
189 boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine,
190 const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
192 boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
193 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
194 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
195 aData->attribute(SketchPlugin_Line::START_ID()));
196 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
197 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198 aData->attribute(SketchPlugin_Line::END_ID()));
200 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
201 return aLin2d.project(thePoint);