]> 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_OperationEditConstraint.h>
10 #include <PartSet_Tools.h>
11
12 #include <SketchPlugin_Sketch.h>
13 #include <SketchPlugin_ConstraintLength.h>
14
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_AttributeRefList.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 <XGUI_ViewerPrs.h>
25
26 #include <AIS_Shape.hxx>
27 #include <AIS_ListOfInteractive.hxx>
28 #include <AIS_InteractiveObject.hxx>
29 #include <AIS_DimensionOwner.hxx>
30 #include <AIS_LengthDimension.hxx>
31 #include <V3d_View.hxx>
32
33 #ifdef _DEBUG
34 #include <QDebug>
35 #endif
36
37 #include <QMouseEvent>
38
39 using namespace std;
40
41 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
42                                                      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(FeaturePtr 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 void PartSet_OperationSketch::init(FeaturePtr theFeature,
63                                    const std::list<XGUI_ViewerPrs>& /*theSelected*/,
64                                    const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
65 {
66   if (theFeature)
67     setEditingFeature(theFeature);
68 }
69
70 FeaturePtr PartSet_OperationSketch::sketch() const
71 {
72   return feature();
73 }
74
75 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
76                                            const std::list<XGUI_ViewerPrs>& theSelected,
77                                            const std::list<XGUI_ViewerPrs>& theHighlighted)
78 {
79   if (!hasSketchPlane()) {
80     if (!theHighlighted.empty()) {
81       XGUI_ViewerPrs aPrs = theHighlighted.front();
82       const TopoDS_Shape& aShape = aPrs.shape();
83       if (!aShape.IsNull())
84         setSketchPlane(aShape);
85     }
86   }
87   else {
88     // if shift button is pressed and there are some already selected objects, the operation should
89     // not be started. We just want to combine some selected objects.
90     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
91     if (aHasShift && theSelected.size() > 0)
92       return;
93
94     if (theHighlighted.size() == 1) {
95       FeaturePtr aFeature = theHighlighted.front().feature();
96       if (aFeature) {
97         std::string anOperationType = getOperationType(aFeature);
98         if (theSelected.size() > 1)
99           anOperationType = PartSet_OperationFeatureEditMulti::Type();
100         restartOperation(anOperationType, aFeature);
101       }
102     }
103     else
104       myFeatures = theHighlighted;
105
106   }
107 }
108
109 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
110                                             const std::list<XGUI_ViewerPrs>& theSelected,
111                                             const std::list<XGUI_ViewerPrs>& theHighlighted)
112 {
113   if (!hasSketchPlane()) {
114   }
115   else {
116     if (theSelected.size() == 1) {
117       FeaturePtr aFeature = theSelected.front().feature();
118       if (aFeature)
119         restartOperation(getOperationType(aFeature), aFeature);
120     }
121   }
122 }
123
124 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
125 {
126   if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
127     return;
128
129   if (myFeatures.size() != 1) {
130     FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
131                                                         myFeatures);
132     if (aFeature)
133                 restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
134   }
135 }
136
137 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
138                                                         PartSet_OperationSketch::subPreview() const
139 {
140   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
141
142   boost::shared_ptr<SketchPlugin_Feature> aFeature;
143
144   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
145   if (!aData->isValid())
146     return aPreviewMap;
147   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
148         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
149
150   std::list<FeaturePtr > aFeatures = aRefList->list();
151   std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
152                                                                   aLast = aFeatures.end();
153   for (; anIt != aLast; anIt++) {
154     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
155     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
156     if (aPreview)
157       aPreviewMap[aFeature] = aPreview;
158   }
159   return aPreviewMap;
160 }
161
162 void PartSet_OperationSketch::stopOperation()
163 {
164   PartSet_OperationSketchBase::stopOperation();
165   emit featureConstructed(feature(), FM_Hide);
166   emit closeLocalContext();
167 }
168
169 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
170 {
171   return hasSketchPlane();
172 }
173
174 void PartSet_OperationSketch::startOperation()
175 {
176   PartSet_OperationSketchBase::startOperation();
177   if (!isEditOperation())
178     emit fitAllView();
179 }
180
181 bool PartSet_OperationSketch::hasSketchPlane() const
182 {
183   bool aHasPlane = false;
184
185   if (feature()) {
186     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
187     AttributeDoublePtr anAttr;
188     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
189       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
190     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
191   }
192   return aHasPlane;
193 }
194
195 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
196 {
197   if (theShape.IsNull())
198     return;
199
200   // get selected shape
201   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
202   aGShape->setImpl(new TopoDS_Shape(theShape));
203
204   // get plane parameters
205   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
206
207   // set plane parameters to feature
208   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
209   double anA, aB, aC, aD;
210   aPlane->coefficients(anA, aB, aC, aD);
211
212   // calculate attributes of the sketch
213   boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
214   boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
215   boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
216   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
217   boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
218   // X axis is preferable to be dirX on the sketch
219   const double tol = Precision::Confusion();
220   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
221   boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
222   boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
223   boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
224
225   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
226     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
227   anOrigin->setValue(anOrigPnt);
228   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
229     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
230   aNormal->setValue(aNormDir);
231   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
232     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
233   aDirX->setValue(aXDir);
234   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
235     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
236   aDirY->setValue(aYDir);
237   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
238
239   flushUpdated();
240
241   emit featureConstructed(feature(), FM_Hide);
242   emit closeLocalContext();
243   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
244 }
245
246 std::string PartSet_OperationSketch::getOperationType(FeaturePtr theFeature)
247 {
248   std::string aType = PartSet_OperationFeatureEdit::Type();
249
250   if (PartSet_Tools::isConstraintFeature(theFeature->getKind())) {
251     aType = PartSet_OperationEditConstraint::Type();
252   }
253   return aType;
254 }