]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationSketch.cpp
Salome HOME
c028eab6e7a4e7233cd08ceeb7153ce5a44e8db4
[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_OperationEditFeature.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 #include <GeomAPI_XYZ.h>
20
21 #include <XGUI_ViewerPrs.h>
22
23 #include <AIS_Shape.hxx>
24 #include <AIS_ListOfInteractive.hxx>
25 #include <AIS_InteractiveObject.hxx>
26 #include <AIS_DimensionOwner.hxx>
27 #include <AIS_LengthDimension.hxx>
28 #include <V3d_View.hxx>
29
30 #ifdef _DEBUG
31 #include <QDebug>
32 #endif
33
34 #include <QMouseEvent>
35
36 using namespace std;
37
38 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
39                                                      QObject* theParent)
40 : PartSet_OperationSketchBase(theId, theParent)
41 {
42 }
43
44 PartSet_OperationSketch::~PartSet_OperationSketch()
45 {
46 }
47
48 std::list<int> PartSet_OperationSketch::getSelectionModes(FeaturePtr theFeature) const
49 {
50   std::list<int> aModes;
51   if (!hasSketchPlane())
52     aModes.push_back(TopAbs_FACE);
53   else
54     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
55
56   return aModes;
57 }
58
59 void PartSet_OperationSketch::init(FeaturePtr theFeature,
60                                    const std::list<XGUI_ViewerPrs>& /*theSelected*/,
61                                    const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
62 {
63   setFeature(theFeature);
64 }
65
66 FeaturePtr PartSet_OperationSketch::sketch() const
67 {
68   return feature();
69 }
70
71 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
72                                            const std::list<XGUI_ViewerPrs>& theSelected,
73                                            const std::list<XGUI_ViewerPrs>& theHighlighted)
74 {
75   if (!hasSketchPlane()) {
76     if (!theHighlighted.empty()) {
77       XGUI_ViewerPrs aPrs = theHighlighted.front();
78       const TopoDS_Shape& aShape = aPrs.shape();
79       if (!aShape.IsNull())
80         setSketchPlane(aShape);
81     }
82   }
83   else {
84     // if shift button is pressed and there are some already selected objects, the operation should
85     // not be started. We just want to combine some selected objects.
86     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
87     if (aHasShift && theSelected.size() > 0)
88       return;
89
90     if (theHighlighted.size() == 1) {
91       FeaturePtr aFeature = theHighlighted.front().feature();
92       if (aFeature)
93         restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
94     }
95     else
96       myFeatures = theHighlighted;
97   }
98 }
99
100 #include <QLineEdit>
101 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
102                                             const std::list<XGUI_ViewerPrs>& theSelected,
103                                             const std::list<XGUI_ViewerPrs>& theHighlighted)
104 {
105   if (!hasSketchPlane()) {
106   }
107   else {
108     if (!theSelected.empty()) {
109       XGUI_ViewerPrs aPrs = theSelected.front();
110       if (!aPrs.owner().IsNull()) {
111         Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(aPrs.owner());
112         if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
113           Handle(SelectMgr_SelectableObject) anObject = anOwner->Selectable();
114           double aValue = 0;
115           if (!anObject.IsNull()) {
116             Handle(AIS_LengthDimension) aLenDim = Handle(AIS_LengthDimension)::DownCast(anObject);
117             if (!aLenDim.IsNull())
118               aValue = aLenDim->GetValue();
119           }
120
121           QLineEdit* aLine = new QLineEdit();
122           QPoint aViewPos = theEvent->globalPos();
123           QPoint aLinePos(aViewPos.x(), aViewPos.y());
124           aLine->move(aLinePos);
125           aLine->setText(QString::number(aValue));
126           aLine->show();
127         }
128       }
129     }
130   }
131 }
132
133 #include <QLineEdit>
134 void PartSet_OperationSketch::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
135                                                const std::list<XGUI_ViewerPrs>& theSelected,
136                                                const std::list<XGUI_ViewerPrs>& theHighlighted)
137 {
138   return;
139   if (!hasSketchPlane())
140     return;
141   if (!theSelected.empty()) {
142     XGUI_ViewerPrs aPrs = theSelected.front();
143     if (!aPrs.owner().IsNull()) {
144       Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(aPrs.owner());
145       if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
146         Handle(SelectMgr_SelectableObject) anObject = anOwner->Selectable();
147         double aValue = 0;
148         if (!anObject.IsNull()) {
149           Handle(AIS_LengthDimension) aLenDim = Handle(AIS_LengthDimension)::DownCast(anObject);
150           if (!aLenDim.IsNull())
151             aValue = aLenDim->GetValue();
152         }
153
154         QLineEdit* aLine = new QLineEdit();
155         QPoint aViewPos = theEvent->globalPos();
156         QPoint aLinePos(aViewPos.x(), aViewPos.y());
157         aLine->move(aLinePos);
158         aLine->setText(QString::number(aValue));
159         aLine->show();
160       }
161     }
162   }
163 }
164
165 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
166 {
167   if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
168     return;
169
170   if (myFeatures.size() != 1) {
171     FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
172                                                         myFeatures);
173     if (aFeature)
174       restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
175   }
176 }
177
178 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
179                                                         PartSet_OperationSketch::subPreview() const
180 {
181   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
182
183   boost::shared_ptr<SketchPlugin_Feature> aFeature;
184
185   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
186   if (!aData->isValid())
187     return aPreviewMap;
188   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
189         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
190
191   std::list<FeaturePtr > aFeatures = aRefList->list();
192   std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
193                                                                   aLast = aFeatures.end();
194   for (; anIt != aLast; anIt++) {
195     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
196     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
197     if (aPreview)
198       aPreviewMap[aFeature] = aPreview;
199   }
200   return aPreviewMap;
201 }
202
203 void PartSet_OperationSketch::stopOperation()
204 {
205   PartSet_OperationSketchBase::stopOperation();
206   emit featureConstructed(feature(), FM_Hide);
207   emit closeLocalContext();
208 }
209
210 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
211 {
212   return hasSketchPlane();
213 }
214
215 void PartSet_OperationSketch::startOperation()
216 {
217   if (!feature()) {
218     setFeature(createFeature());
219     emit fitAllView();
220   }
221 }
222
223 bool PartSet_OperationSketch::hasSketchPlane() const
224 {
225   bool aHasPlane = false;
226
227   if (feature()) {
228     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
229     AttributeDoublePtr anAttr;
230     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
231       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
232     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
233   }
234   return aHasPlane;
235 }
236
237 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
238 {
239   if (theShape.IsNull())
240     return;
241
242   // get selected shape
243   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
244   aGShape->setImpl(new TopoDS_Shape(theShape));
245
246   // get plane parameters
247   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
248
249   // set plane parameters to feature
250   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
251   double anA, aB, aC, aD;
252   aPlane->coefficients(anA, aB, aC, aD);
253
254   // calculate attributes of the sketch
255   boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
256   boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
257   boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
258   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
259   boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
260   // X axis is preferable to be dirX on the sketch
261   const double tol = Precision::Confusion();
262   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
263   boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
264   boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
265   boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
266
267   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
268     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
269   anOrigin->setValue(anOrigPnt);
270   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
271     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
272   aNormal->setValue(aNormDir);
273   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
274     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
275   aDirX->setValue(aXDir);
276   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
277     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
278   aDirY->setValue(aYDir);
279   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
280
281   flushUpdated();
282
283   emit featureConstructed(feature(), FM_Hide);
284   emit closeLocalContext();
285   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
286 }