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 #include <ModelAPI_Session.h>
12
13 #include <GeomDataAPI_Point.h>
14 #include <GeomDataAPI_Dir.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAPI_Pln.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Pnt.h>
19
20 #include <GeomAPI_Dir.h>
21 #include <GeomAPI_XYZ.h>
22
23 #include <SketchPlugin_Feature.h>
24 #include <SketchPlugin_Sketch.h>
25 #include <SketchPlugin_ConstraintCoincidence.h>
26 #include <SketchPlugin_ConstraintDistance.h>
27 #include <SketchPlugin_ConstraintLength.h>
28 #include <SketchPlugin_ConstraintRadius.h>
29 #include <SketchPlugin_ConstraintRigid.h>
30 #include <SketchPlugin_Constraint.h>
31 #include <SketchPlugin_Circle.h>
32 #include <SketchPlugin_Arc.h>
33 #include <SketchPlugin_Line.h>
34
35 #include <ModuleBase_ViewerPrs.h>
36
37 #include <V3d_View.hxx>
38 #include <gp_Pln.hxx>
39 #include <gp_Circ.hxx>
40 #include <ProjLib.hxx>
41 #include <ElSLib.hxx>
42 #include <Geom_Line.hxx>
43 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <BRep_Tool.hxx>
45 #include <TopoDS.hxx>
46
47 #ifdef _DEBUG
48 #include <QDebug>
49 #endif
50
51 const double PRECISION_TOLERANCE = 0.000001;
52
53 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
54 {
55   if (theView.IsNull())
56     return gp_Pnt();
57
58   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
59   theView->Eye(XEye, YEye, ZEye);
60
61   theView->At(XAt, YAt, ZAt);
62   gp_Pnt EyePoint(XEye, YEye, ZEye);
63   gp_Pnt AtPoint(XAt, YAt, ZAt);
64
65   gp_Vec EyeVector(EyePoint, AtPoint);
66   gp_Dir EyeDir(EyeVector);
67
68   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
69   Standard_Real X, Y, Z;
70   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
71   gp_Pnt ConvertedPoint(X, Y, Z);
72
73   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
74   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
75                                      PlaneOfTheView);
76   return ResultPoint;
77 }
78
79 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
80 Handle(V3d_View) theView,
81                                 double& theX, double& theY)
82 {
83   if (!theSketch)
84     return;
85
86   AttributeDoublePtr anAttr;
87   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
88
89   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
90       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
91
92   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
93       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
94   boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
95       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
96
97   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
98   gp_Vec aVec(anOriginPnt, thePoint);
99
100   if (!theView.IsNull()) {
101     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
102     theView->Eye(XEye, YEye, ZEye);
103
104     theView->At(XAt, YAt, ZAt);
105     gp_Pnt EyePoint(XEye, YEye, ZEye);
106     gp_Pnt AtPoint(XAt, YAt, ZAt);
107
108     gp_Vec anEyeVec(EyePoint, AtPoint);
109     anEyeVec.Normalize();
110
111     boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
112         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
113     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
114
115     double aDen = anEyeVec * aNormalVec;
116     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
117
118     gp_Vec aDeltaVec = anEyeVec * aLVec;
119     aVec = aVec - aDeltaVec;
120   }
121   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
122   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
123 }
124
125 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
126                                 gp_Pnt& thePoint)
127 {
128   if (!theSketch)
129     return;
130
131   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
132
133   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
134       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
135   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
136       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
137   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
138       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
139
140   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
141       ->added(aY->dir()->xyz()->multiplied(theY));
142
143   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
144   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
145 }
146
147 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
148                                          FeaturePtr theSketch,
149                                          const QList<ModuleBase_ViewerPrs>& theFeatures)
150 {
151   double aX, anY;
152   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
153   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
154
155   FeaturePtr aFeature;
156   FeaturePtr aDeltaFeature;
157   double aMinDelta = -1;
158   ModuleBase_ViewerPrs aPrs;
159   foreach (ModuleBase_ViewerPrs aPrs, theFeatures) {
160     if (!aPrs.object())
161       continue;
162     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
163         SketchPlugin_Feature>(aPrs.object());
164     if (!aSketchFeature)
165       continue;
166     double aDelta = aSketchFeature->distanceToPoint(
167         boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
168     if (aMinDelta < 0 || aMinDelta > aDelta) {
169       aMinDelta = aDelta;
170       // TODO aDeltaFeature = aPrs.result();
171     }
172   }
173   return aDeltaFeature;
174 }
175
176 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
177 {
178   return ModelAPI_Session::get()->moduleDocument();
179 }
180
181 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::getFeaturePoint(FeaturePtr theFeature,
182                                                                       double theX, double theY)
183 {
184   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
185                                                                  new GeomAPI_Pnt2d(theX, theY));
186   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes =
187                                     theFeature->data()->attributes(GeomDataAPI_Point2D::type());
188   std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
189                                                                     aLast = anAttiributes.end();
190   boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
191   for (; anIt != aLast && !aFPoint; anIt++) {
192     boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
193         GeomDataAPI_Point2D>(*anIt);
194     if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
195       aFPoint = aCurPoint;
196   }
197
198   return aFPoint;
199 }
200
201 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
202                                     const std::string& theAttribute)
203 {
204   if (!theFeature)
205     return;
206   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
207   AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
208       aData->attribute(theAttribute));
209   if (anAttribute)
210     anAttribute->setValue(theValue);
211 }
212
213 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
214                                    bool& isValid)
215 {
216   isValid = false;
217   double aValue;
218   if (theFeature) {
219     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
220     AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
221         aData->attribute(theAttribute));
222     if (anAttribute) {
223       aValue = anAttribute->value();
224       isValid = true;
225     }
226   }
227   return aValue;
228 }
229
230 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
231                                   const std::string& theKind)
232 {
233   FeaturePtr aFeature;
234   if (!theFeature)
235     return aFeature;
236
237   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
238   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
239       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
240   if (anAttr) {
241     aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
242     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
243       aFeature = FeaturePtr();
244     }
245   }
246   return aFeature;
247 }
248
249 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
250                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
251                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
252 {
253   FeaturePtr aFeature;
254   if (theSketch) {
255     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
256   } else {
257     boost::shared_ptr<ModelAPI_Document> aDoc = document();
258     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
259   }
260
261   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
262
263   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
264       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
265   aRef1->setAttr(thePoint1);
266
267   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
268       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
269   aRef2->setAttr(thePoint2);
270
271   if (aFeature)  // TODO: generate an error if feature was not created
272     aFeature->execute();
273 }
274
275 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
276                                    const std::string& theAttribute, double theClickedX,
277                                    double theClickedY)
278 {
279   // find a feature point by the selection mode
280   //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
281   boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
282       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
283   if (!aFeaturePoint)
284     return;
285
286   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
287   // the constraint is created between the feature point and the found sketch point
288   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
289   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
290       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
291
292   std::list<ObjectPtr> aFeatures = aRefList->list();
293   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
294   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
295   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
296       new GeomAPI_Pnt2d(theClickedX, theClickedY));
297   for (; anIt != aLast; anIt++) {
298     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
299     // find the given point in the feature attributes
300     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
301     std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
302         aLast = anAttiributes.end();
303     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
304     for (; anIt != aLast && !aFPoint; anIt++) {
305       boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
306           GeomDataAPI_Point2D>(*anIt);
307       if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
308         aFPoint = aCurPoint;
309     }
310     if (aFPoint)
311       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
312   }
313 }
314
315 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr 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 = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
322       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
323   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
324       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
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(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
335                                                       CompositeFeaturePtr theSketch)
336 {
337   boost::shared_ptr<GeomAPI_Pnt> aPoint;
338   if (!theSketch || !thePoint2D)
339     return aPoint;
340
341   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
342       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
343   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
344       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
345   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
346       theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
347
348   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
349 }
350
351 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
352 {
353   return theKind == SketchPlugin_ConstraintDistance::ID()
354       || theKind == SketchPlugin_ConstraintLength::ID()
355       || theKind == SketchPlugin_ConstraintRadius::ID()
356       || theKind == SketchPlugin_ConstraintRigid::ID();
357 }
358
359 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
360 {
361   TopoDS_Shape aShape = thePrs.shape();
362   if (aShape.ShapeType() != TopAbs_EDGE)
363     return ResultPtr();
364
365   Standard_Real aStart, aEnd;
366   Handle(V3d_View) aNullView;
367   FeaturePtr myFeature;
368
369   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
370   GeomAdaptor_Curve aAdaptor(aCurve);
371   if (aAdaptor.GetType() == GeomAbs_Line) {
372     // Create line
373     myFeature = theSketch->addFeature(SketchPlugin_Line::ID());
374
375     //DataPtr aData = myFeature->data();
376     //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
377     //  boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
378
379     //double aX, aY;
380     //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
381     //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
382     //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
383
384     //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
385     //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
386     //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
387   } else if (aAdaptor.GetType() == GeomAbs_Circle) {
388     if (aAdaptor.IsClosed()) {
389       // Create circle
390       myFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
391       //gp_Circ aCirc = aAdaptor.Circle();
392       //gp_Pnt aCenter = aCirc.Location();
393
394       //double aX, aY;
395       //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
396       //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
397       //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
398     } else {
399       // Create arc
400       myFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
401     }
402   }
403   if (myFeature) {
404     DataPtr aData = myFeature->data();
405     AttributeSelectionPtr anAttr = 
406       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
407       (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
408
409     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
410     if (anAttr && aRes) {
411       boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
412       anEdge->setImpl(new TopoDS_Shape(aShape));
413
414       anAttr->setValue(aRes, anEdge);
415       myFeature->execute();
416       return myFeature->firstResult();
417     }
418   }
419   return ResultPtr();
420 }