]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Tools.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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_Document.h>
10
11 #include <GeomDataAPI_Point.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <GeomAPI_Dir.h>
16 #include <GeomAPI_XYZ.h>
17
18 #include <SketchPlugin_Feature.h>
19 #include <SketchPlugin_Sketch.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
21 #include <SketchPlugin_Constraint.h>
22
23 #include <PartSet_FeatureLinePrs.h>
24 #include <PartSet_FeaturePointPrs.h>
25 #include <PartSet_FeatureCirclePrs.h>
26 #include <PartSet_FeatureArcPrs.h>
27
28 #include <PartSet_FeatureLengthPrs.h>
29 #include <PartSet_FeatureRadiusPrs.h>
30 #include <PartSet_FeatureDistancePrs.h>
31
32 #include <XGUI_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(), PlaneOfTheView);
69   return ResultPoint;
70 }
71
72 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
73                                 Handle(V3d_View) theView, 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 = 
82     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
83
84   boost::shared_ptr<GeomDataAPI_Dir> aX = 
85     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
86   boost::shared_ptr<GeomDataAPI_Dir> anY = 
87     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
88
89   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
90   gp_Vec aVec(anOriginPnt, thePoint);
91
92   if (!theView.IsNull())
93   {
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 = 
105                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
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,
119                                 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 = 
128     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
129   boost::shared_ptr<GeomDataAPI_Dir> aX = 
130     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
131   boost::shared_ptr<GeomDataAPI_Dir> aY = 
132     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
133
134   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
135     aX->dir()->xyz()->multiplied(theX))->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 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
142                                                                       FeaturePtr theSketch,
143                                                                       FeaturePtr theFeature)
144 {
145   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
146
147   if (theKind == PartSet_FeaturePointPrs::getKind()) {
148     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
149   }
150   else if (theKind == PartSet_FeatureLinePrs::getKind()) {
151     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
152   }
153   else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
154     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
155   }
156   else if (theKind == PartSet_FeatureArcPrs::getKind()) {
157     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
158   }
159   else if (theKind == PartSet_FeatureLengthPrs::getKind()) {
160     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLengthPrs(theSketch));
161   }
162   else if (theKind == PartSet_FeatureRadiusPrs::getKind()) {
163     aFeaturePrs = boost::shared_ptr<PartSet_FeatureRadiusPrs>(new PartSet_FeatureRadiusPrs(theSketch));
164   }
165   else if (theKind == PartSet_FeatureDistancePrs::getKind()) {
166     aFeaturePrs = boost::shared_ptr<PartSet_FeatureDistancePrs>(new PartSet_FeatureDistancePrs(theSketch));
167   }
168
169
170   if (theFeature && aFeaturePrs)
171     aFeaturePrs->init(theFeature);
172
173   return aFeaturePrs;
174 }
175
176 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
177                                          FeaturePtr theSketch,
178                                          const std::list<XGUI_ViewerPrs>& theFeatures)
179 {
180   double aX, anY;
181   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
182   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
183
184   FeaturePtr aFeature;
185   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
186
187   FeaturePtr aDeltaFeature;   
188   double aMinDelta = -1;
189   XGUI_ViewerPrs aPrs;
190   for (; anIt != aLast; anIt++) {
191     aPrs = *anIt;
192     if (!aPrs.feature())
193       continue;
194     double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
195     if (aMinDelta < 0 || aMinDelta > aDelta) {
196       aMinDelta = aDelta;
197       aDeltaFeature = aPrs.feature();
198     }
199   }
200   return aDeltaFeature;
201 }
202
203 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
204                                       double theX, double theY)
205 {
206   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
207                                            theFeature->getKind(), FeaturePtr(), theFeature);
208   double aDelta = 0;
209   if (aFeaturePrs)
210     aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
211
212   return aDelta;
213 }
214
215 void PartSet_Tools::moveFeature(FeaturePtr theFeature, double theDeltaX, double theDeltaY)
216 {
217   if (!theFeature)
218     return;
219
220   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
221                                            theFeature->getKind(), FeaturePtr(), theFeature);
222   if (aFeaturePrs)
223   aFeaturePrs->move(theDeltaX, theDeltaY);
224 }
225
226 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
227 {
228   return ModelAPI_PluginManager::get()->rootDocument();
229 }
230
231 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
232                                     const std::string& theAttribute)
233 {
234   if (!theFeature)
235     return;
236   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
237   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
238         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
239   if (aPoint)
240     aPoint->setValue(theX, theY);
241 }
242
243 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
244                                     const std::string& theAttribute)
245 {
246   if (!theFeature)
247     return;
248   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
249   AttributeDoublePtr anAttribute =
250         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
251   if (anAttribute)
252     anAttribute->setValue(theValue);
253 }
254
255 bool PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& theAttribute,
256                                  double& theValue)
257 {
258   bool aResult = false;
259   if (!theFeature)
260     return aResult;
261   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
262   AttributeDoublePtr anAttribute =
263         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
264   if (anAttribute) {
265     theValue = anAttribute->value();
266     aResult = true;
267   }
268   return aResult;
269 }
270
271 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
272                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
273                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
274 {
275   boost::shared_ptr<ModelAPI_Document> aDoc = document();
276   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
277
278   if (theSketch) {
279     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
280                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
281     aSketch->addSub(aFeature);
282   }
283
284   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
285
286   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
287         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
288   aRef1->setAttr(thePoint1);
289
290   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
291         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
292   aRef2->setAttr(thePoint2);
293
294   if (aFeature) // TODO: generate an error if feature was not created
295     aFeature->execute();
296 }
297
298 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
299                                                                 double theX, double theY)
300 {
301   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
302                                            theFeature->getKind(), FeaturePtr(), theFeature);
303
304   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
305   if (aFeaturePrs)
306     aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
307
308   return aPoint2D;
309 }