Salome HOME
ddf6edfa947f182f6456eee1a18d62d93706385b
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Point.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Point.h"
21 #include "SketchPlugin_Sketch.h"
22
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
28
29 #include <GeomAPI_Pnt2d.h>
30 #include <GeomAPI_Vertex.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <GeomAlgoAPI_PointBuilder.h>
33
34 SketchPlugin_Point::SketchPlugin_Point()
35     : SketchPlugin_SketchEntity()
36 {
37 }
38
39 void SketchPlugin_Point::initDerivedClassAttributes()
40 {
41   data()->addAttribute(SketchPlugin_Point::COORD_ID(), GeomDataAPI_Point2D::typeId());
42   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
43   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
44 }
45
46 void SketchPlugin_Point::execute()
47 {
48   SketchPlugin_Sketch* aSketch = sketch();
49   if (aSketch) {
50     // compute a point in 3D view
51     std::shared_ptr<GeomDataAPI_Point2D> aPoint =
52         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
53             data()->attribute(SketchPlugin_Point::COORD_ID()));
54     std::shared_ptr<GeomAPI_Pnt> aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y()));
55     // make a visible point
56     std::shared_ptr<GeomAPI_Shape> aPointShape = GeomAlgoAPI_PointBuilder::vertex(aPoint3D);
57     std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
58     aConstr->setShape(aPointShape);
59     aConstr->setIsInHistory(false);
60     setResult(aConstr);
61   }
62 }
63
64 bool SketchPlugin_Point::isFixed() {
65   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
66 }
67
68 void SketchPlugin_Point::attributeChanged(const std::string& theID) {
69   // the second condition for inability to move external point anywhere
70   if (theID == EXTERNAL_ID() || isFixed()) {
71     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
72     if (!aSelection) {
73       // empty shape in selection shows that the shape is equal to context
74       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
75       if (anExtRes)
76         aSelection = anExtRes->shape();
77     }
78     // update arguments due to the selection value
79     if (aSelection && !aSelection->isNull() && aSelection->isVertex()) {
80       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aSelection));
81       std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
82         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
83       aCoordAttr->setValue(sketch()->to2D(aVertex->point()));
84     }
85   }
86 }