Salome HOME
46b0c4ca2a70d4da3dd3389c596dbea623b2762e
[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
9 #include <SketchPlugin_Sketch.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <GeomAlgoAPI_FaceBuilder.h>
13 #include <GeomDataAPI_Point.h>
14 #include <GeomDataAPI_Dir.h>
15
16 #include <AIS_Shape.hxx>
17 #include <AIS_ListOfInteractive.hxx>
18
19 #ifdef _DEBUG
20 #include <QDebug>
21 #endif
22
23 using namespace std;
24
25 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
26                                                      QObject* theParent)
27 : PartSet_OperationSketchBase(theId, theParent), myIsEditMode(false)
28 {
29 }
30
31 PartSet_OperationSketch::~PartSet_OperationSketch()
32 {
33 }
34
35 std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
36 {
37   std::list<int> aModes;
38   if (!myIsEditMode)
39     aModes.push_back(TopAbs_FACE);
40   else {
41     aModes.push_back(TopAbs_VERTEX);
42     aModes.push_back(TopAbs_EDGE);
43   }
44   return aModes;
45 }
46
47 void PartSet_OperationSketch::setSelected(boost::shared_ptr<ModelAPI_Feature> theFeature,
48                                           const TopoDS_Shape& theShape)
49 {
50   if (theShape.IsNull())
51     return;
52
53   if (!myIsEditMode) {
54     setSketchPlane(theShape);
55     myIsEditMode = true;
56   }
57   else if (theFeature)
58     emit launchOperation(PartSet_OperationEditLine::Type(), theFeature);
59 }
60
61 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
62 {
63   // get selected shape
64   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
65   aGShape->setImpl(new TopoDS_Shape(theShape));
66
67   // get plane parameters
68   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
69
70   // set plane parameters to feature
71   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
72   double anA, aB, aC, aD;
73   aPlane->coefficients(anA, aB, aC, aD);
74
75   boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
76   /*
77   aData->real(SKETCH_ATTR_PLANE_A)->setValue(anA);
78   aData->real(SKETCH_ATTR_PLANE_B)->setValue(aB);
79   aData->real(SKETCH_ATTR_PLANE_C)->setValue(aC);
80   aData->real(SKETCH_ATTR_PLANE_D)->setValue(aD);
81   */
82   // temporary solution for main planes only
83   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
84     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
85   anOrigin->setValue(0, 0, 0);
86   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
87     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
88   aNormal->setValue(anA, aB, aC);
89   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
90     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
91   aDirX->setValue(aB, aC, anA);
92   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
93     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
94   aDirY->setValue(aC, anA, aB);
95   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
96   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
97 }
98