1 // File: PartSet_FeaturePrs.h
2 // Created: 16 Jun 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_FeatureLengthPrs.h>
6 #include <PartSet_Tools.h>
8 #include <PartSet_FeatureLinePrs.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_ConstraintLength.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Lin2d.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeDouble.h>
25 #include <Precision.hxx>
29 PartSet_FeatureLengthPrs::PartSet_FeatureLengthPrs(FeaturePtr theSketch)
30 : PartSet_FeaturePrs(theSketch)
34 std::string PartSet_FeatureLengthPrs::getKind()
36 return SKETCH_CONSTRAINT_LENGTH_KIND;
39 PartSet_SelectionMode PartSet_FeatureLengthPrs::setPoint(double theX, double theY,
40 const PartSet_SelectionMode& theMode)
42 PartSet_SelectionMode aMode = theMode;
46 //PartSet_Tools::setFeaturePoint(feature(), theX, theY, CIRCLE_ATTR_CENTER);
47 //aMode = SM_SecondPoint;
50 case SM_SecondPoint: {
51 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
52 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
53 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
56 aFeature = anAttr->feature();
57 if (aFeature->getKind() != SKETCH_LINE_KIND) {
58 aFeature = FeaturePtr();
61 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
62 (new GeomAPI_Pnt2d(theX, theY));
63 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
64 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
65 double aDistance = aPoint->distance(aResult);
67 double aStartX, aStartY;
68 PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
70 if (aFeatureLin->crossed(aPoint) < 0)
71 aDistance = -aDistance;
73 AttributeDoublePtr aFlyoutAttr =
74 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
75 aFlyoutAttr->setValue(aDistance);
86 std::string PartSet_FeatureLengthPrs::getAttribute(const PartSet_SelectionMode& theMode) const
88 std::string aAttribute;
92 //aAttribute = CIRCLE_ATTR_CENTER;
95 //aAttribute = CIRCLE_ATTR_RADIUS;
103 PartSet_SelectionMode PartSet_FeatureLengthPrs::getNextMode(const std::string& theAttribute) const
105 PartSet_SelectionMode aMode = SM_SecondPoint;
107 /*if (theAttribute == CIRCLE_ATTR_CENTER)
108 aMode = SM_SecondPoint;
109 else if (theAttribute == CIRCLE_ATTR_RADIUS)
110 aMode = SM_DonePoint;*/
114 void PartSet_FeatureLengthPrs::move(double theDeltaX, double theDeltaY)
116 /*boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
117 if (!aData->isValid())
120 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
121 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
122 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);*/
125 double PartSet_FeatureLengthPrs::distanceToPoint(FeaturePtr theFeature,
126 double theX, double theY)
129 if (!theFeature || theFeature->getKind() != getKind())
132 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
133 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
134 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
136 boost::shared_ptr<GeomAPI_Pnt2d> aPoint2d(new GeomAPI_Pnt2d(theX, theY));
137 return aPoint->pnt()->distance(aPoint2d);*/
141 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::findPoint(FeaturePtr theFeature,
142 double theX, double theY)
144 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
145 /*if (!theFeature || theFeature->getKind() != getKind())
148 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
149 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
150 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
151 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
157 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLengthPrs::featurePoint
158 (const PartSet_SelectionMode& theMode)
160 /*std::string aPointArg;
164 aPointArg = CIRCLE_ATTR_CENTER;
169 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
170 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
171 (aData->attribute(aPointArg));
174 return boost::shared_ptr<GeomDataAPI_Point2D>();
177 void PartSet_FeatureLengthPrs::initFeature(FeaturePtr theSourceFeature)
179 if (feature() && theSourceFeature && theSourceFeature->getKind() == SKETCH_LINE_KIND)
181 // set length feature
182 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
183 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
184 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
185 aRef->setFeature(theSourceFeature);
188 aData = theSourceFeature->data();
189 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
190 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
191 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
192 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
194 double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
195 PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);