Salome HOME
Boost has been removed from code
[modules/shaper.git] / src / PartSet / PartSet_OperationSketch.cpp
1 // File:        PartSet_OperationSketch.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationSketch.h>
6
7 #include <PartSet_OperationFeatureEdit.h>
8 #include <PartSet_Tools.h>
9
10 #include <SketchPlugin_Sketch.h>
11 #include <SketchPlugin_ConstraintLength.h>
12
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Events.h>
17
18 #include <GeomAlgoAPI_FaceBuilder.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomAPI_XYZ.h>
22
23 #include <ModuleBase_ViewerPrs.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_IViewer.h>
26 #include <Events_Loop.h>
27
28 #include <AIS_Shape.hxx>
29 #include <AIS_ListOfInteractive.hxx>
30 #include <AIS_InteractiveObject.hxx>
31 #include <AIS_DimensionOwner.hxx>
32 #include <AIS_LengthDimension.hxx>
33 #include <V3d_View.hxx>
34
35 #ifdef _DEBUG
36 #include <QDebug>
37 #endif
38
39 #include <QMouseEvent>
40
41 using namespace std;
42
43 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId, QObject* theParent)
44     : PartSet_OperationSketchBase(theId, theParent)
45 {
46 }
47
48 PartSet_OperationSketch::~PartSet_OperationSketch()
49 {
50 }
51
52 CompositeFeaturePtr PartSet_OperationSketch::sketch() const
53 {
54   return std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
55 }
56
57 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer, ModuleBase_ISelection* theSelection)
58 {
59   if (hasSketchPlane()) {
60     // if shift button is pressed and there are some already selected objects, the operation should
61     // not be started. We just want to combine some selected objects.
62     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
63     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
64     QList<ModuleBase_ViewerPrs> aHighlighted = theSelection->getHighlighted();
65     // commented: the next code is commented because the nearestFeature check the highlighting
66     // and selection inside
67     //if (aHasShift && (aSelected.size() > 0)) {
68     //  foreach(ModuleBase_ViewerPrs aPrs, aHighlighted)
69     //    aSelected.append(aPrs);
70     //}
71     //if (aHasShift && aSelected.size() > 0)
72     //  return;
73
74     // there should be a start of operation, which uses the pre-highlighted objects,
75     // the selected ones are collected here and are processed by a mouse move
76     //if (aHighlighted.size() == 1) {
77     //if (aSelected.size() > 0) {
78     //  ObjectPtr aFeature = aSelected.first().object();
79     //  if (aFeature) {
80     // commented: end
81         Handle(V3d_View) aView = theViewer->activeView();
82         ObjectPtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
83                                                            aSelected, aHighlighted);
84         if (aFeature)
85           restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
86       //}
87     //}
88     //else
89     //  myFeatures = aHighlighted;
90     //else
91     //myFeatures = aSelected;
92   } 
93 }
94
95
96 void PartSet_OperationSketch::selectionChanged(ModuleBase_ISelection* theSelection)
97 {
98   if (hasSketchPlane()) 
99     return;
100
101   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
102   if (!aSelected.empty()) {
103     ModuleBase_ViewerPrs aPrs = aSelected.first();
104     // We have to select a plane before any operation
105     TopoDS_Shape aShape = aPrs.shape();
106     if (!aShape.IsNull()) {
107       std::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
108       flushUpdated();
109       emit featureConstructed(feature(), FM_Hide);
110       // If selection is not a sketcher presentation then it has to be stored as 
111       // External shape
112       if (feature() != aPrs.object()) {
113         //std::shared_ptr<SketchPlugin_Sketch> aSketch = 
114         //  std::dynamic_pointer_cast<SketchPlugin_Sketch>(feature());
115         DataPtr aData = feature()->data();
116         AttributeSelectionPtr aSelAttr = 
117           std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
118           (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
119         if (aSelAttr) {
120           ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
121           if (aRes) {
122             GeomShapePtr aShapePtr(new GeomAPI_Shape());
123             aShapePtr->setImpl(new TopoDS_Shape(aShape));
124             aSelAttr->setValue(aRes, aShapePtr);
125           }
126         }
127       } else {
128         // Turn viewer to the plane
129         emit planeSelected(aDir->x(), aDir->y(), aDir->z());
130       }
131       emit updatePropPanel();
132       emit launchSketch();
133     }
134   }
135 }
136
137
138 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
139                                             ModuleBase_ISelection* theSelection)
140 {
141   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
142   if (hasSketchPlane()) {
143     /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
144     /// but for other IO, for example constraint dimensions.
145     /// It is empty and we have to use the process mouse release to start edition operation
146     /// for these objects
147     if (aSelected.size() == 1) {
148       ObjectPtr aObject = aSelected.first().object();
149       if (aObject) {
150         restartOperation(PartSet_OperationFeatureEdit::Type(), aObject);
151       }
152     }
153   }
154 }
155
156 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
157 {
158   if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
159     return;
160
161   // myFeatures are not filled in the previous realization, so, this code is just commented
162   // because has no effect
163   /*if (myFeatures.size() != 1) {
164     Handle(V3d_View) aView = theViewer->activeView();
165     ObjectPtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
166                                                        myFeatures);
167     if (aFeature)
168       restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
169   }*/
170 }
171
172 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
173 {
174   std::list<FeaturePtr> aFeaList;
175   FeaturePtr aFeature = feature();
176   if (!aFeature)
177     return aFeaList;
178
179   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
180   if (!aData->isValid())
181     return std::list<FeaturePtr>();
182   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
183       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
184
185   std::list<ObjectPtr> aList = aRefList->list();
186   std::list<ObjectPtr>::iterator aIt;
187   for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
188     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
189     if (aFeature)
190       aFeaList.push_back(aFeature);
191   }
192   return aFeaList;
193 }
194
195 void PartSet_OperationSketch::stopOperation()
196 {
197   PartSet_OperationSketchBase::stopOperation();
198   emit featureConstructed(feature(), FM_Hide);
199 }
200
201 void PartSet_OperationSketch::afterCommitOperation()
202 {
203   FeaturePtr aFeature = feature();
204   std::list<ResultPtr> aResults = aFeature->results();
205   std::list<ResultPtr>::const_iterator aIt;
206   Events_ID anEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW);
207   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
208     ModelAPI_EventCreator::get()->sendUpdated(*aIt, anEvent);
209   }
210   Events_Loop::loop()->flush(anEvent);
211 }
212
213 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
214 {
215   return hasSketchPlane();
216 }
217
218 void PartSet_OperationSketch::startOperation()
219 {
220   PartSet_OperationSketchBase::startOperation();
221   if (!isEditOperation())
222     emit fitAllView();
223 }
224
225 bool PartSet_OperationSketch::hasSketchPlane() const
226 {
227   bool aHasPlane = false;
228
229   if (feature()) {
230     std::shared_ptr<ModelAPI_Data> aData = feature()->data();
231     AttributeDoublePtr anAttr;
232     std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
233         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
234     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
235   }
236   return aHasPlane;
237 }
238
239 std::shared_ptr<GeomAPI_Dir> PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
240 {
241   if (theShape.IsNull())
242     return std::shared_ptr<GeomAPI_Dir>();
243
244   // get selected shape
245   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
246   aGShape->setImpl(new TopoDS_Shape(theShape));
247
248   // get plane parameters
249   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
250
251   // set plane parameters to feature
252   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
253   double anA, aB, aC, aD;
254   aPlane->coefficients(anA, aB, aC, aD);
255
256   // calculate attributes of the sketch
257   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
258   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
259   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
260   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
261   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
262   // X axis is preferable to be dirX on the sketch
263   const double tol = Precision::Confusion();
264   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
265   std::shared_ptr<GeomAPI_Dir> aTempDir(
266       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
267   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
268   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
269
270   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
271       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
272   anOrigin->setValue(anOrigPnt);
273   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
274       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
275   aNormal->setValue(aNormDir);
276   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
277       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
278   aDirX->setValue(aXDir);
279   std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
280       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
281   aDirY->setValue(aYDir);
282   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
283   return aDir;
284 }
285
286
287 bool PartSet_OperationSketch::isGranted(ModuleBase_Operation* theOperation) const
288 {
289   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
290   return aPreviewOp != NULL;
291 }
292