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