1 // File: PartSet_FeaturePrs.h
2 // Created: 04 Jun 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_FeatureLinePrs.h>
6 #include <PartSet_Tools.h>
8 #include <SketchPlugin_Feature.h>
9 #include <SketchPlugin_Sketch.h>
10 #include <SketchPlugin_ConstraintCoincidence.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Constraint.h>
14 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeRefList.h>
21 #include <Precision.hxx>
22 #include <V3d_View.hxx>
26 PartSet_FeatureLinePrs::PartSet_FeatureLinePrs(FeaturePtr theSketch)
27 : PartSet_FeaturePrs(theSketch)
31 std::string PartSet_FeatureLinePrs::getKind()
33 return SKETCH_LINE_KIND;
36 void PartSet_FeatureLinePrs::initFeature(FeaturePtr theFeature)
38 if (feature() && theFeature)
40 // use the last point of the previous feature as the first of the new one
41 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
42 boost::shared_ptr<GeomDataAPI_Point2D> anInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
43 (aData->attribute(LINE_ATTR_END));
44 PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START);
45 PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END);
47 aData = feature()->data();
48 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
49 (aData->attribute(LINE_ATTR_START));
50 PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint);
54 PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY,
55 const PartSet_SelectionMode& theMode)
57 PartSet_SelectionMode aMode = theMode;
61 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_START);
62 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
63 aMode = SM_SecondPoint;
66 case SM_SecondPoint: {
67 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
77 std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const
79 std::string aAttribute;
83 aAttribute = LINE_ATTR_START;
86 aAttribute = LINE_ATTR_END;
94 PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& theAttribute) const
96 PartSet_SelectionMode aMode;
98 if (theAttribute == LINE_ATTR_START)
99 aMode = SM_SecondPoint;
100 else if (theAttribute == LINE_ATTR_END)
101 aMode = SM_DonePoint;
105 void PartSet_FeatureLinePrs::projectPointOnLine(FeaturePtr theFeature,
106 const PartSet_SelectionMode& theMode,
107 const gp_Pnt& thePoint, Handle(V3d_View) theView,
108 double& theX, double& theY)
111 double X0, X1, X2, X3;
112 double Y0, Y1, Y2, Y3;
113 getLinePoint(theFeature, LINE_ATTR_START, X2, Y2);
114 getLinePoint(theFeature, LINE_ATTR_END, X3, Y3);
115 PartSet_Tools::convertTo2D(thePoint, sketch(), theView, X1, Y1);
119 PartSet_Tools::projectPointOnLine(X2, Y2, X3, Y3, X1, Y1, theX, theY);
121 case SM_SecondPoint: {
122 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
123 PartSet_Tools::intersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, theX, theY);
132 double PartSet_FeatureLinePrs::distanceToPoint(FeaturePtr theFeature,
133 double theX, double theY)
136 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
139 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
140 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
141 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
142 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
143 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
146 PartSet_Tools::projectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
147 aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));
152 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::findPoint(FeaturePtr theFeature,
153 double theX, double theY)
155 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
156 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
159 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
160 aPoint2D = PartSet_FeatureLinePrs::findPoint(theFeature, theX, theY);
162 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
163 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
164 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
165 fabs(aPoint->y() - theY) < Precision::Confusion())
168 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
169 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
170 fabs(aPoint->y() - theY) < Precision::Confusion())
176 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::featurePoint
177 (const PartSet_SelectionMode& theMode)
179 std::string aPointArg;
183 aPointArg = LINE_ATTR_START;
186 aPointArg = LINE_ATTR_END;
191 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
192 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
193 (aData->attribute(aPointArg));
197 void PartSet_FeatureLinePrs::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute,
198 double& theX, double& theY)
200 if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind())
202 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
203 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
204 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));