]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketch.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 <XGUI_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 void PartSet_OperationSketch::init(FeaturePtr theFeature,
62                                    const std::list<XGUI_ViewerPrs>& /*theSelected*/,
63                                    const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
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<XGUI_ViewerPrs>& theSelected,
76                                            const std::list<XGUI_ViewerPrs>& theHighlighted)
77 {
78   if (!hasSketchPlane()) {
79     if (!theHighlighted.empty()) {
80       XGUI_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       FeaturePtr aFeature = theHighlighted.front().feature();
95       if (aFeature) {
96         std::string anOperationType = getOperationType(aFeature);
97         if (theSelected.size() > 1)
98           anOperationType = PartSet_OperationFeatureEditMulti::Type();
99         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<XGUI_ViewerPrs>& theSelected,
110                                             const std::list<XGUI_ViewerPrs>& theHighlighted)
111 {
112   if (!hasSketchPlane()) {
113   }
114   else {
115     if (theSelected.size() == 1) {
116       FeaturePtr aFeature = theSelected.front().feature();
117       if (aFeature)
118         restartOperation(getOperationType(aFeature), aFeature);
119     }
120   }
121 }
122
123 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
124 {
125   if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
126     return;
127
128   if (myFeatures.size() != 1) {
129     FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
130                                                         myFeatures);
131     if (aFeature)
132                 restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
133   }
134 }
135
136 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
137                                                         PartSet_OperationSketch::subPreview() const
138 {
139   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
140
141   boost::shared_ptr<SketchPlugin_Feature> aFeature;
142
143   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
144   if (!aData->isValid())
145     return aPreviewMap;
146   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
147         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
148
149   std::list<FeaturePtr > aFeatures = aRefList->list();
150   std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
151                                                                   aLast = aFeatures.end();
152   for (; anIt != aLast; anIt++) {
153     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
154     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
155     if (aPreview)
156       aPreviewMap[aFeature] = aPreview;
157   }
158   return aPreviewMap;
159 }
160
161 void PartSet_OperationSketch::stopOperation()
162 {
163   PartSet_OperationSketchBase::stopOperation();
164   emit featureConstructed(feature(), FM_Hide);
165   emit closeLocalContext();
166 }
167
168 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
169 {
170   return hasSketchPlane();
171 }
172
173 void PartSet_OperationSketch::startOperation()
174 {
175   PartSet_OperationSketchBase::startOperation();
176   if (!isEditOperation())
177     emit fitAllView();
178 }
179
180 bool PartSet_OperationSketch::hasSketchPlane() const
181 {
182   bool aHasPlane = false;
183
184   if (feature()) {
185     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
186     AttributeDoublePtr anAttr;
187     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
188       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
189     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
190   }
191   return aHasPlane;
192 }
193
194 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
195 {
196   if (theShape.IsNull())
197     return;
198
199   // get selected shape
200   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
201   aGShape->setImpl(new TopoDS_Shape(theShape));
202
203   // get plane parameters
204   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
205
206   // set plane parameters to feature
207   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
208   double anA, aB, aC, aD;
209   aPlane->coefficients(anA, aB, aC, aD);
210
211   // calculate attributes of the sketch
212   boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
213   boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
214   boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
215   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
216   boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
217   // X axis is preferable to be dirX on the sketch
218   const double tol = Precision::Confusion();
219   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
220   boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
221   boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
222   boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
223
224   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
225     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
226   anOrigin->setValue(anOrigPnt);
227   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
228     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
229   aNormal->setValue(aNormDir);
230   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
231     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
232   aDirX->setValue(aXDir);
233   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
234     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
235   aDirY->setValue(aYDir);
236   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
237
238   flushUpdated();
239
240   emit featureConstructed(feature(), FM_Hide);
241   emit closeLocalContext();
242   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
243 }
244
245 std::string PartSet_OperationSketch::getOperationType(FeaturePtr theFeature)
246 {
247   return PartSet_OperationFeatureEdit::Type();
248 }