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 void PartSet_FeatureLinePrs::initFeature(FeaturePtr theFeature)
40 if (feature() && theFeature)
42 // use the last point of the previous feature as the first of the new one
43 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
44 boost::shared_ptr<GeomDataAPI_Point2D> anInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
45 (aData->attribute(LINE_ATTR_END));
46 PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START);
47 PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END);
49 aData = feature()->data();
50 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
51 (aData->attribute(LINE_ATTR_START));
52 PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint);
56 PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY,
57 const PartSet_SelectionMode& theMode)
59 PartSet_SelectionMode aMode = theMode;
63 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_START);
64 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
65 aMode = SM_SecondPoint;
68 case SM_SecondPoint: {
69 PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END);
79 std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const
81 std::string aAttribute;
85 aAttribute = LINE_ATTR_START;
88 aAttribute = LINE_ATTR_END;
96 PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& theAttribute) const
98 PartSet_SelectionMode aMode;
100 if (theAttribute == LINE_ATTR_START)
101 aMode = SM_SecondPoint;
102 else if (theAttribute == LINE_ATTR_END)
103 aMode = SM_DonePoint;
107 void PartSet_FeatureLinePrs::move(double theDeltaX, double theDeltaY)
109 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
110 if (!aData->isValid())
113 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
114 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
115 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
117 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
118 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
119 aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
122 void PartSet_FeatureLinePrs::projectPointOnLine(FeaturePtr theFeature,
123 const PartSet_SelectionMode& theMode,
124 const gp_Pnt& thePoint, Handle(V3d_View) theView,
125 double& theX, double& theY)
127 if (theFeature && theFeature->getKind() == getKind()) {
131 PartSet_Tools::convertTo2D(thePoint, sketch(), theView, X1, Y1);
132 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(X1, Y1));
133 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(theFeature);
136 case SM_FirstPoint: {
137 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
142 case SM_SecondPoint: {
143 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
144 boost::shared_ptr<GeomAPI_Lin2d> aCurrentLin = boost::shared_ptr<GeomAPI_Lin2d>
145 (new GeomAPI_Lin2d(X0, Y0, X1, Y1));
146 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->intersect(aCurrentLin);
147 boost::shared_ptr<GeomAPI_Pnt2d> aPoint0 = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(X0, Y0));
148 if (aResult->distance(aPoint0) < Precision::Confusion()) { // the start point is nearest to the line
149 // if the first point of a line belongs to the given line
150 // we need to project the second point on the same line
151 aResult = aFeatureLin->project(aPoint);
163 double PartSet_FeatureLinePrs::distanceToPoint(FeaturePtr theFeature,
164 double theX, double theY)
167 if (!theFeature || theFeature->getKind() != getKind())
170 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
171 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
172 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
173 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
174 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
176 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
177 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
179 if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
180 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(aPoint);
181 aDelta = aResult->distance(aPoint);
184 aDelta = aLin2d.distance(aPoint);
190 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::findPoint(FeaturePtr theFeature,
191 double theX, double theY)
193 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
194 if (!theFeature || theFeature->getKind() != getKind())
197 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
198 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
199 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
200 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
201 fabs(aPoint->y() - theY) < Precision::Confusion())
204 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
205 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
206 fabs(aPoint->y() - theY) < Precision::Confusion())
212 boost::shared_ptr<GeomAPI_Lin2d> PartSet_FeatureLinePrs::createLin2d(FeaturePtr theFeature)
214 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin;
215 if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind())
218 double aStartX, aStartY, anEndX, anEndY;
219 getLinePoint(theFeature, LINE_ATTR_START, aStartX, aStartY);
220 getLinePoint(theFeature, LINE_ATTR_END, anEndX, anEndY);
222 aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
223 (new GeomAPI_Lin2d(aStartX, aStartY, anEndX, anEndY));
227 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::featurePoint
228 (const PartSet_SelectionMode& theMode)
230 std::string aPointArg;
234 aPointArg = LINE_ATTR_START;
237 aPointArg = LINE_ATTR_END;
242 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
243 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
244 (aData->attribute(aPointArg));
248 void PartSet_FeatureLinePrs::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute,
249 double& theX, double& theY)
251 if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind())
253 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
254 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
255 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));