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
30 #include <XGUI_ViewerPrs.h>
31
32 #include <V3d_View.hxx>
33 #include <gp_Pln.hxx>
34 #include <ProjLib.hxx>
35 #include <ElSLib.hxx>
36 #include <Geom_Line.hxx>
37 #include <GeomAPI_ProjectPointOnCurve.hxx>
38
39 #ifdef _DEBUG
40 #include <QDebug>
41 #endif
42
43 const double PRECISION_TOLERANCE = 0.000001;
44
45 gp_Pnt PartSet_Tools::convertClickToPoint(QPoint thePoint, Handle(V3d_View) theView)
46 {
47   if (theView.IsNull())
48     return gp_Pnt();
49
50   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
51   theView->Eye(XEye, YEye, ZEye);
52
53   theView->At(XAt, YAt, ZAt);
54   gp_Pnt EyePoint(XEye, YEye, ZEye);
55   gp_Pnt AtPoint(XAt, YAt, ZAt);
56
57   gp_Vec EyeVector(EyePoint, AtPoint);
58   gp_Dir EyeDir(EyeVector);
59
60   gp_Pln PlaneOfTheView = gp_Pln(AtPoint, EyeDir);
61   Standard_Real X, Y, Z;
62   theView->Convert(thePoint.x(), thePoint.y(), X, Y, Z);
63   gp_Pnt ConvertedPoint(X, Y, Z);
64
65   gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView, ConvertedPoint);
66   gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView);
67   return ResultPoint;
68 }
69
70 void PartSet_Tools::convertTo2D(const gp_Pnt& thePoint, FeaturePtr theSketch,
71                                 Handle(V3d_View) theView, double& theX, double& theY)
72 {
73   if (!theSketch)
74     return;
75
76   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
77   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
78
79   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
80     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
81
82   boost::shared_ptr<GeomDataAPI_Dir> aX = 
83     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
84   boost::shared_ptr<GeomDataAPI_Dir> anY = 
85     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
86
87   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
88   gp_Vec aVec(anOriginPnt, thePoint);
89
90   if (!theView.IsNull())
91   {
92     V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
93     theView->Eye(XEye, YEye, ZEye);
94
95     theView->At(XAt, YAt, ZAt);
96     gp_Pnt EyePoint(XEye, YEye, ZEye);
97     gp_Pnt AtPoint(XAt, YAt, ZAt);
98
99     gp_Vec anEyeVec(EyePoint, AtPoint);
100     anEyeVec.Normalize();
101
102     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
103                   boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
104     gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z());
105
106     double aDen = anEyeVec * aNormalVec;
107     double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX;
108
109     gp_Vec aDeltaVec = anEyeVec*aLVec;
110     aVec = aVec - aDeltaVec;
111   }
112   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
113   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
114 }
115
116 void PartSet_Tools::convertTo3D(const double theX, const double theY,
117                                 FeaturePtr theSketch,
118                                 gp_Pnt& thePoint)
119 {
120   if (!theSketch)
121     return;
122
123   boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
124
125   boost::shared_ptr<GeomDataAPI_Point> aC = 
126     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
127   boost::shared_ptr<GeomDataAPI_Dir> aX = 
128     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
129   boost::shared_ptr<GeomDataAPI_Dir> aY = 
130     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
131
132   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
133     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
134
135   boost::shared_ptr<GeomAPI_Pnt> aPoint = boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
136   thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
137 }
138
139 boost::shared_ptr<PartSet_FeaturePrs> PartSet_Tools::createFeaturePrs(const std::string& theKind,
140                                                                       FeaturePtr theSketch,
141                                                                       FeaturePtr theFeature)
142 {
143   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs;
144
145   if (theKind == PartSet_FeaturePointPrs::getKind()) {
146     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeaturePointPrs(theSketch));
147   }
148   else if (theKind == PartSet_FeatureLinePrs::getKind()) {
149     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLinePrs(theSketch));
150   }
151   else if (theKind == PartSet_FeatureCirclePrs::getKind()) {
152     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureCirclePrs(theSketch));
153   }
154   else if (theKind == PartSet_FeatureArcPrs::getKind()) {
155     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureArcPrs(theSketch));
156   }
157   else if (theKind == PartSet_FeatureLengthPrs::getKind()) {
158     aFeaturePrs = boost::shared_ptr<PartSet_FeaturePrs>(new PartSet_FeatureLengthPrs(theSketch));
159   }
160
161   if (theFeature && aFeaturePrs)
162     aFeaturePrs->init(theFeature, FeaturePtr());
163
164   return aFeaturePrs;
165 }
166
167 FeaturePtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
168                                          FeaturePtr theSketch,
169                                          const std::list<XGUI_ViewerPrs>& theFeatures)
170 {
171   double aX, anY;
172   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(thePoint, theView);
173   PartSet_Tools::convertTo2D(aPoint, theSketch, theView, aX, anY);
174
175   FeaturePtr aFeature;
176   std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
177
178   FeaturePtr aDeltaFeature;   
179   double aMinDelta = -1;
180   XGUI_ViewerPrs aPrs;
181   for (; anIt != aLast; anIt++) {
182     aPrs = *anIt;
183     if (!aPrs.feature())
184       continue;
185     double aDelta = distanceToPoint(aPrs.feature(), aX, anY);
186     if (aMinDelta < 0 || aMinDelta > aDelta) {
187       aMinDelta = aDelta;
188       aDeltaFeature = aPrs.feature();
189     }
190   }
191   return aDeltaFeature;
192 }
193
194 double PartSet_Tools::distanceToPoint(FeaturePtr theFeature,
195                                       double theX, double theY)
196 {
197   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
198                                            theFeature->getKind(), FeaturePtr(), theFeature);
199   double aDelta = 0;
200   if (aFeaturePrs)
201     aDelta = aFeaturePrs->distanceToPoint(theFeature, theX, theY);
202
203   return aDelta;
204 }
205
206 void PartSet_Tools::moveFeature(FeaturePtr theFeature, double theDeltaX, double theDeltaY)
207 {
208   if (!theFeature)
209     return;
210
211   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
212                                            theFeature->getKind(), FeaturePtr(), theFeature);
213   if (aFeaturePrs)
214   aFeaturePrs->move(theDeltaX, theDeltaY);
215 }
216
217 boost::shared_ptr<ModelAPI_Document> PartSet_Tools::document()
218 {
219   return ModelAPI_PluginManager::get()->rootDocument();
220 }
221
222 void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY,
223                                     const std::string& theAttribute)
224 {
225   if (!theFeature)
226     return;
227   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
228   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
229         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
230   if (aPoint)
231     aPoint->setValue(theX, theY);
232 }
233
234 void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue,
235                                     const std::string& theAttribute)
236 {
237   if (!theFeature)
238     return;
239   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
240   boost::shared_ptr<ModelAPI_AttributeDouble> anAttribute =
241         boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(theAttribute));
242   if (anAttribute)
243     anAttribute->setValue(theValue);
244 }
245
246 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
247                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
248                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
249 {
250   boost::shared_ptr<ModelAPI_Document> aDoc = document();
251   FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND);
252
253   if (theSketch) {
254     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
255                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(theSketch);
256     aSketch->addSub(aFeature);
257   }
258
259   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
260
261   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
262         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
263   aRef1->setAttr(thePoint1);
264
265   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
266         boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
267   aRef2->setAttr(thePoint2);
268
269   if (aFeature) // TODO: generate an error if feature was not created
270     aFeature->execute();
271 }
272
273 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::findPoint(FeaturePtr theFeature,
274                                                                 double theX, double theY)
275 {
276   boost::shared_ptr<PartSet_FeaturePrs> aFeaturePrs = PartSet_Tools::createFeaturePrs(
277                                            theFeature->getKind(), FeaturePtr(), theFeature);
278
279   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
280   if (aFeaturePrs)
281     aPoint2D = aFeaturePrs->findPoint(theFeature, theX, theY);
282
283   return aPoint2D;
284 }