Salome HOME
Add pictures for sketch docs
[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 bool SketchPlugin_Point::isFixed() {
66   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
67 }
68
69 void SketchPlugin_Point::attributeChanged(const std::string& theID) {
70   // the second condition for unability to move external point anywhere
71   if (theID == EXTERNAL_ID() || isFixed()) {
72     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
73     if (!aSelection) {
74       // empty shape in selection shows that the shape is equal to context
75       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
76       if (anExtRes)
77         aSelection = anExtRes->shape();
78     }
79     // update arguments due to the selection value
80     if (aSelection && !aSelection->isNull() && aSelection->isVertex()) {
81       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aSelection));
82       std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
83         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
84       aCoordAttr->setValue(sketch()->to2D(aVertex->point()));
85     }
86   }
87 }