Salome HOME
Merge branch 'master' of newgeom:newgeom
[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_AttributeRefList.h>
10 #include <ModelAPI_Document.h>
11
12 #include <GeomDataAPI_Point.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <GeomAPI_Pln.h>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Pnt.h>
18
19 #include <GeomAPI_Dir.h>
20 #include <GeomAPI_XYZ.h>
21
22 #include <SketchPlugin_Feature.h>
23 #include <SketchPlugin_Sketch.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintDistance.h>
26 #include <SketchPlugin_ConstraintLength.h>
27 #include <SketchPlugin_ConstraintRadius.h>
28 #include <SketchPlugin_Constraint.h>
29
30 #include <XGUI_ViewerPrs.h>
31
32 #include <V3d_View.hxx>
33 #include <gp_Pln.hxx>
34 #include <ProjLib.hxx>
35 #include <ElSLib.hxx>
36 #include <Geom_Line.hxx>
37 #include <GeomAPI_ProjectPointOnCurve.hxx>
38
39 #ifdef _DEBUG
40 #include <QDebug>
41 #endif
42
43 const double PRECISION_TOLERANCE = 0.000001;
44
45 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
46 {
47   if (theView.IsNull())
48     return gp_Pnt();
49
50   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
51   theView->Eye(XEye, YEye, ZEye);
52
53   theView->At(XAt, YAt, ZAt);
54   gp_Pnt EyePoint(XEye, YEye, ZEye);
55   gp_Pnt AtPoint(XAt, YAt, ZAt);
56
57   gp_Vec EyeVector(EyePoint, AtPoint);
58   gp_Dir EyeDir(EyeVector);
59
60   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
61   Standard_Real X, Y, Z;
62   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
63   gp_Pnt ConvertedPoint(X, Y, Z);
64
65   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
66   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
67   return ResultPoint;
68 }
69
70 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
71                                 Handle(V3d_View) theView, double& theX, double& theY)
72 {
73   if (!theSketch)
74     return;
75
76   AttributeDoublePtr anAttr;
77   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
78
79   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
80     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
81
82   boost::shared_ptr<GeomDataAPI_Dir> aX = 
83     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
84   boost::shared_ptr<GeomDataAPI_Dir> anY = 
85     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
86
87   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
88   gp_Vec aVec(anOriginPnt, thePoint);
89
90   if (!theView.IsNull())
91   {
92     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
93     theView->Eye(XEye, YEye, ZEye);
94
95     theView->At(XAt, YAt, ZAt);
96     gp_Pnt EyePoint(XEye, YEye, ZEye);
97     gp_Pnt AtPoint(XAt, YAt, ZAt);
98
99     gp_Vec anEyeVec(EyePoint, AtPoint);
100     anEyeVec.Normalize();
101
102     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
103                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
104     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
105
106     double aDen = anEyeVec * aNormalVec;
107     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
108
109     gp_Vec aDeltaVec = anEyeVec*aLVec;
110     aVec = aVec - aDeltaVec;
111   }
112   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
113   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
114 }
115
116 void PartSet_Tools::convertTo3D(const double theX, const double theY,
117                                 FeaturePtr theSketch,
118                                 gp_Pnt& thePoint)
119 {
120   if (!theSketch)
121     return;
122
123   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
124
125   boost::shared_ptr<GeomDataAPI_Point> aC = 
126     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
127   boost::shared_ptr<GeomDataAPI_Dir> aX = 
128     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
129   boost::shared_ptr<GeomDataAPI_Dir> aY = 
130     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
131
132   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
133     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
134
135   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
136   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
137 }
138
139 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
140                                          FeaturePtr theSketch,
141                                          const std::list<XGUI_ViewerPrs>& theFeatures)
142 {
143   double aX, anY;
144   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
145   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
146
147   FeaturePtr aFeature;
148   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
149
150   FeaturePtr aDeltaFeature;   
151   double aMinDelta = -1;
152   XGUI_ViewerPrs aPrs;
153   for (; anIt != aLast; anIt++) {
154     aPrs = *anIt;
155     if (!aPrs.feature())
156       continue;
157     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = 
158                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(aPrs.feature());
159     if (!aSketchFeature)
160       continue;
161     double aDelta = aSketchFeature->distanceToPoint(
162                                boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
163     if (aMinDelta < 0 || aMinDelta > aDelta) {
164       aMinDelta = aDelta;
165       aDeltaFeature = aPrs.feature();
166     }
167   }
168   return aDeltaFeature;
169 }
170
171 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
172 {
173   return ModelAPI_PluginManager::get()->rootDocument();
174 }
175
176 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
177                                     const std::string& theAttribute)
178 {
179   if (!theFeature)
180     return;
181   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
182   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
183         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
184   if (aPoint)
185     aPoint->setValue(theX, theY);
186 }
187
188 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
189                                     const std::string& theAttribute)
190 {
191   if (!theFeature)
192     return;
193   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
194   AttributeDoublePtr anAttribute =
195         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
196   if (anAttribute)
197     anAttribute->setValue(theValue);
198 }
199
200 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
201                                    bool& isValid)
202 {
203   isValid = false;
204   double aValue;
205   if (theFeature) {
206     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
207     AttributeDoublePtr anAttribute =
208           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
209     if (anAttribute) {
210       aValue = anAttribute->value();
211       isValid = true;
212     }
213   }
214   return aValue;
215 }
216
217 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
218                                   const std::string& theKind)
219 {
220   FeaturePtr aFeature;
221   if (!theFeature)
222     return aFeature;
223
224   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
225   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
226           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
227   if (anAttr) {
228     aFeature = anAttr->feature();
229     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
230       aFeature = FeaturePtr();
231     }
232   }
233   return aFeature;
234 }
235
236 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
237                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
238                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
239 {
240   boost::shared_ptr<ModelAPI_Document> aDoc = document();
241   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
242
243   if (theSketch) {
244     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
245                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
246     aSketch->addSub(aFeature);
247   }
248
249   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
250
251   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
252         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
253   aRef1->setAttr(thePoint1);
254
255   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
256         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
257   aRef2->setAttr(thePoint2);
258
259   if (aFeature) // TODO: generate an error if feature was not created
260     aFeature->execute();
261 }
262
263 void PartSet_Tools::setConstraints(FeaturePtr theSketch, FeaturePtr theFeature,
264                                    const std::string& theAttribute,
265                                    double theClickedX, double theClickedY)
266 {
267   // find a feature point by the selection mode
268   //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
269   boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint =
270         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
271   if (!aFeaturePoint)
272     return;
273
274   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
275   // the constraint is created between the feature point and the found sketch point
276   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
277   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
278         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
279
280   std::list<FeaturePtr > aFeatures = aRefList->list();
281   std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
282   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
283   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>
284                                                      (new GeomAPI_Pnt2d(theClickedX, theClickedY));
285   for (; anIt != aLast; anIt++)
286   {
287     FeaturePtr aFeature = *anIt;
288     // find the given point in the feature attributes
289     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
290     std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
291                                                                       aLast = anAttiributes.end();
292     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
293     for (;anIt!=aLast && !aFPoint; anIt++) {
294       boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint =
295                                           boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
296       if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
297         aFPoint = aCurPoint;
298     }
299     if (aFPoint)
300       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
301   }
302 }
303
304 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(FeaturePtr theSketch)
305 {
306   boost::shared_ptr<GeomAPI_Pln> aPlane;
307   double aA, aB, aC, aD;
308
309   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
310   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
311     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
312   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
313     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
314   aA = aNormal->x();
315   aB = aNormal->y();
316   aC = aNormal->z();
317   aD = 0;
318
319   aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
320   return aPlane;
321 }
322
323 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(
324                                                 boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
325                                                 FeaturePtr theSketch)
326 {
327   boost::shared_ptr<GeomAPI_Pnt> aPoint;
328   if (!theSketch || !thePoint2D)
329     return aPoint;
330
331   boost::shared_ptr<GeomDataAPI_Point> aC = 
332     boost::dynamic_pointer_cast<GeomDataAPI_Point>(theSketch->data()->attribute(SKETCH_ATTR_ORIGIN));
333   boost::shared_ptr<GeomDataAPI_Dir> aX = 
334     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRX));
335   boost::shared_ptr<GeomDataAPI_Dir> aY = 
336     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(theSketch->data()->attribute(SKETCH_ATTR_DIRY));
337
338   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
339 }
340
341 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
342 {
343   return theKind == SKETCH_CONSTRAINT_DISTANCE_KIND ||
344          theKind == SKETCH_CONSTRAINT_LENGTH_KIND ||
345          theKind == SKETCH_CONSTRAINT_RADIUS_KIND;
346 }