Salome HOME
c198e9b90fd22c44e7c066ea30ee88a022aa2df1
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Point.cpp
1 // File:    SketchPlugin_Point.cpp
2 // Created: 07 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchPlugin_Point.h"
6 #include "SketchPlugin_Sketch.h"
7
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_ResultConstruction.h>
10 #include <ModelAPI_AttributeSelection.h>
11 #include <ModelAPI_Validator.h>
12
13 #include <GeomAPI_Pnt2d.h>
14
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAlgoAPI_PointBuilder.h>
17
18 using namespace std;
19
20 SketchPlugin_Point::SketchPlugin_Point()
21 {
22 }
23
24 void SketchPlugin_Point::initAttributes()
25 {
26   data()->addAttribute(SketchPlugin_Point::COORD_ID(), GeomDataAPI_Point2D::type());
27   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
28   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
29 }
30
31 void SketchPlugin_Point::execute()
32 {
33   SketchPlugin_Sketch* aSketch = sketch();
34   if (aSketch) {
35     // compute a point in 3D view
36     boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
37         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
38             data()->attribute(SketchPlugin_Point::COORD_ID()));
39     boost::shared_ptr<GeomAPI_Pnt> aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y()));
40     // make a visible point
41     boost::shared_ptr<GeomAPI_Shape> aPointShape = GeomAlgoAPI_PointBuilder::point(aPoint3D);
42     boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
43     aConstr->setShape(aPointShape);
44     aConstr->setIsInHistory(false);
45     setResult(aConstr);
46   }
47 }
48
49 void SketchPlugin_Point::move(double theDeltaX, double theDeltaY)
50 {
51   boost::shared_ptr<ModelAPI_Data> aData = data();
52   if (!aData->isValid())
53     return;
54
55   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
56       aData->attribute(SketchPlugin_Point::COORD_ID()));
57   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
58 }
59
60 double SketchPlugin_Point::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
61 {
62   boost::shared_ptr<ModelAPI_Data> aData = data();
63   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
64       aData->attribute(SketchPlugin_Point::COORD_ID()));
65
66   return aPoint->pnt()->distance(thePoint);
67 }
68
69 bool SketchPlugin_Point::isFixed() {
70   return data()->selection(EXTERNAL_ID())->context();
71 }