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