Salome HOME
3bb916fab0926f66e023cdf65286d2bf88461962
[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), myIsEditMode(false)
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 (!myIsEditMode)
48     aModes.push_back(TopAbs_FACE);
49   else
50     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
51   return aModes;
52 }
53
54 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
55                                            const std::list<XGUI_ViewerPrs>& theSelected)
56 {
57   myFeatures = theSelected;
58 }
59
60 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
61                                             const std::list<XGUI_ViewerPrs>& theSelected)
62 {
63   if (theSelected.empty())
64     return;
65
66   if (!myIsEditMode) {
67     XGUI_ViewerPrs aPrs = theSelected.front();
68     const TopoDS_Shape& aShape = aPrs.shape();
69     if (!aShape.IsNull()) {
70       setSketchPlane(aShape);
71       myIsEditMode = true;
72     }
73   }
74   else {
75     if (theSelected.size() == 1) {
76       boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
77       if (aFeature)
78         emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
79     }
80   }
81   myFeatures.clear();
82 }
83
84 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
85 {
86   if (!myIsEditMode || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
87     return;
88
89   if (myFeatures.size() != 1) {
90     boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
91                                                                 theView, feature(), myFeatures);
92     if (aFeature)
93       emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
94   }
95 }
96
97 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
98                                                         PartSet_OperationSketch::preview() const
99 {
100   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
101
102   boost::shared_ptr<SketchPlugin_Feature> aFeature;
103
104   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
105   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
106         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
107
108   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
109   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
110                                                                   aLast = aFeatures.end();
111   for (; anIt != aLast; anIt++) {
112     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
113     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
114     if (aPreview)
115       aPreviewMap[aFeature] = aPreview;
116   }
117   return aPreviewMap;
118 }
119
120 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
121 {
122   if (theShape.IsNull())
123     return;
124
125   // get selected shape
126   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
127   aGShape->setImpl(new TopoDS_Shape(theShape));
128
129   // get plane parameters
130   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
131
132   // set plane parameters to feature
133   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
134   double anA, aB, aC, aD;
135   aPlane->coefficients(anA, aB, aC, aD);
136
137   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
138   /*
139   aData->real(SKETCH_ATTR_PLANE_A)->setValue(anA);
140   aData->real(SKETCH_ATTR_PLANE_B)->setValue(aB);
141   aData->real(SKETCH_ATTR_PLANE_C)->setValue(aC);
142   aData->real(SKETCH_ATTR_PLANE_D)->setValue(aD);
143   */
144   // temporary solution for main planes only
145   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
146     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
147   anOrigin->setValue(0, 0, 0);
148   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
149     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
150   aNormal->setValue(anA, aB, aC);
151   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
152     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
153   aDirX->setValue(aB, aC, anA);
154   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
155     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
156   aDirY->setValue(aC, anA, aB);
157   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
158   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
159 }