Salome HOME
Put groups to the separated plugin: Collection
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Point.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_Point.cpp
4 // Created: 07 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_Point.h"
8 #include "SketchPlugin_Sketch.h"
9
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_Validator.h>
14 #include <ModelAPI_Session.h>
15
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Vertex.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAlgoAPI_PointBuilder.h>
20
21 SketchPlugin_Point::SketchPlugin_Point()
22     : SketchPlugin_SketchEntity()
23 {
24 }
25
26 void SketchPlugin_Point::initDerivedClassAttributes()
27 {
28   data()->addAttribute(SketchPlugin_Point::COORD_ID(), GeomDataAPI_Point2D::typeId());
29   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
30   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
31 }
32
33 void SketchPlugin_Point::execute()
34 {
35   SketchPlugin_Sketch* aSketch = sketch();
36   if (aSketch) {
37     // compute a point in 3D view
38     std::shared_ptr<GeomDataAPI_Point2D> aPoint =
39         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
40             data()->attribute(SketchPlugin_Point::COORD_ID()));
41     std::shared_ptr<GeomAPI_Pnt> aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y()));
42     // make a visible point
43     std::shared_ptr<GeomAPI_Shape> aPointShape = GeomAlgoAPI_PointBuilder::vertex(aPoint3D);
44     std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
45     aConstr->setShape(aPointShape);
46     aConstr->setIsInHistory(false);
47     setResult(aConstr);
48   }
49 }
50
51 void SketchPlugin_Point::move(double theDeltaX, double theDeltaY)
52 {
53   std::shared_ptr<ModelAPI_Data> aData = data();
54   if (!aData->isValid())
55     return;
56
57   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
58       aData->attribute(SketchPlugin_Point::COORD_ID()));
59   aPoint1->move(theDeltaX, theDeltaY);
60 }
61
62 bool SketchPlugin_Point::isFixed() {
63   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
64 }
65
66 void SketchPlugin_Point::attributeChanged(const std::string& theID) {
67   // the second condition for unability to move external point anywhere
68   if (theID == EXTERNAL_ID() || isFixed()) {
69     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
70     if (!aSelection) {
71       // empty shape in selection shows that the shape is equal to context
72       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
73       if (anExtRes)
74         aSelection = anExtRes->shape();
75     }
76     // update arguments due to the selection value
77     if (aSelection && !aSelection->isNull() && aSelection->isVertex()) {
78       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aSelection));
79       std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
80         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
81       aCoordAttr->setValue(sketch()->to2D(aVertex->point()));
82     }
83   }
84 }