Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 boost::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     //if (aHasShift && (aSelected.size() > 0)) {
66       foreach(ModuleBase_ViewerPrs aPrs, aHighlighted)
67         aSelected.append(aPrs);
68     //}
69     //if (aHasShift && aSelected.size() > 0)
70     //  return;
71
72     // there should be a start of operation, which uses the pre-highlighted objects,
73     // the selected ones are collected here and are processed by a mouse move
74     if (aHighlighted.size() == 1) {
75     //if (aSelected.size() > 0) {
76       ObjectPtr aFeature = aSelected.first().object();
77       if (aFeature) {
78         std::string anOperationType = PartSet_OperationFeatureEdit::Type();
79         restartOperation(anOperationType, aFeature);
80       }
81     }
82     else
83       myFeatures = aHighlighted;
84     //else
85     //myFeatures = aSelected;
86
87   } 
88 }
89
90
91 void PartSet_OperationSketch::selectionChanged(ModuleBase_ISelection* theSelection)
92 {
93   if (hasSketchPlane()) 
94     return;
95
96   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
97   if (!aSelected.empty()) {
98     ModuleBase_ViewerPrs aPrs = aSelected.first();
99     // We have to select a plane before any operation
100     TopoDS_Shape aShape = aPrs.shape();
101     if (!aShape.IsNull()) {
102       boost::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
103       flushUpdated();
104       emit featureConstructed(feature(), FM_Hide);
105       // If selection is not a sketcher presentation then it has to be stored as 
106       // External shape
107       if (feature() != aPrs.object()) {
108         //boost::shared_ptr<SketchPlugin_Sketch> aSketch = 
109         //  boost::dynamic_pointer_cast<SketchPlugin_Sketch>(feature());
110         DataPtr aData = feature()->data();
111         AttributeSelectionPtr aSelAttr = 
112           boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
113           (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
114         if (aSelAttr) {
115           ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
116           if (aRes) {
117             GeomShapePtr aShapePtr(new GeomAPI_Shape());
118             aShapePtr->setImpl(new TopoDS_Shape(aShape));
119             aSelAttr->setValue(aRes, aShapePtr);
120           }
121         }
122       } else {
123         // Turn viewer to the plane
124         emit planeSelected(aDir->x(), aDir->y(), aDir->z());
125       }
126       emit launchSketch();
127     }
128   }
129 }
130
131
132 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
133                                             ModuleBase_ISelection* theSelection)
134 {
135   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
136   if (hasSketchPlane()) {
137     /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
138     /// but for other IO, for example constraint dimensions.
139     /// It is empty and we have to use the process mouse release to start edition operation
140     /// for these objects
141     if (aSelected.size() == 1) {
142       ObjectPtr aObject = aSelected.first().object();
143       if (aObject) {
144         restartOperation(PartSet_OperationFeatureEdit::Type(), aObject);
145       }
146     }
147   }
148 }
149
150 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
151 {
152   if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
153     return;
154
155   if (myFeatures.size() != 1) {
156     Handle(V3d_View) aView = theViewer->activeView();
157     FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
158                                                         myFeatures);
159     if (aFeature)
160       restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
161   }
162 }
163
164 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
165 {
166   std::list<FeaturePtr> aFeaList;
167   FeaturePtr aFeature = feature();
168   if (!aFeature)
169     return aFeaList;
170
171   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
172   if (!aData->isValid())
173     return std::list<FeaturePtr>();
174   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
175       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
176
177   std::list<ObjectPtr> aList = aRefList->list();
178   std::list<ObjectPtr>::iterator aIt;
179   for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
180     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
181     if (aFeature)
182       aFeaList.push_back(aFeature);
183   }
184   return aFeaList;
185 }
186
187 void PartSet_OperationSketch::stopOperation()
188 {
189   PartSet_OperationSketchBase::stopOperation();
190   emit featureConstructed(feature(), FM_Hide);
191 }
192
193 void PartSet_OperationSketch::afterCommitOperation()
194 {
195   FeaturePtr aFeature = feature();
196   std::list<ResultPtr> aResults = aFeature->results();
197   std::list<ResultPtr>::const_iterator aIt;
198   Events_ID anEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW);
199   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
200     ModelAPI_EventCreator::get()->sendUpdated(*aIt, anEvent);
201   }
202   Events_Loop::loop()->flush(anEvent);
203 }
204
205 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
206 {
207   return hasSketchPlane();
208 }
209
210 void PartSet_OperationSketch::startOperation()
211 {
212   PartSet_OperationSketchBase::startOperation();
213   if (!isEditOperation())
214     emit fitAllView();
215 }
216
217 bool PartSet_OperationSketch::hasSketchPlane() const
218 {
219   bool aHasPlane = false;
220
221   if (feature()) {
222     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
223     AttributeDoublePtr anAttr;
224     boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
225         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
226     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
227   }
228   return aHasPlane;
229 }
230
231 boost::shared_ptr<GeomAPI_Dir> PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
232 {
233   if (theShape.IsNull())
234     return boost::shared_ptr<GeomAPI_Dir>();
235
236   // get selected shape
237   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
238   aGShape->setImpl(new TopoDS_Shape(theShape));
239
240   // get plane parameters
241   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
242
243   // set plane parameters to feature
244   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
245   double anA, aB, aC, aD;
246   aPlane->coefficients(anA, aB, aC, aD);
247
248   // calculate attributes of the sketch
249   boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
250   boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
251   boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
252   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
253   boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
254   // X axis is preferable to be dirX on the sketch
255   const double tol = Precision::Confusion();
256   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
257   boost::shared_ptr<GeomAPI_Dir> aTempDir(
258       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
259   boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
260   boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
261
262   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
263       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
264   anOrigin->setValue(anOrigPnt);
265   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
266       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
267   aNormal->setValue(aNormDir);
268   boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
269       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
270   aDirX->setValue(aXDir);
271   boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
272       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
273   aDirY->setValue(aYDir);
274   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
275   return aDir;
276 }
277
278
279 bool PartSet_OperationSketch::isGranted(ModuleBase_Operation* theOperation) const
280 {
281   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
282   return aPreviewOp != NULL;
283 }
284