Salome HOME
c43d16ef0a87a310c29ee1f3d3bbdf2be38993ee
[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_OperationEditLine.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
20 #include <XGUI_ViewerPrs.h>
21
22 #include <AIS_Shape.hxx>
23 #include <AIS_ListOfInteractive.hxx>
24 #include <V3d_View.hxx>
25
26 #ifdef _DEBUG
27 #include <QDebug>
28 #endif
29
30 #include <QMouseEvent>
31
32 using namespace std;
33
34 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
35                                                      QObject* theParent)
36 : PartSet_OperationSketchBase(theId, theParent)
37 {
38 }
39
40 PartSet_OperationSketch::~PartSet_OperationSketch()
41 {
42 }
43
44 std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
45 {
46   std::list<int> aModes;
47   if (!hasSketchPlane())
48     aModes.push_back(TopAbs_FACE);
49   else
50     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
51   return aModes;
52 }
53
54 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketch::sketch() const
55 {
56   return feature();
57 }
58
59 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
60                                            const std::list<XGUI_ViewerPrs>& /*theSelected*/,
61                                            const std::list<XGUI_ViewerPrs>& theHighlighted)
62 {
63   if (!hasSketchPlane()) {
64     XGUI_ViewerPrs aPrs = theHighlighted.front();
65     const TopoDS_Shape& aShape = aPrs.shape();
66     if (!aShape.IsNull())
67       setSketchPlane(aShape);
68   }
69   else {
70     if (theHighlighted.size() == 1) {
71       boost::shared_ptr<ModelAPI_Feature> aFeature = theHighlighted.front().feature();
72       if (aFeature)
73         emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
74     }
75     else
76       myFeatures = theHighlighted;
77   }
78 }
79
80 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
81 {
82   if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
83     return;
84
85   if (myFeatures.size() != 1) {
86     boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
87                                                                 theView, feature(), myFeatures);
88     if (aFeature)
89       emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
90   }
91 }
92
93 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
94                                                         PartSet_OperationSketch::subPreview() const
95 {
96   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
97
98   boost::shared_ptr<SketchPlugin_Feature> aFeature;
99
100   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
101   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
102         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
103
104   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
105   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
106                                                                   aLast = aFeatures.end();
107   for (; anIt != aLast; anIt++) {
108     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
109     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
110     if (aPreview)
111       aPreviewMap[aFeature] = aPreview;
112   }
113   return aPreviewMap;
114 }
115
116 void PartSet_OperationSketch::stopOperation()
117 {
118   PartSet_OperationSketchBase::stopOperation();
119   emit featureConstructed(feature(), FM_Hide);
120   emit closeLocalContext();
121 }
122
123 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
124 {
125   return hasSketchPlane();
126 }
127
128 bool PartSet_OperationSketch::hasSketchPlane() const
129 {
130   bool aHasPlane = false;
131
132   if (feature()) {
133     // set plane parameters to feature
134     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
135
136     boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
137     // temporary solution for main planes only
138     boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
139       boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
140     double aX = aNormal->x(), anY = aNormal->y(), aZ = aNormal->z();
141
142     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
143   }
144   return aHasPlane;
145 }
146
147 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
148 {
149   if (theShape.IsNull())
150     return;
151
152   // get selected shape
153   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
154   aGShape->setImpl(new TopoDS_Shape(theShape));
155
156   // get plane parameters
157   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
158
159   // set plane parameters to feature
160   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
161   double anA, aB, aC, aD;
162   aPlane->coefficients(anA, aB, aC, aD);
163
164   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
165   // temporary solution for main planes only
166   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
167     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
168   anOrigin->setValue(0, 0, 0);
169   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
170     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
171   aNormal->setValue(anA, aB, aC);
172   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
173     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
174   aDirX->setValue(aB, aC, anA);
175   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
176     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
177   aDirY->setValue(aC, anA, aB);
178   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
179
180   flushUpdated();
181
182   emit featureConstructed(feature(), FM_Hide);
183   emit closeLocalContext();
184   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
185 }