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