]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
Select external objects for sketcher
[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 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
182                                     const std::string& theAttribute)
183 {
184   if (!theFeature)
185     return;
186   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
187   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = 
188     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
189   if (aPoint)
190     aPoint->setValue(theX, theY);
191 }
192
193 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
194                                     const std::string& theAttribute)
195 {
196   if (!theFeature)
197     return;
198   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
199   AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
200       aData->attribute(theAttribute));
201   if (anAttribute)
202     anAttribute->setValue(theValue);
203 }
204
205 double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
206                                    bool& isValid)
207 {
208   isValid = false;
209   double aValue;
210   if (theFeature) {
211     boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
212     AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
213         aData->attribute(theAttribute));
214     if (anAttribute) {
215       aValue = anAttribute->value();
216       isValid = true;
217     }
218   }
219   return aValue;
220 }
221
222 FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
223                                   const std::string& theKind)
224 {
225   FeaturePtr aFeature;
226   if (!theFeature)
227     return aFeature;
228
229   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
230   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = boost::dynamic_pointer_cast<
231       ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
232   if (anAttr) {
233     aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->object());
234     if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
235       aFeature = FeaturePtr();
236     }
237   }
238   return aFeature;
239 }
240
241 void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
242                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
243                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
244 {
245   FeaturePtr aFeature;
246   if (theSketch) {
247     aFeature = theSketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
248   } else {
249     boost::shared_ptr<ModelAPI_Document> aDoc = document();
250     aFeature = aDoc->addFeature(SketchPlugin_ConstraintCoincidence::ID());
251   }
252
253   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
254
255   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = boost::dynamic_pointer_cast<
256       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
257   aRef1->setAttr(thePoint1);
258
259   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = boost::dynamic_pointer_cast<
260       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
261   aRef2->setAttr(thePoint2);
262
263   if (aFeature)  // TODO: generate an error if feature was not created
264     aFeature->execute();
265 }
266
267 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
268                                    const std::string& theAttribute, double theClickedX,
269                                    double theClickedY)
270 {
271   // find a feature point by the selection mode
272   //boost::shared_ptr<GeomDataAPI_Point2D> aPoint = featurePoint(theMode);
273   boost::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint = boost::dynamic_pointer_cast<
274       GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
275   if (!aFeaturePoint)
276     return;
277
278   // get all sketch features. If the point with the given coordinates belong to any sketch feature,
279   // the constraint is created between the feature point and the found sketch point
280   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
281   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
282       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
283
284   std::list<ObjectPtr> aFeatures = aRefList->list();
285   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
286   std::list<boost::shared_ptr<ModelAPI_Attribute> > anAttiributes;
287   boost::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = boost::shared_ptr<GeomAPI_Pnt2d>(
288       new GeomAPI_Pnt2d(theClickedX, theClickedY));
289   for (; anIt != aLast; anIt++) {
290     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
291     // find the given point in the feature attributes
292     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
293     std::list<boost::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
294         aLast = anAttiributes.end();
295     boost::shared_ptr<GeomDataAPI_Point2D> aFPoint;
296     for (; anIt != aLast && !aFPoint; anIt++) {
297       boost::shared_ptr<GeomDataAPI_Point2D> aCurPoint = boost::dynamic_pointer_cast<
298           GeomDataAPI_Point2D>(*anIt);
299       if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
300         aFPoint = aCurPoint;
301     }
302     if (aFPoint)
303       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
304   }
305 }
306
307 boost::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
308 {
309   boost::shared_ptr<GeomAPI_Pln> aPlane;
310   double aA, aB, aC, aD;
311
312   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
313   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
314       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
315   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
316       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
317   aA = aNormal->x();
318   aB = aNormal->y();
319   aC = aNormal->z();
320   aD = 0;
321
322   aPlane = boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
323   return aPlane;
324 }
325
326 boost::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(boost::shared_ptr<GeomAPI_Pnt2d> thePoint2D,
327                                                       CompositeFeaturePtr theSketch)
328 {
329   boost::shared_ptr<GeomAPI_Pnt> aPoint;
330   if (!theSketch || !thePoint2D)
331     return aPoint;
332
333   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
334       theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
335   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
336       theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
337   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
338       theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
339
340   return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
341 }
342
343 bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
344 {
345   return theKind == SketchPlugin_ConstraintDistance::ID()
346       || theKind == SketchPlugin_ConstraintLength::ID()
347       || theKind == SketchPlugin_ConstraintRadius::ID()
348       || theKind == SketchPlugin_ConstraintRigid::ID();
349 }
350
351 ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
352 {
353   TopoDS_Shape aShape = thePrs.shape();
354   if (aShape.ShapeType() != TopAbs_EDGE)
355     return ResultPtr();
356
357   Standard_Real aStart, aEnd;
358   Handle(V3d_View) aNullView;
359   FeaturePtr myFeature;
360
361   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
362   GeomAdaptor_Curve aAdaptor(aCurve);
363   if (aAdaptor.GetType() == GeomAbs_Line) {
364     // Create line
365     myFeature = theSketch->addFeature(SketchPlugin_Line::ID());
366
367     //DataPtr aData = myFeature->data();
368     //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
369     //  boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
370
371     //double aX, aY;
372     //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
373     //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
374     //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
375
376     //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
377     //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
378     //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
379   } else if (aAdaptor.GetType() == GeomAbs_Circle) {
380     if (aAdaptor.IsClosed()) {
381       // Create circle
382       myFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
383       //gp_Circ aCirc = aAdaptor.Circle();
384       //gp_Pnt aCenter = aCirc.Location();
385
386       //double aX, aY;
387       //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
388       //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
389       //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
390     } else {
391       // Create arc
392       myFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
393     }
394   }
395   if (myFeature) {
396     DataPtr aData = myFeature->data();
397     AttributeSelectionPtr anAttr = 
398       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
399       (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
400
401     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
402     if (anAttr && aRes) {
403       boost::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
404       anEdge->setImpl(new TopoDS_Shape(aShape));
405
406       anAttr->setValue(aRes, anEdge);
407       myFeature->execute();
408       return myFeature->firstResult();
409     }
410   }
411   return ResultPtr();
412 }