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