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