]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
45b994e524126691a086cd1816b4c9d6b57ac00f
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
1 // File:        PartSet_Tools.h
2 // Created:     28 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_Tools.h>
6
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_Document.h>
10
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>
17
18 #include <GeomAPI_Dir.h>
19 #include <GeomAPI_XYZ.h>
20
21 #include <SketchPlugin_Feature.h>
22 #include <SketchPlugin_Sketch.h>
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_Constraint.h>
25
26 #include <PartSet_FeatureLinePrs.h>
27 #include <PartSet_FeaturePointPrs.h>
28 #include <PartSet_FeatureCirclePrs.h>
29 #include <PartSet_FeatureArcPrs.h>
30
31 #include <PartSet_ConstraintLengthPrs.h>
32 #include <PartSet_ConstraintRadiusPrs.h>
33 #include <PartSet_ConstraintDistancePrs.h>
34
35 #include <XGUI_ViewerPrs.h>
36
37 #include <V3d_View.hxx>
38 #include <gp_Pln.hxx>
39 #include <ProjLib.hxx>
40 #include <ElSLib.hxx>
41 #include <Geom_Line.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
43
44 #ifdef _DEBUG
45 #include <QDebug>
46 #endif
47
48 const double PRECISION_TOLERANCE = 0.000001;
49
50 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
51 {
52   if (theView.IsNull())
53     return gp_Pnt();
54
55   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
56   theView->Eye(XEye, YEye, ZEye);
57
58   theView->At(XAt, YAt, ZAt);
59   gp_Pnt EyePoint(XEye, YEye, ZEye);
60   gp_Pnt AtPoint(XAt, YAt, ZAt);
61
62   gp_Vec EyeVector(EyePoint, AtPoint);
63   gp_Dir EyeDir(EyeVector);
64
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);
69
70   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
71   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
72   return ResultPoint;
73 }
74
75 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
76                                 Handle(V3d_View) theView, double& theX, double& theY)
77 {
78   if (!theSketch)
79     return;
80
81   AttributeDoublePtr anAttr;
82   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
83
84   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
85     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
86
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));
91
92   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
93   gp_Vec aVec(anOriginPnt, thePoint);
94
95   if (!theView.IsNull())
96   {
97     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
98     theView->Eye(XEye, YEye, ZEye);
99
100     theView->At(XAt, YAt, ZAt);
101     gp_Pnt EyePoint(XEye, YEye, ZEye);
102     gp_Pnt AtPoint(XAt, YAt, ZAt);
103
104     gp_Vec anEyeVec(EyePoint, AtPoint);
105     anEyeVec.Normalize();
106
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());
110
111     double aDen = anEyeVec * aNormalVec;
112     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
113
114     gp_Vec aDeltaVec = anEyeVec*aLVec;
115     aVec = aVec - aDeltaVec;
116   }
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();
119 }
120
121 void PartSet_Tools::convertTo3D(const double theX, const double theY,
122                                 FeaturePtr theSketch,
123                                 gp_Pnt& thePoint)
124 {
125   if (!theSketch)
126     return;
127
128   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
129
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));
136
137   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
138     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
139
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());
142 }
143
144 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
145                                                                       FeaturePtr theSketch,
146                                                                       FeaturePtr theFeature)
147 {
148   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
149
150   if (theKind == PartSet_FeaturePointPrs::getKind()) {
151     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
152   }
153   else if (theKind == PartSet_FeatureLinePrs::getKind()) {
154     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
155   }
156   else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
157     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
158   }
159   else if (theKind == PartSet_FeatureArcPrs::getKind()) {
160     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
161   }
162   else if (theKind == PartSet_ConstraintLengthPrs::getKind()) {
163     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_ConstraintLengthPrs(theSketch));
164   }
165   else if (theKind == PartSet_ConstraintRadiusPrs::getKind()) {
166     aFeaturePrs = boost::shared_ptr<PartSet_ConstraintRadiusPrs>(new PartSet_ConstraintRadiusPrs(theSketch));
167   }
168   else if (theKind == PartSet_ConstraintDistancePrs::getKind()) {
169     aFeaturePrs = boost::shared_ptr<PartSet_ConstraintDistancePrs>(new PartSet_ConstraintDistancePrs(theSketch));
170   }
171
172
173   if (theFeature && aFeaturePrs)
174     aFeaturePrs->init(theFeature);
175
176   return aFeaturePrs;
177 }
178
179 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
180                                          FeaturePtr theSketch,
181                                          const std::list<XGUI_ViewerPrs>& theFeatures)
182 {
183   double aX, anY;
184   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
185   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
186
187   FeaturePtr aFeature;
188   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
189
190   FeaturePtr aDeltaFeature;   
191   double aMinDelta = -1;
192   XGUI_ViewerPrs aPrs;
193   for (; anIt != aLast; anIt++) {
194     aPrs = *anIt;
195     if (!aPrs.feature())
196       continue;
197     double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
198     if (aMinDelta < 0 || aMinDelta > aDelta) {
199       aMinDelta = aDelta;
200       aDeltaFeature = aPrs.feature();
201     }
202   }
203   return aDeltaFeature;
204 }
205
206 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
207                                       double theX, double theY)
208 {
209   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
210                                            theFeature->getKind(), FeaturePtr(), theFeature);
211   double aDelta = 0;
212   if (aFeaturePrs)
213     aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
214
215   return aDelta;
216 }
217
218 void PartSet_Tools::moveFeature(FeaturePtr theFeature, double theDeltaX, double theDeltaY)
219 {
220   if (!theFeature)
221     return;
222
223   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
224                                            theFeature->getKind(), FeaturePtr(), theFeature);
225   if (aFeaturePrs)
226   aFeaturePrs->move(theDeltaX, theDeltaY);
227 }
228
229 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
230 {
231   return ModelAPI_PluginManager::get()->rootDocument();
232 }
233
234 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
235                                     const std::string& theAttribute)
236 {
237   if (!theFeature)
238     return;
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));
242   if (aPoint)
243     aPoint->setValue(theX, theY);
244 }
245
246 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
247                                     const std::string& theAttribute)
248 {
249   if (!theFeature)
250     return;
251   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
252   AttributeDoublePtr anAttribute =
253         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
254   if (anAttribute)
255     anAttribute->setValue(theValue);
256 }
257
258 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
259                                    bool& isValid)
260 {
261   isValid = false;
262   double aValue;
263   if (theFeature) {
264     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
265     AttributeDoublePtr anAttribute =
266           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
267     if (anAttribute) {
268       aValue = anAttribute->value();
269       isValid = true;
270     }
271   }
272   return aValue;
273 }
274
275 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
276                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
277                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
278 {
279   boost::shared_ptr<ModelAPI_Document> aDoc = document();
280   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
281
282   if (theSketch) {
283     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
284                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
285     aSketch->addSub(aFeature);
286   }
287
288   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
289
290   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
291         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
292   aRef1->setAttr(thePoint1);
293
294   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
295         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
296   aRef2->setAttr(thePoint2);
297
298   if (aFeature) // TODO: generate an error if feature was not created
299     aFeature->execute();
300 }
301
302 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
303                                                                 double theX, double theY)
304 {
305   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
306                                            theFeature->getKind(), FeaturePtr(), theFeature);
307
308   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
309   if (aFeaturePrs)
310     aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
311
312   return aPoint2D;
313 }
314
315 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
316 {
317   boost::shared_ptr<GeomAPI_Pln> aPlane;
318   double aA, aB, aC, aD;
319
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));
325   aA = aNormal->x();
326   aB = aNormal->y();
327   aC = aNormal->z();
328   aD = 0;
329
330   aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
331   return aPlane;
332 }
333
334 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
335                                                 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
336                                                 FeaturePtr theSketch)
337 {
338   boost::shared_ptr<GeomAPI_Pnt> aPoint;
339   if (!theSketch || !thePoint2D)
340     return aPoint;
341
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));
348
349   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
350 }