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