]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketchLine.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_OperationSketchLine.cpp
1 // File:        PartSet_OperationSketchLine.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationSketchLine.h>
6
7 #include <SketchPlugin_Feature.h>
8 #include <GeomDataAPI_Point2D.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <ModelAPI_Data.h>
12
13 #include <SketchPlugin_Sketch.h>
14 #include <SketchPlugin_Line.h>
15
16 #ifdef _DEBUG
17 #include <QDebug>
18 #endif
19
20 using namespace std;
21
22 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
23                                                   QObject* theParent,
24                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
25 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
26   myPointSelectionMode(SM_FirstPoint)
27 {
28 }
29
30 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
31 {
32 }
33
34 bool PartSet_OperationSketchLine::isGranted() const
35 {
36   return true;
37 }
38
39 int PartSet_OperationSketchLine::getSelectionMode() const
40 {
41   return 0;//TopAbs_FACE;
42 }
43
44 void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
45 {
46   switch (myPointSelectionMode)
47   {
48     case SM_FirstPoint: {
49       setLinePoint(thePoint, LINE_ATTR_START);
50       myPointSelectionMode = SM_SecondPoint;
51     }
52     break;
53     case SM_SecondPoint: {
54       setLinePoint(thePoint, LINE_ATTR_END);
55       myPointSelectionMode = SM_None;
56     }
57     break;
58     case SM_None: {
59
60     }
61     break;
62     default:
63       break;
64   }
65 }
66
67 void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint)
68 {
69   if (myPointSelectionMode == SM_SecondPoint)
70     setLinePoint(thePoint, LINE_ATTR_END);
71 }
72
73 void PartSet_OperationSketchLine::startOperation()
74 {
75   PartSet_OperationSketchBase::startOperation();
76
77   if (mySketch) {
78     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
79                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
80
81     aFeature->addSub(feature());
82   }
83   myPointSelectionMode = SM_FirstPoint;
84 }
85
86 void PartSet_OperationSketchLine::stopOperation()
87 {
88   PartSet_OperationSketchBase::stopOperation();
89   myPointSelectionMode = SM_None;
90 }
91
92 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
93                                                const std::string& theAttribute)
94 {
95   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
96   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
97         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
98
99   double aX = 0;
100   double anY = 0;
101   convertTo2D(thePoint, aX, anY);
102   aPoint->setValue(aX, anY);
103 }
104
105 void PartSet_OperationSketchLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
106 {
107   if (!mySketch)
108     return;
109
110   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
111   boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
112
113   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
114     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
115
116   boost::shared_ptr<GeomDataAPI_Dir> aX = 
117     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
118   boost::shared_ptr<GeomDataAPI_Dir> anY = 
119     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
120
121   gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
122   theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
123   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
124 }