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