Salome HOME
#refs 77 - reported by Hervé Legrand: double click in 3D viewer leads to crash when...
[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_OperationEditLine.h>
8 #include <PartSet_Tools.h>
9
10 #include <SketchPlugin_Sketch.h>
11
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefList.h>
15
16 #include <GeomAlgoAPI_FaceBuilder.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Dir.h>
19
20 #include <XGUI_ViewerPrs.h>
21
22 #include <AIS_Shape.hxx>
23 #include <AIS_ListOfInteractive.hxx>
24 #include <V3d_View.hxx>
25
26 #ifdef _DEBUG
27 #include <QDebug>
28 #endif
29
30 #include <QMouseEvent>
31
32 using namespace std;
33
34 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
35                                                      QObject* theParent)
36 : PartSet_OperationSketchBase(theId, theParent)
37 {
38 }
39
40 PartSet_OperationSketch::~PartSet_OperationSketch()
41 {
42 }
43
44 std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
45 {
46   std::list<int> aModes;
47   if (!hasSketchPlane())
48     aModes.push_back(TopAbs_FACE);
49   else
50     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
51   return aModes;
52 }
53
54 void PartSet_OperationSketch::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
55                                    const std::list<XGUI_ViewerPrs>& /*theSelected*/,
56                                    const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
57 {
58   setFeature(theFeature);
59 }
60
61 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketch::sketch() const
62 {
63   return feature();
64 }
65
66 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
67                                            const std::list<XGUI_ViewerPrs>& theSelected,
68                                            const std::list<XGUI_ViewerPrs>& theHighlighted)
69 {
70   if (!hasSketchPlane()) {
71     if (!theHighlighted.empty()) {
72       XGUI_ViewerPrs aPrs = theHighlighted.front();
73       const TopoDS_Shape& aShape = aPrs.shape();
74       if (!aShape.IsNull())
75         setSketchPlane(aShape);
76     }
77   }
78   else {
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       boost::shared_ptr<ModelAPI_Feature> aFeature = theHighlighted.front().feature();
87       if (aFeature)
88         restartOperation(PartSet_OperationEditLine::Type(), aFeature);
89     }
90     else
91       myFeatures = theHighlighted;
92   }
93 }
94
95 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
96 {
97   if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
98     return;
99
100   if (myFeatures.size() != 1) {
101     boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
102                                                                 theView, feature(), myFeatures);
103     if (aFeature)
104       restartOperation(PartSet_OperationEditLine::Type(), aFeature);
105   }
106 }
107
108 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
109                                                         PartSet_OperationSketch::subPreview() const
110 {
111   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
112
113   boost::shared_ptr<SketchPlugin_Feature> aFeature;
114
115   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
116   if (!aData->isValid())
117     return aPreviewMap;
118   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
119         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
120
121   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
122   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
123                                                                   aLast = aFeatures.end();
124   for (; anIt != aLast; anIt++) {
125     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
126     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
127     if (aPreview)
128       aPreviewMap[aFeature] = aPreview;
129   }
130   return aPreviewMap;
131 }
132
133 void PartSet_OperationSketch::stopOperation()
134 {
135   PartSet_OperationSketchBase::stopOperation();
136   emit featureConstructed(feature(), FM_Hide);
137   emit closeLocalContext();
138 }
139
140 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
141 {
142   return hasSketchPlane();
143 }
144
145 void PartSet_OperationSketch::startOperation()
146 {
147   if (!feature())
148     setFeature(createFeature());
149 }
150
151 bool PartSet_OperationSketch::hasSketchPlane() const
152 {
153   bool aHasPlane = false;
154
155   if (feature()) {
156     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
157     boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
158     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
159       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
160     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
161   }
162   return aHasPlane;
163 }
164
165 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
166 {
167   if (theShape.IsNull())
168     return;
169
170   // get selected shape
171   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
172   aGShape->setImpl(new TopoDS_Shape(theShape));
173
174   // get plane parameters
175   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
176
177   // set plane parameters to feature
178   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
179   double anA, aB, aC, aD;
180   aPlane->coefficients(anA, aB, aC, aD);
181
182   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
183   // temporary solution for main planes only
184   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
185     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
186   anOrigin->setValue(0, 0, 0);
187   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
188     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
189   aNormal->setValue(anA, aB, aC);
190   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
191     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
192   aDirX->setValue(aB, aC, anA);
193   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
194     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
195   aDirY->setValue(aC, anA, aB);
196   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
197
198   flushUpdated();
199
200   emit featureConstructed(feature(), FM_Hide);
201   emit closeLocalContext();
202   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
203 }