Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[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 <GeomAlgoAPI_FaceBuilder.h>
15 #include <GeomDataAPI_Point.h>
16 #include <GeomDataAPI_Dir.h>
17
18 #include <XGUI_ViewerPrs.h>
19
20 #include <AIS_Shape.hxx>
21 #include <AIS_ListOfInteractive.hxx>
22 #include <V3d_View.hxx>
23
24 #ifdef _DEBUG
25 #include <QDebug>
26 #endif
27
28 #include <QMouseEvent>
29
30 using namespace std;
31
32 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
33                                                      QObject* theParent)
34 : PartSet_OperationSketchBase(theId, theParent), myIsEditMode(false)
35 {
36 }
37
38 PartSet_OperationSketch::~PartSet_OperationSketch()
39 {
40 }
41
42 std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
43 {
44   std::list<int> aModes;
45   if (!myIsEditMode)
46     aModes.push_back(TopAbs_FACE);
47   else {
48     aModes.push_back(TopAbs_VERTEX);
49     aModes.push_back(TopAbs_EDGE);
50   }
51   return aModes;
52 }
53
54 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
55                                             const std::list<XGUI_ViewerPrs>& theSelected)
56 {
57   if (theSelected.empty())
58     return;
59
60   if (!myIsEditMode) {
61     XGUI_ViewerPrs aPrs = theSelected.front();
62     const TopoDS_Shape& aShape = aPrs.shape();
63     if (!aShape.IsNull()) {
64       setSketchPlane(aShape);
65       myIsEditMode = true;
66     }
67   }
68 }
69
70 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView,
71                                          const std::list<XGUI_ViewerPrs>& theSelected)
72 {
73   if (!myIsEditMode || !(theEvent->buttons() &  Qt::LeftButton) || theSelected.empty())
74     return;
75
76   boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
77                                                               theView, feature(), theSelected);
78   if (aFeature)
79     emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
80 }
81
82 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
83 {
84   if (theShape.IsNull())
85     return;
86
87   // get selected shape
88   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
89   aGShape->setImpl(new TopoDS_Shape(theShape));
90
91   // get plane parameters
92   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
93
94   // set plane parameters to feature
95   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
96   double anA, aB, aC, aD;
97   aPlane->coefficients(anA, aB, aC, aD);
98
99   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
100   /*
101   aData->real(SKETCH_ATTR_PLANE_A)->setValue(anA);
102   aData->real(SKETCH_ATTR_PLANE_B)->setValue(aB);
103   aData->real(SKETCH_ATTR_PLANE_C)->setValue(aC);
104   aData->real(SKETCH_ATTR_PLANE_D)->setValue(aD);
105   */
106   // temporary solution for main planes only
107   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
108     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
109   anOrigin->setValue(0, 0, 0);
110   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
111     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
112   aNormal->setValue(anA, aB, aC);
113   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
114     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
115   aDirX->setValue(aB, aC, anA);
116   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
117     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
118   aDirY->setValue(aC, anA, aB);
119   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
120   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
121 }