]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Line.cpp
Salome HOME
Working with Construction results in SketchPlugin
[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 // face of the square-face displayed for selection of general plane
20 const double PLANE_SIZE = 200;
21
22 SketchPlugin_Line::SketchPlugin_Line()
23   : SketchPlugin_Feature()
24 {
25 }
26
27 void SketchPlugin_Line::initAttributes()
28 {
29   data()->addAttribute(LINE_ATTR_START, GeomDataAPI_Point2D::type());
30   data()->addAttribute(LINE_ATTR_END, GeomDataAPI_Point2D::type());
31 }
32
33 void SketchPlugin_Line::execute() 
34 {
35   SketchPlugin_Sketch* aSketch = sketch();
36   if (aSketch) {
37     // compute a start point in 3D view
38     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
39       boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(LINE_ATTR_START));
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     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 = document()->createConstruction();
50       aConstr->setShape(anEdge);
51       results().push_back(aConstr);
52     }
53   }
54 }
55
56 void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
57 {
58   boost::shared_ptr<ModelAPI_Data> aData = data();
59   if (!aData->isValid())
60     return;
61
62   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
63         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
64   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
65
66   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
67         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
68   aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
69 }
70
71 double SketchPlugin_Line::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
72 {
73   double aDelta = 0;
74
75   boost::shared_ptr<ModelAPI_Data> aData = data();
76   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
77         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
78   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
79         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
80
81   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
82
83   if (false/*projection*/) { // TODO: if it has not been necessary, remove this block
84     boost::shared_ptr<GeomAPI_Pnt2d> aResult = aLin2d.project(thePoint);
85     aDelta = aResult->distance(thePoint);
86   }
87   else { // distance
88     aDelta = aLin2d.distance(thePoint);
89   }
90
91   return aDelta;
92 }