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