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