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