1 // File: PartSet_Tools.h
2 // Created: 28 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_Tools.h>
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_Document.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Dir.h>
16 #include <GeomAPI_XYZ.h>
18 #include <SketchPlugin_Feature.h>
19 #include <SketchPlugin_Sketch.h>
20 #include <SketchPlugin_Point.h>
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_ConstraintCoincidence.h>
23 #include <SketchPlugin_Constraint.h>
25 #include <XGUI_ViewerPrs.h>
27 #include <V3d_View.hxx>
29 #include <ProjLib.hxx>
31 #include <Geom_Line.hxx>
32 #include <GeomAPI_ProjectPointOnCurve.hxx>
38 const double PRECISION_TOLERANCE = 0.000001;
40 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
45 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
46 theView->Eye(XEye, YEye, ZEye);
48 theView->At(XAt, YAt, ZAt);
49 gp_Pnt EyePoint(XEye, YEye, ZEye);
50 gp_Pnt AtPoint(XAt, YAt, ZAt);
52 gp_Vec EyeVector(EyePoint, AtPoint);
53 gp_Dir EyeDir(EyeVector);
55 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
56 Standard_Real X, Y, Z;
57 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
58 gp_Pnt ConvertedPoint(X, Y, Z);
60 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
61 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
65 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
66 Handle(V3d_View) theView, double& theX, double& theY)
71 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
72 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
74 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
75 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
77 boost::shared_ptr<GeomDataAPI_Dir> aX =
78 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
79 boost::shared_ptr<GeomDataAPI_Dir> anY =
80 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
82 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
83 gp_Vec aVec(anOriginPnt, thePoint);
85 if (!theView.IsNull())
87 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
88 theView->Eye(XEye, YEye, ZEye);
90 theView->At(XAt, YAt, ZAt);
91 gp_Pnt EyePoint(XEye, YEye, ZEye);
92 gp_Pnt AtPoint(XAt, YAt, ZAt);
94 gp_Vec anEyeVec(EyePoint, AtPoint);
97 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
98 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
99 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
101 double aDen = anEyeVec * aNormalVec;
102 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
104 gp_Vec aDeltaVec = anEyeVec*aLVec;
105 aVec = aVec - aDeltaVec;
107 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
108 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
111 void PartSet_Tools::convertTo3D(const double theX, const double theY,
112 FeaturePtr theSketch,
118 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
120 boost::shared_ptr<GeomDataAPI_Point> aC =
121 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
122 boost::shared_ptr<GeomDataAPI_Dir> aX =
123 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
124 boost::shared_ptr<GeomDataAPI_Dir> aY =
125 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
127 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
128 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
130 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
131 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
134 void PartSet_Tools::intersectLines(double theX0, double theY0, double theX1, double theY1,
135 double theX2, double theY2, double theX3, double theY3,
136 double& theX, double& theY)
138 double aV1 = theX1 - theX0, aV2 = theY1 - theY0;
139 double aW1 = theX3 - theX2, aW2 = theY3 - theY2;
142 if (aV1 != 0 && aV2 != 0)
143 aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 );
147 theX = theX2 + aT2*aW1;
148 theY = theY2 + aT2*aW2;
150 // the coordinates of two lines are on the common line
151 //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6
152 Standard_Real aPrec = PRECISION_TOLERANCE;
153 if (fabs(theX - theX0) < aPrec && fabs(theY - theY0) < aPrec) {
154 projectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY);
158 void PartSet_Tools::projectPointOnLine(double theX1, double theY1, double theX2, double theY2,
159 double thePointX, double thePointY, double& theX, double& theY)
163 Handle(Geom_Line) aLine = new Geom_Line(gp_Pnt(theX1, theY1, 0),
164 gp_Dir(gp_Vec(gp_Pnt(theX1, theY1, 0), gp_Pnt(theX2, theY2, 0))));
165 GeomAPI_ProjectPointOnCurve aProj(gp_Pnt(thePointX, thePointY, 0), aLine);
167 Standard_Integer aNbPoint = aProj.NbPoints();
169 gp_Pnt aPoint = aProj.Point(1);
175 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint,
176 Handle_V3d_View theView,
177 FeaturePtr theSketch,
178 const std::list<XGUI_ViewerPrs>& theFeatures)
181 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
182 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
185 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
187 FeaturePtr aDeltaFeature;
188 double aMinDelta = -1;
190 for (; anIt != aLast; anIt++) {
194 double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
195 if (aMinDelta < 0 || aMinDelta > aDelta) {
197 aDeltaFeature = aPrs.feature();
200 return aDeltaFeature;
203 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
204 double theX, double theY)
207 if (theFeature->getKind() != SKETCH_LINE_KIND)
210 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
212 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
213 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
214 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
215 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
218 PartSet_Tools::projectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY);
220 aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0));
225 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
227 return ModelAPI_PluginManager::get()->rootDocument();
230 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
231 const std::string& theAttribute)
235 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
236 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
237 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
239 aPoint->setValue(theX, theY);
242 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
243 const std::string& theAttribute)
247 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
248 boost::shared_ptr<ModelAPI_AttributeDouble> anAttribute =
249 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
251 anAttribute->setValue(theValue);
254 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
255 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
256 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
258 boost::shared_ptr<ModelAPI_Document> aDoc = document();
259 FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
262 boost::shared_ptr<SketchPlugin_Feature> aSketch =
263 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
264 aSketch->addSub(aFeature);
267 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
269 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
270 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
271 aRef1->setAttr(thePoint1);
273 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
274 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
275 aRef2->setAttr(thePoint2);
277 if (aFeature) // TODO: generate an error if feature was not created
281 void PartSet_Tools::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute,
282 double& theX, double& theY)
284 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
286 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
287 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
288 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
293 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
294 double theX, double theY)
296 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
300 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
301 if (theFeature->getKind() == SKETCH_LINE_KIND)
303 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
304 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
305 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
308 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
309 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
313 else if (theFeature->getKind() == SKETCH_POINT_KIND)
315 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
316 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(POINT_ATTR_COORD));
317 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )