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