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()) {
128 double X0, X1, X2, X3;
129 double Y0, Y1, Y2, Y3;
130 getLinePoint(theFeature, LINE_ATTR_START, X2, Y2);
131 getLinePoint(theFeature, LINE_ATTR_END, X3, Y3);
133 PartSet_Tools::convertTo2D(thePoint, sketch(), theView, X1, Y1);
134 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(X1, Y1));
135 boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
136 (new GeomAPI_Lin2d(X2, Y2, X3, Y3));
138 case SM_FirstPoint: {
139 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
144 case SM_SecondPoint: {
145 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
146 boost::shared_ptr<GeomAPI_Lin2d> aCurrentLin = boost::shared_ptr<GeomAPI_Lin2d>
147 (new GeomAPI_Lin2d(X0, Y0, X1, Y1));
148 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->intersect(aCurrentLin);
149 boost::shared_ptr<GeomAPI_Pnt2d> aPoint0 = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(X0, Y0));
150 if (aResult->distance(aPoint0) < Precision::Confusion()) { // the start point is nearest to the line
151 // if the first point of a line belongs to the given line
152 // we need to project the second point on the same line
153 aResult = aFeatureLin->project(aPoint);
165 double PartSet_FeatureLinePrs::distanceToPoint(FeaturePtr theFeature,
166 double theX, double theY)
169 if (!theFeature || theFeature->getKind() != getKind())
172 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
173 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
174 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
175 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
176 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
178 GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
179 boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
181 if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
182 boost::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(aPoint);
183 aDelta = aResult->distance(aPoint);
186 aDelta = aLin2d.distance(aPoint);
192 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::findPoint(FeaturePtr theFeature,
193 double theX, double theY)
195 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
196 if (!theFeature || theFeature->getKind() != getKind())
199 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
200 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
201 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
202 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
203 fabs(aPoint->y() - theY) < Precision::Confusion())
206 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
207 if (fabs(aPoint->x() - theX) < Precision::Confusion() &&
208 fabs(aPoint->y() - theY) < Precision::Confusion())
214 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_FeatureLinePrs::featurePoint
215 (const PartSet_SelectionMode& theMode)
217 std::string aPointArg;
221 aPointArg = LINE_ATTR_START;
224 aPointArg = LINE_ATTR_END;
229 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
230 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
231 (aData->attribute(aPointArg));
235 void PartSet_FeatureLinePrs::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute,
236 double& theX, double& theY)
238 if (!theFeature || theFeature->getKind() != PartSet_FeatureLinePrs::getKind())
240 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
241 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
242 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));