]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
892f610a0d36e43ebc4511f884184f9c97cc98bb
[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_ConstraintRigid.h>
29 #include <SketchPlugin_Constraint.h>
30
31 #include <ModuleBase_ViewerPrs.h>
32
33 #include <V3d_View.hxx>
34 #include <gp_Pln.hxx>
35 #include <ProjLib.hxx>
36 #include <ElSLib.hxx>
37 #include <Geom_Line.hxx>
38 #include <GeomAPI_ProjectPointOnCurve.hxx>
39
40 #ifdef _DEBUG
41 #include <QDebug>
42 #endif
43
44 const double PRECISION_TOLERANCE = 0.000001;
45
46 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
47 {
48   if (theView.IsNull())
49     return gp_Pnt();
50
51   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
52   theView->Eye(XEye, YEye, ZEye);
53
54   theView->At(XAt, YAt, ZAt);
55   gp_Pnt EyePoint(XEye, YEye, ZEye);
56   gp_Pnt AtPoint(XAt, YAt, ZAt);
57
58   gp_Vec EyeVector(EyePoint, AtPoint);
59   gp_Dir EyeDir(EyeVector);
60
61   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
62   Standard_Real X, Y, Z;
63   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
64   gp_Pnt ConvertedPoint(X, Y, Z);
65
66   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
67   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(),
68                                      PlaneOfTheView);
69   return ResultPoint;
70 }
71
72 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
73 Handle(V3d_View) theView,
74                                 double& theX, double& theY)
75 {
76   if (!theSketch)
77     return;
78
79   AttributeDoublePtr anAttr;
80   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
81
82   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
83       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
84
85   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
86       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
87   boost::shared_ptr<GeomDataAPI_Dir> anY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
88       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
89
90   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
91   gp_Vec aVec(anOriginPnt, thePoint);
92
93   if (!theView.IsNull()) {
94     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
95     theView->Eye(XEye, YEye, ZEye);
96
97     theView->At(XAt, YAt, ZAt);
98     gp_Pnt EyePoint(XEye, YEye, ZEye);
99     gp_Pnt AtPoint(XAt, YAt, ZAt);
100
101     gp_Vec anEyeVec(EyePoint, AtPoint);
102     anEyeVec.Normalize();
103
104     boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
105         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
106     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
107
108     double aDen = anEyeVec * aNormalVec;
109     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
110
111     gp_Vec aDeltaVec = anEyeVec * aLVec;
112     aVec = aVec - aDeltaVec;
113   }
114   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
115   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
116 }
117
118 void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
119                                 gp_Pnt& thePoint)
120 {
121   if (!theSketch)
122     return;
123
124   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
125
126   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
127       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
128   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
129       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
130   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
131       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
132
133   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
134       ->added(aY->dir()->xyz()->multiplied(theY));
135
136   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
137   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
138 }
139
140 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
141                                          FeaturePtr theSketch,
142                                          const std::list<ModuleBase_ViewerPrs>& theFeatures)
143 {
144   double aX, anY;
145   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
146   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
147
148   FeaturePtr aFeature;
149   std::list<ModuleBase_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures
150       .end();
151
152   FeaturePtr aDeltaFeature;
153   double aMinDelta = -1;
154   ModuleBase_ViewerPrs aPrs;
155   for (; anIt != aLast; anIt++) {
156     aPrs = *anIt;
157     if (!aPrs.object())
158       continue;
159     boost::shared_ptr<SketchPlugin_Feature> aSketchFeature = boost::dynamic_pointer_cast<
160         SketchPlugin_Feature>(aPrs.object());
161     if (!aSketchFeature)
162       continue;
163     double aDelta = aSketchFeature->distanceToPoint(
164         boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
165     if (aMinDelta < 0 || aMinDelta > aDelta) {
166       aMinDelta = aDelta;
167       // TODO aDeltaFeature = aPrs.result();
168     }
169   }
170   return aDeltaFeature;
171 }
172
173 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
174 {
175   return ModelAPI_Session::get()->moduleDocument();
176 }
177
178 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
179                                     const std::string& theAttribute)
180 {
181   if (!theFeature)
182     return;
183   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
184   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
185       aData->attribute(theAttribute));
186   if (aPoint)
187     aPoint->setValue(theX, theY);
188 }
189
190 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
191                                     const std::string& theAttribute)
192 {
193   if (!theFeature)
194     return;
195   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
196   AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
197       aData->attribute(theAttribute));
198   if (anAttribute)
199     anAttribute->setValue(theValue);
200 }
201
202 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
203                                    bool& isValid)
204 {
205   isValid = false;
206   double aValue;
207   if (theFeature) {
208     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
209     AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
210         aData->attribute(theAttribute));
211     if (anAttribute) {
212       aValue = anAttribute->value();
213       isValid = true;
214     }
215   }
216   return aValue;
217 }
218
219 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
220                                   const std::string& theKind)
221 {
222   FeaturePtr aFeature;
223   if (!theFeature)
224     return aFeature;
225
226   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
227   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
228       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
229   if (anAttr) {
230     aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
231     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
232       aFeature = FeaturePtr();
233     }
234   }
235   return aFeature;
236 }
237
238 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
239                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
240                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
241 {
242   FeaturePtr aFeature;
243   if (theSketch) {
244     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
245   } else {
246     boost::shared_ptr<ModelAPI_Document> aDoc = document();
247     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
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(CompositeFeaturePtr 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(CompositeFeaturePtr 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                                                       CompositeFeaturePtr 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       || theKind == SketchPlugin_ConstraintRigid::ID();
346 }