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>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_Pnt2d.h>
16 #include <GeomAPI_Pnt.h>
18 #include <GeomAPI_Dir.h>
19 #include <GeomAPI_XYZ.h>
21 #include <SketchPlugin_Feature.h>
22 #include <SketchPlugin_Sketch.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_Constraint.h>
26 #include <PartSet_FeatureLinePrs.h>
27 #include <PartSet_FeaturePointPrs.h>
28 #include <PartSet_FeatureCirclePrs.h>
29 #include <PartSet_FeatureArcPrs.h>
31 #include <PartSet_ConstraintLengthPrs.h>
32 #include <PartSet_ConstraintRadiusPrs.h>
33 #include <PartSet_ConstraintDistancePrs.h>
35 #include <XGUI_ViewerPrs.h>
37 #include <V3d_View.hxx>
39 #include <ProjLib.hxx>
41 #include <Geom_Line.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
48 const double PRECISION_TOLERANCE = 0.000001;
50 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
55 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
56 theView->Eye(XEye, YEye, ZEye);
58 theView->At(XAt, YAt, ZAt);
59 gp_Pnt EyePoint(XEye, YEye, ZEye);
60 gp_Pnt AtPoint(XAt, YAt, ZAt);
62 gp_Vec EyeVector(EyePoint, AtPoint);
63 gp_Dir EyeDir(EyeVector);
65 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
66 Standard_Real X, Y, Z;
67 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
68 gp_Pnt ConvertedPoint(X, Y, Z);
70 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
71 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
75 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
76 Handle(V3d_View) theView, double& theX, double& theY)
81 AttributeDoublePtr anAttr;
82 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
84 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
85 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
87 boost::shared_ptr<GeomDataAPI_Dir> aX =
88 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
89 boost::shared_ptr<GeomDataAPI_Dir> anY =
90 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
92 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
93 gp_Vec aVec(anOriginPnt, thePoint);
95 if (!theView.IsNull())
97 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
98 theView->Eye(XEye, YEye, ZEye);
100 theView->At(XAt, YAt, ZAt);
101 gp_Pnt EyePoint(XEye, YEye, ZEye);
102 gp_Pnt AtPoint(XAt, YAt, ZAt);
104 gp_Vec anEyeVec(EyePoint, AtPoint);
105 anEyeVec.Normalize();
107 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
108 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
109 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
111 double aDen = anEyeVec * aNormalVec;
112 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
114 gp_Vec aDeltaVec = anEyeVec*aLVec;
115 aVec = aVec - aDeltaVec;
117 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
118 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
121 void PartSet_Tools::convertTo3D(const double theX, const double theY,
122 FeaturePtr theSketch,
128 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
130 boost::shared_ptr<GeomDataAPI_Point> aC =
131 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
132 boost::shared_ptr<GeomDataAPI_Dir> aX =
133 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
134 boost::shared_ptr<GeomDataAPI_Dir> aY =
135 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
137 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
138 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
140 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
141 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
144 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
145 FeaturePtr theSketch,
146 FeaturePtr theFeature)
148 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
150 if (theKind == PartSet_FeaturePointPrs::getKind()) {
151 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
153 else if (theKind == PartSet_FeatureLinePrs::getKind()) {
154 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
156 else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
157 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
159 else if (theKind == PartSet_FeatureArcPrs::getKind()) {
160 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
162 else if (theKind == PartSet_ConstraintLengthPrs::getKind()) {
163 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_ConstraintLengthPrs(theSketch));
165 else if (theKind == PartSet_ConstraintRadiusPrs::getKind()) {
166 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintRadiusPrs>(new PartSet_ConstraintRadiusPrs(theSketch));
168 else if (theKind == PartSet_ConstraintDistancePrs::getKind()) {
169 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintDistancePrs>(new PartSet_ConstraintDistancePrs(theSketch));
173 if (theFeature && aFeaturePrs)
174 aFeaturePrs->init(theFeature);
179 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
180 FeaturePtr theSketch,
181 const std::list<XGUI_ViewerPrs>& theFeatures)
184 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
185 PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
188 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
190 FeaturePtr aDeltaFeature;
191 double aMinDelta = -1;
193 for (; anIt != aLast; anIt++) {
197 double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
198 if (aMinDelta < 0 || aMinDelta > aDelta) {
200 aDeltaFeature = aPrs.feature();
203 return aDeltaFeature;
206 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
207 double theX, double theY)
209 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
210 theFeature->getKind(), FeaturePtr(), theFeature);
213 aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
218 void PartSet_Tools::moveFeature(FeaturePtr theFeature, double theDeltaX, double theDeltaY)
223 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
224 theFeature->getKind(), FeaturePtr(), theFeature);
226 aFeaturePrs->move(theDeltaX, theDeltaY);
229 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
231 return ModelAPI_PluginManager::get()->rootDocument();
234 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
235 const std::string& theAttribute)
239 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
240 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
241 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
243 aPoint->setValue(theX, theY);
246 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
247 const std::string& theAttribute)
251 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
252 AttributeDoublePtr anAttribute =
253 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
255 anAttribute->setValue(theValue);
258 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
264 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
265 AttributeDoublePtr anAttribute =
266 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
268 aValue = anAttribute->value();
275 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
276 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
277 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
279 boost::shared_ptr<ModelAPI_Document> aDoc = document();
280 FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
283 boost::shared_ptr<SketchPlugin_Feature> aSketch =
284 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
285 aSketch->addSub(aFeature);
288 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
290 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
291 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
292 aRef1->setAttr(thePoint1);
294 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
295 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
296 aRef2->setAttr(thePoint2);
298 if (aFeature) // TODO: generate an error if feature was not created
302 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
303 double theX, double theY)
305 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
306 theFeature->getKind(), FeaturePtr(), theFeature);
308 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
310 aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
315 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
317 boost::shared_ptr<GeomAPI_Pln> aPlane;
318 double aA, aB, aC, aD;
320 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
321 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
322 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
323 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
324 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
330 aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
334 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
335 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
336 FeaturePtr theSketch)
338 boost::shared_ptr<GeomAPI_Pnt> aPoint;
339 if (!theSketch || !thePoint2D)
342 boost::shared_ptr<GeomDataAPI_Point> aC =
343 boost::dynamic_pointer_cast<GeomDataAPI_Point>(theSketch->data()->attribute(SKETCH_ATTR_ORIGIN));
344 boost::shared_ptr<GeomDataAPI_Dir> aX =
345 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRX));
346 boost::shared_ptr<GeomDataAPI_Dir> aY =
347 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRY));
349 return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());