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