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>
15 #include <GeomAPI_Lin2d.h>
16 #include <GeomAPI_Pnt2d.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Document.h>
20 #include <ModelAPI_AttributeRefAttr.h>
21 #include <ModelAPI_AttributeRefList.h>
23 #include <Precision.hxx>
24 #include <V3d_View.hxx>
28 PartSet_FeatureLinePrs::PartSet_FeatureLinePrs(FeaturePtr theSketch)
29 : PartSet_FeaturePrs(theSketch)
33 std::string PartSet_FeatureLinePrs::getKind()
35 return SKETCH_LINE_KIND;
38 PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY,
39 const PartSet_SelectionMode& theMode)
41 PartSet_SelectionMode aMode = theMode;
45 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_START);
46 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
47 aMode = SM_SecondPoint;
50 case SM_SecondPoint: {
51 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
61 PartSet_SelectionMode PartSet_FeatureLinePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
63 PartSet_SelectionMode aMode = theMode;
64 if (feature() && theFeature && theMode == SM_FirstPoint)
66 // use the last point of the previous feature as the first of the new one
67 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
68 boost::shared_ptr<GeomDataAPI_Point2D> anInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
69 (aData->attribute(LINE_ATTR_END));
70 PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START);
71 PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END);
73 aData = feature()->data();
74 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
75 (aData->attribute(LINE_ATTR_START));
76 PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint);
77 aMode = SM_SecondPoint;
82 std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const
84 std::string aAttribute;
88 aAttribute = LINE_ATTR_START;
91 aAttribute = LINE_ATTR_END;
99 PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& theAttribute) const
101 PartSet_SelectionMode aMode;
103 if (theAttribute == LINE_ATTR_START)
104 aMode = SM_SecondPoint;
105 else if (theAttribute == LINE_ATTR_END)
106 aMode = SM_DonePoint;
110 void PartSet_FeatureLinePrs::projectPointOnLine(FeaturePtr theFeature,
111 const PartSet_SelectionMode& theMode,
112 const gp_Pnt& thePoint, Handle(V3d_View) theView,
113 double& theX, double& theY)
115 if (theFeature && theFeature->getKind() == getKind()) {
119 PartSet_Tools::convertTo2D(thePoint, sketch(), theView, X1, Y1);
120 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(X1, Y1));
121 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(theFeature);
124 case SM_FirstPoint: {
125 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
130 case SM_SecondPoint: {
131 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
132 boost::shared_ptr<GeomAPI_Lin2d> aCurrentLin = boost::shared_ptr<GeomAPI_Lin2d>
133 (new GeomAPI_Lin2d(X0, Y0, X1, Y1));
134 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->intersect(aCurrentLin);
135 boost::shared_ptr<GeomAPI_Pnt2d> aPoint0 = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(X0, Y0));
136 if (aResult->distance(aPoint0) < Precision::Confusion()) { // the start point is nearest to the line
137 // if the first point of a line belongs to the given line
138 // we need to project the second point on the same line
139 aResult = aFeatureLin->project(aPoint);
151 double PartSet_FeatureLinePrs::distanceToPoint(FeaturePtr theFeature,
152 double theX, double theY)
155 if (!theFeature || theFeature->getKind() != getKind())
158 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
159 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
160 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
161 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
162 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
164 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
165 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
167 if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
168 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(aPoint);
169 aDelta = aResult->distance(aPoint);
172 aDelta = aLin2d.distance(aPoint);
178 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::findPoint(FeaturePtr theFeature,
179 double theX, double theY)
181 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
182 if (!theFeature || theFeature->getKind() != getKind())
185 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
186 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
187 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
188 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
189 fabs(aPoint->y() - theY) < Precision::Confusion())
192 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
193 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
194 fabs(aPoint->y() - theY) < Precision::Confusion())
200 boost::shared_ptr<GeomAPI_Lin2d> PartSet_FeatureLinePrs::createLin2d(FeaturePtr theFeature)
202 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin;
203 if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind())
206 double aStartX, aStartY, anEndX, anEndY;
207 getLinePoint(theFeature, LINE_ATTR_START, aStartX, aStartY);
208 getLinePoint(theFeature, LINE_ATTR_END, anEndX, anEndY);
210 aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
211 (new GeomAPI_Lin2d(aStartX, aStartY, anEndX, anEndY));
215 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::featurePoint
216 (const PartSet_SelectionMode& theMode)
218 std::string aPointArg;
222 aPointArg = LINE_ATTR_START;
225 aPointArg = LINE_ATTR_END;
230 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
231 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
232 (aData->attribute(aPointArg));
236 void PartSet_FeatureLinePrs::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute,
237 double& theX, double& theY)
239 if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind())
241 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
242 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
243 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));