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