]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationConstraint.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_OperationConstraint.cpp
1 // File:        PartSet_OperationConstraint.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationConstraint.h>
6
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <ModuleBase_OperationDescription.h>
16
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
21
22 #include <SketchPlugin_Constraint.h>
23
24 #include <Geom_Line.hxx>
25 #include <gp_Lin.hxx>
26
27 #include <XGUI_ViewerPrs.h>
28 #include <XGUI_Constants.h>
29
30 #include <SketchPlugin_Line.h>
31
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
34 #include <TopoDS.hxx>
35 #include <BRep_Tool.hxx>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #endif
40
41 #include <QMouseEvent>
42
43 using namespace std;
44
45 PartSet_OperationConstraint::PartSet_OperationConstraint(const QString& theId,
46                                                   QObject* theParent,
47                                               boost::shared_ptr<ModelAPI_Feature> theFeature)
48 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
49 {
50 }
51
52 PartSet_OperationConstraint::~PartSet_OperationConstraint()
53 {
54 }
55
56 bool PartSet_OperationConstraint::isGranted(ModuleBase_IOperation* theOperation) const
57 {
58   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
59 }
60
61 void PartSet_OperationConstraint::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
62                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
63                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
64 {
65   //if (!theFeature || theFeature->getKind() != "SketchLine")
66   //  return;
67   // use the last point of the previous feature as the first of the new one
68   //boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
69   //myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
70 }
71
72 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationConstraint::sketch() const
73 {
74   return mySketch;
75 }
76
77 void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
78                                                 const std::list<XGUI_ViewerPrs>& theSelected,
79                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
80 {
81   /*if (myPointSelectionMode == SM_DonePoint)
82   {
83     // if the point creation is finished, the next mouse release should commit the modification
84     // the next release can happens by double click in the viewer
85     commit();
86     restartOperation(PartSet_OperationConstraint::Type(), feature());
87     return;
88   }
89
90   double aX, anY;
91
92   bool isFoundPoint = false;
93   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
94   if (theSelected.empty()) {
95     PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
96     isFoundPoint = true;
97   }
98   else {
99     XGUI_ViewerPrs aPrs = theSelected.front();
100     const TopoDS_Shape& aShape = aPrs.shape();
101     if (!aShape.IsNull()) // the point is selected
102     {
103       if (aShape.ShapeType() == TopAbs_VERTEX) {
104         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
105         if (!aVertex.IsNull()) {
106           aPoint = BRep_Tool::Pnt(aVertex);
107           PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
108           isFoundPoint = true;
109
110           setConstraints(aX, anY);
111         }
112       }
113       else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
114       {
115         boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
116         if (aFeature) {
117           double X0, X1, X2, X3;
118           double Y0, Y1, Y2, Y3;
119           getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
120           getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
121           PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
122
123           switch (myPointSelectionMode) {
124             case SM_FirstPoint:
125               PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
126             break;
127             case SM_SecondPoint: {
128               getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
129               PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
130             }
131             break;
132             default:
133             break;
134           }
135           isFoundPoint = true;
136         }
137       }
138     }
139   }
140
141   switch (myPointSelectionMode)
142   {
143     case SM_FirstPoint: {
144       setLinePoint(feature(), aX, anY, LINE_ATTR_START);
145       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
146       flushUpdated();
147
148       //setPointSelectionMode(SM_SecondPoint);
149     }
150     break;
151     case SM_SecondPoint: {
152       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
153       flushUpdated();
154
155       //setPointSelectionMode(SM_DonePoint);
156    }
157     break;
158     default:
159       break;
160   }
161 */
162 }
163
164 void PartSet_OperationConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
165 {
166 /*  switch (myPointSelectionMode)
167   {
168     case SM_FirstPoint: {
169       double aX, anY;
170       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
171       PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
172       setLinePoint(feature(), aX, anY, LINE_ATTR_START);
173       setLinePoint(feature(), aX, anY, LINE_ATTR_END);
174       flushUpdated();
175       emit focusActivated(LINE_ATTR_START);
176     }
177     break;
178     case SM_SecondPoint:
179     {
180       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
181       setLinePoint(aPoint, theView, LINE_ATTR_END);
182       flushUpdated();
183       emit focusActivated(LINE_ATTR_END);
184     }
185     break;
186     case SM_DonePoint:
187     {
188       commit();
189       restartOperation(PartSet_OperationConstraint::Type(), feature());
190     }
191     default:
192       break;
193   }*/
194 }
195
196 void PartSet_OperationConstraint::startOperation()
197 {
198   PartSet_OperationSketchBase::startOperation();
199   //setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
200
201   emit multiSelectionEnabled(false);
202 }
203
204 void PartSet_OperationConstraint::abortOperation()
205 {
206   emit featureConstructed(feature(), FM_Hide);
207   PartSet_OperationSketchBase::abortOperation();
208 }
209
210 void PartSet_OperationConstraint::stopOperation()
211 {
212   PartSet_OperationSketchBase::stopOperation();
213   emit multiSelectionEnabled(true);
214 }
215
216 void PartSet_OperationConstraint::afterCommitOperation()
217 {
218   PartSet_OperationSketchBase::afterCommitOperation();  
219   emit featureConstructed(feature(), FM_Deactivation);
220 }
221
222 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationConstraint::createFeature(const bool theFlushMessage)
223 {
224   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
225   if (sketch()) {
226     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
227                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
228
229     aFeature->addSub(aNewFeature);
230   }
231   /*if (myInitPoint) {
232     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
233     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
234
235     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
236     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
237                                                                 (aData->attribute(LINE_ATTR_START));
238     createConstraint(myInitPoint, aPoint);
239   }*/
240
241   emit featureConstructed(aNewFeature, FM_Activation);
242   if (theFlushMessage)
243     flushCreated();
244   return aNewFeature;
245 }