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