Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Line.cpp
1 // File:        SketchPlugin_Line.cxx
2 // Created:     27 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "SketchPlugin_Line.h"
6 #include "SketchPlugin_Sketch.h"
7 #include <ModelAPI_Data.h>
8 #include <GeomAPI_Pnt.h>
9 #include <GeomAlgoAPI_EdgeBuilder.h>
10 #include <GeomDataAPI_Point2D.h>
11
12 using namespace std;
13
14 // face of the square-face displayed for selection of general plane
15 const double PLANE_SIZE = 200;
16
17 SketchPlugin_Line::SketchPlugin_Line()
18   : SketchPlugin_Feature()
19 {
20 }
21
22 void SketchPlugin_Line::initAttributes()
23 {
24   data()->addAttribute(LINE_ATTR_START, GeomDataAPI_Point2D::type());
25   data()->addAttribute(LINE_ATTR_END, GeomDataAPI_Point2D::type());
26 }
27
28 void SketchPlugin_Line::execute() 
29 {
30 }
31
32 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Line::preview()
33 {
34   SketchPlugin_Sketch* aSketch = sketch();
35   if (aSketch) {
36     // compute a start point in 3D view
37     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
38       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(LINE_ATTR_START));
39     boost::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
40     // compute an end point in 3D view
41     boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
42       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(LINE_ATTR_END));
43     boost::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
44     // make linear edge
45     boost::shared_ptr<GeomAPI_Shape> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
46     setPreview(anEdge);
47   }
48   return getPreview();
49 }
50
51 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
52 {
53   boost::shared_ptr<ModelAPI_Data> aData = data();
54   if (!aData->isValid())
55     return;
56
57   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
58         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
59   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
60
61   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
62         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
63   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
64 }