Salome HOME
Merge branch 'master' of newgeom:newgeom
[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
9 #include <GeomAPI_Pnt.h>
10 #include <GeomAPI_Lin2d.h>
11 #include <GeomAPI_Pnt2d.h>
12
13 #include <GeomAlgoAPI_EdgeBuilder.h>
14 #include <GeomDataAPI_Point2D.h>
15
16 #include <AIS_InteractiveObject.hxx>
17
18 using namespace std;
19
20 // face of the square-face displayed for selection of general plane
21 const double PLANE_SIZE = 200;
22
23 SketchPlugin_Line::SketchPlugin_Line()
24   : SketchPlugin_Feature()
25 {
26 }
27
28 void SketchPlugin_Line::initAttributes()
29 {
30   data()->addAttribute(LINE_ATTR_START, GeomDataAPI_Point2D::type());
31   data()->addAttribute(LINE_ATTR_END, GeomDataAPI_Point2D::type());
32 }
33
34 void SketchPlugin_Line::execute() 
35 {
36 }
37
38 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Line::preview()
39 {
40   SketchPlugin_Sketch* aSketch = sketch();
41   if (aSketch) {
42     // compute a start point in 3D view
43     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
44       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(LINE_ATTR_START));
45     // compute an end point in 3D view
46     boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
47       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(LINE_ATTR_END));
48     if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
49       boost::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
50       boost::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
51       // make linear edge
52       boost::shared_ptr<GeomAPI_Shape> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
53       setPreview(anEdge);
54     }
55   }
56   return getPreview();
57 }
58
59 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
60 {
61   boost::shared_ptr<ModelAPI_Data> aData = data();
62   if (!aData->isValid())
63     return;
64
65   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
66         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
67   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
68
69   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
70         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
71   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
72 }
73
74 double SketchPlugin_Line::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
75 {
76   double aDelta = 0;
77
78   boost::shared_ptr<ModelAPI_Data> aData = data();
79   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
80         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
81   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
82         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
83
84   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
85
86   if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
87     boost::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
88     aDelta = aResult->distance(thePoint);
89   }
90   else { // distance
91     aDelta = aLin2d.distance(thePoint);
92   }
93
94   return aDelta;
95 }