Salome HOME
Bugfix for issue #107 which also works for issue #82
[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 <ModuleBase_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(),
67                                      PlaneOfTheView);
68   return ResultPoint;
69 }
70
71 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
72 Handle(V3d_View) theView,
73                                 double& theX, double& theY)
74 {
75   if (!theSketch)
76     return;
77
78   AttributeDoublePtr anAttr;
79   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
80
81   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
82       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
83
84   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
85       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
86   boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
87       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
88
89   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
90   gp_Vec aVec(anOriginPnt, thePoint);
91
92   if (!theView.IsNull()) {
93     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
94     theView->Eye(XEye, YEye, ZEye);
95
96     theView->At(XAt, YAt, ZAt);
97     gp_Pnt EyePoint(XEye, YEye, ZEye);
98     gp_Pnt AtPoint(XAt, YAt, ZAt);
99
100     gp_Vec anEyeVec(EyePoint, AtPoint);
101     anEyeVec.Normalize();
102
103     boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
104         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
105     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
106
107     double aDen = anEyeVec * aNormalVec;
108     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
109
110     gp_Vec aDeltaVec = anEyeVec * aLVec;
111     aVec = aVec - aDeltaVec;
112   }
113   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
114   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
115 }
116
117 void PartSet_Tools::convertTo3D(const double theX, const double theY, 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 = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
126       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
127   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
128       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
129   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
130       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
131
132   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
133       ->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<ModuleBase_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<ModuleBase_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures
149       .end();
150
151   FeaturePtr aDeltaFeature;
152   double aMinDelta = -1;
153   ModuleBase_ViewerPrs aPrs;
154   for (; anIt != aLast; anIt++) {
155     aPrs = *anIt;
156     if (!aPrs.object())
157       continue;
158     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
159         SketchPlugin_Feature>(aPrs.object());
160     if (!aSketchFeature)
161       continue;
162     double aDelta = aSketchFeature->distanceToPoint(
163         boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
164     if (aMinDelta < 0 || aMinDelta > aDelta) {
165       aMinDelta = aDelta;
166       // TODO aDeltaFeature = aPrs.result();
167     }
168   }
169   return aDeltaFeature;
170 }
171
172 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
173 {
174   return ModelAPI_Session::get()->moduleDocument();
175 }
176
177 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
178                                     const std::string& theAttribute)
179 {
180   if (!theFeature)
181     return;
182   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
183   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
184       aData->attribute(theAttribute));
185   if (aPoint)
186     aPoint->setValue(theX, theY);
187 }
188
189 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
190                                     const std::string& theAttribute)
191 {
192   if (!theFeature)
193     return;
194   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
195   AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
196       aData->attribute(theAttribute));
197   if (anAttribute)
198     anAttribute->setValue(theValue);
199 }
200
201 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
202                                    bool& isValid)
203 {
204   isValid = false;
205   double aValue;
206   if (theFeature) {
207     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
208     AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
209         aData->attribute(theAttribute));
210     if (anAttribute) {
211       aValue = anAttribute->value();
212       isValid = true;
213     }
214   }
215   return aValue;
216 }
217
218 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
219                                   const std::string& theKind)
220 {
221   FeaturePtr aFeature;
222   if (!theFeature)
223     return aFeature;
224
225   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
226   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
227       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
228   if (anAttr) {
229     aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
230     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
231       aFeature = FeaturePtr();
232     }
233   }
234   return aFeature;
235 }
236
237 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
238                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
239                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
240 {
241   boost::shared_ptr<ModelAPI_Document> aDoc = document();
242   FeaturePtr aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
243
244   if (theSketch) {
245     boost::shared_ptr<SketchPlugin_Feature> aSketch = boost::dynamic_pointer_cast<
246         SketchPlugin_Feature>(theSketch);
247     aSketch->addSub(aFeature);
248   }
249
250   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
251
252   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
253       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
254   aRef1->setAttr(thePoint1);
255
256   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
257       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
258   aRef2->setAttr(thePoint2);
259
260   if (aFeature)  // TODO: generate an error if feature was not created
261     aFeature->execute();
262 }
263
264 void PartSet_Tools::setConstraints(FeaturePtr theSketch, FeaturePtr theFeature,
265                                    const std::string& theAttribute, double theClickedX,
266                                    double theClickedY)
267 {
268   // find a feature point by the selection mode
269   //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
270   boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
271       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
272   if (!aFeaturePoint)
273     return;
274
275   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
276   // the constraint is created between the feature point and the found sketch point
277   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
278   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
279       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
280
281   std::list<ObjectPtr> aFeatures = aRefList->list();
282   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
283   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
284   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
285       new GeomAPI_Pnt2d(theClickedX, theClickedY));
286   for (; anIt != aLast; anIt++) {
287     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*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 = boost::dynamic_pointer_cast<
295           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 = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
311       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
312   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
313       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
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(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
324                                                       FeaturePtr theSketch)
325 {
326   boost::shared_ptr<GeomAPI_Pnt> aPoint;
327   if (!theSketch || !thePoint2D)
328     return aPoint;
329
330   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
331       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
332   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
333       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
334   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
335       theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
336
337   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
338 }
339
340 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
341 {
342   return theKind == SketchPlugin_ConstraintDistance::ID()
343       || theKind == SketchPlugin_ConstraintLength::ID()
344       || theKind == SketchPlugin_ConstraintRadius::ID();
345 }