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