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