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