Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Point.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_Point.h"
22 #include "SketchPlugin_Sketch.h"
23
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_Vertex.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <GeomAlgoAPI_PointBuilder.h>
34
35 SketchPlugin_Point::SketchPlugin_Point()
36     : SketchPlugin_SketchEntity()
37 {
38 }
39
40 void SketchPlugin_Point::initDerivedClassAttributes()
41 {
42   data()->addAttribute(SketchPlugin_Point::COORD_ID(), GeomDataAPI_Point2D::typeId());
43   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
44   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
45 }
46
47 void SketchPlugin_Point::execute()
48 {
49   SketchPlugin_Sketch* aSketch = sketch();
50   if (aSketch) {
51     // compute a point in 3D view
52     std::shared_ptr<GeomDataAPI_Point2D> aPoint =
53         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
54             data()->attribute(SketchPlugin_Point::COORD_ID()));
55     std::shared_ptr<GeomAPI_Pnt> aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y()));
56     // make a visible point
57     std::shared_ptr<GeomAPI_Shape> aPointShape = GeomAlgoAPI_PointBuilder::vertex(aPoint3D);
58     std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
59     aConstr->setShape(aPointShape);
60     aConstr->setIsInHistory(false);
61     setResult(aConstr);
62   }
63 }
64
65 void SketchPlugin_Point::move(double theDeltaX, double theDeltaY)
66 {
67   std::shared_ptr<ModelAPI_Data> aData = data();
68   if (!aData->isValid())
69     return;
70
71   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
72       aData->attribute(SketchPlugin_Point::COORD_ID()));
73   aPoint1->move(theDeltaX, theDeltaY);
74 }
75
76 bool SketchPlugin_Point::isFixed() {
77   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
78 }
79
80 void SketchPlugin_Point::attributeChanged(const std::string& theID) {
81   // the second condition for unability to move external point anywhere
82   if (theID == EXTERNAL_ID() || isFixed()) {
83     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
84     if (!aSelection) {
85       // empty shape in selection shows that the shape is equal to context
86       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
87       if (anExtRes)
88         aSelection = anExtRes->shape();
89     }
90     // update arguments due to the selection value
91     if (aSelection && !aSelection->isNull() && aSelection->isVertex()) {
92       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aSelection));
93       std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
94         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
95       aCoordAttr->setValue(sketch()->to2D(aVertex->point()));
96     }
97   }
98 }