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_ConstraintCoincidence.h>
21 #include <SketchPlugin_Constraint.h>
23 #include <PartSet_FeatureLinePrs.h>
24 #include <PartSet_FeaturePointPrs.h>
25 #include <PartSet_FeatureCirclePrs.h>
26 #include <PartSet_FeatureArcPrs.h>
28 #include <PartSet_ConstraintLengthPrs.h>
29 #include <PartSet_ConstraintRadiusPrs.h>
30 #include <PartSet_ConstraintDistancePrs.h>
32 #include <XGUI_ViewerPrs.h>
34 #include <V3d_View.hxx>
36 #include <ProjLib.hxx>
38 #include <Geom_Line.hxx>
39 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 const double PRECISION_TOLERANCE = 0.000001;
47 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
52 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
53 theView->Eye(XEye, YEye, ZEye);
55 theView->At(XAt, YAt, ZAt);
56 gp_Pnt EyePoint(XEye, YEye, ZEye);
57 gp_Pnt AtPoint(XAt, YAt, ZAt);
59 gp_Vec EyeVector(EyePoint, AtPoint);
60 gp_Dir EyeDir(EyeVector);
62 gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
63 Standard_Real X, Y, Z;
64 theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
65 gp_Pnt ConvertedPoint(X, Y, Z);
67 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
68 gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
72 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
73 Handle(V3d_View) theView, double& theX, double& theY)
78 AttributeDoublePtr anAttr;
79 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
81 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
82 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
84 boost::shared_ptr<GeomDataAPI_Dir> aX =
85 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
86 boost::shared_ptr<GeomDataAPI_Dir> anY =
87 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
89 gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
90 gp_Vec aVec(anOriginPnt, thePoint);
92 if (!theView.IsNull())
94 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
95 theView->Eye(XEye, YEye, ZEye);
97 theView->At(XAt, YAt, ZAt);
98 gp_Pnt EyePoint(XEye, YEye, ZEye);
99 gp_Pnt AtPoint(XAt, YAt, ZAt);
101 gp_Vec anEyeVec(EyePoint, AtPoint);
102 anEyeVec.Normalize();
104 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
105 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
106 gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
108 double aDen = anEyeVec * aNormalVec;
109 double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
111 gp_Vec aDeltaVec = anEyeVec*aLVec;
112 aVec = aVec - aDeltaVec;
114 theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
115 theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
118 void PartSet_Tools::convertTo3D(const double theX, const double theY,
119 FeaturePtr theSketch,
125 boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
127 boost::shared_ptr<GeomDataAPI_Point> aC =
128 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
129 boost::shared_ptr<GeomDataAPI_Dir> aX =
130 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
131 boost::shared_ptr<GeomDataAPI_Dir> aY =
132 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
134 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
135 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
137 boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
138 thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
141 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
142 FeaturePtr theSketch,
143 FeaturePtr theFeature)
145 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
147 if (theKind == PartSet_FeaturePointPrs::getKind()) {
148 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
150 else if (theKind == PartSet_FeatureLinePrs::getKind()) {
151 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
153 else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
154 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
156 else if (theKind == PartSet_FeatureArcPrs::getKind()) {
157 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
159 else if (theKind == PartSet_ConstraintLengthPrs::getKind()) {
160 aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_ConstraintLengthPrs(theSketch));
162 else if (theKind == PartSet_ConstraintRadiusPrs::getKind()) {
163 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintRadiusPrs>(new PartSet_ConstraintRadiusPrs(theSketch));
165 else if (theKind == PartSet_ConstraintDistancePrs::getKind()) {
166 aFeaturePrs = boost::shared_ptr<PartSet_ConstraintDistancePrs>(new PartSet_ConstraintDistancePrs(theSketch));
170 if (theFeature && aFeaturePrs)
171 aFeaturePrs->init(theFeature);
176 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, 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)
206 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
207 theFeature->getKind(), FeaturePtr(), theFeature);
210 aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
215 void PartSet_Tools::moveFeature(FeaturePtr theFeature, double theDeltaX, double theDeltaY)
220 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
221 theFeature->getKind(), FeaturePtr(), theFeature);
223 aFeaturePrs->move(theDeltaX, theDeltaY);
226 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
228 return ModelAPI_PluginManager::get()->rootDocument();
231 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
232 const std::string& theAttribute)
236 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
237 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
238 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
240 aPoint->setValue(theX, theY);
243 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
244 const std::string& theAttribute)
248 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
249 AttributeDoublePtr anAttribute =
250 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
252 anAttribute->setValue(theValue);
255 bool PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
258 bool aResult = false;
261 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
262 AttributeDoublePtr anAttribute =
263 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
265 theValue = anAttribute->value();
271 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
272 boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
273 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
275 boost::shared_ptr<ModelAPI_Document> aDoc = document();
276 FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
279 boost::shared_ptr<SketchPlugin_Feature> aSketch =
280 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
281 aSketch->addSub(aFeature);
284 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
286 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
287 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
288 aRef1->setAttr(thePoint1);
290 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
291 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
292 aRef2->setAttr(thePoint2);
294 if (aFeature) // TODO: generate an error if feature was not created
298 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
299 double theX, double theY)
301 boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
302 theFeature->getKind(), FeaturePtr(), theFeature);
304 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
306 aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);