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_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 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
134 {
135   if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
136     return;
137
138   if (myFeatures.size() != 1) {
139     FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
140                                                         myFeatures);
141     if (aFeature)
142       restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
143   }
144 }
145
146 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
147                                                         PartSet_OperationSketch::subPreview() const
148 {
149   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
150
151   boost::shared_ptr<SketchPlugin_Feature> aFeature;
152
153   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
154   if (!aData->isValid())
155     return aPreviewMap;
156   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
157         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
158
159   std::list<FeaturePtr > aFeatures = aRefList->list();
160   std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
161                                                                   aLast = aFeatures.end();
162   for (; anIt != aLast; anIt++) {
163     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
164     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
165     if (aPreview)
166       aPreviewMap[aFeature] = aPreview;
167   }
168   return aPreviewMap;
169 }
170
171 void PartSet_OperationSketch::stopOperation()
172 {
173   PartSet_OperationSketchBase::stopOperation();
174   emit featureConstructed(feature(), FM_Hide);
175   emit closeLocalContext();
176 }
177
178 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
179 {
180   return hasSketchPlane();
181 }
182
183 void PartSet_OperationSketch::startOperation()
184 {
185   if (!feature()) {
186     setFeature(createFeature());
187     emit fitAllView();
188   }
189 }
190
191 bool PartSet_OperationSketch::hasSketchPlane() const
192 {
193   bool aHasPlane = false;
194
195   if (feature()) {
196     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
197     boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
198     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
199       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
200     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
201   }
202   return aHasPlane;
203 }
204
205 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
206 {
207   if (theShape.IsNull())
208     return;
209
210   // get selected shape
211   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
212   aGShape->setImpl(new TopoDS_Shape(theShape));
213
214   // get plane parameters
215   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
216
217   // set plane parameters to feature
218   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
219   double anA, aB, aC, aD;
220   aPlane->coefficients(anA, aB, aC, aD);
221
222   // calculate attributes of the sketch
223   boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
224   boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
225   boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
226   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
227   boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
228   // X axis is preferable to be dirX on the sketch
229   const double tol = Precision::Confusion();
230   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
231   boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
232   boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
233   boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
234
235   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
236     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
237   anOrigin->setValue(anOrigPnt);
238   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
239     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
240   aNormal->setValue(aNormDir);
241   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
242     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
243   aDirX->setValue(aXDir);
244   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
245     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
246   aDirY->setValue(aYDir);
247   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
248
249   flushUpdated();
250
251   emit featureConstructed(feature(), FM_Hide);
252   emit closeLocalContext();
253   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
254 }