]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Point.cpp
Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Point.cpp
1 // Copyright (C) 2014-2019  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 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_AttributeReference.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   data()->addAttribute(PARENT_ID(), ModelAPI_AttributeReference::typeId());
47   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PARENT_ID());
48 }
49
50 void SketchPlugin_Point::execute()
51 {
52   SketchPlugin_Sketch* aSketch = sketch();
53   if (aSketch) {
54     // compute a point in 3D view
55     std::shared_ptr<GeomDataAPI_Point2D> aPoint =
56         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
57             data()->attribute(SketchPlugin_Point::COORD_ID()));
58     std::shared_ptr<GeomAPI_Pnt> aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y()));
59     // make a visible point
60     std::shared_ptr<GeomAPI_Shape> aPointShape = GeomAlgoAPI_PointBuilder::vertex(aPoint3D);
61     std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
62     aConstr->setShape(aPointShape);
63     aConstr->setIsInHistory(false);
64     setResult(aConstr);
65   }
66 }
67
68 bool SketchPlugin_Point::isFixed() {
69   return data()->selection(EXTERNAL_ID())->context().get() != NULL;
70 }
71
72 void SketchPlugin_Point::attributeChanged(const std::string& theID) {
73   // the second condition for inability to move external point anywhere
74   if (theID == EXTERNAL_ID() || isFixed()) {
75     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
76     if (!aSelection) {
77       // empty shape in selection shows that the shape is equal to context
78       ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
79       if (anExtRes)
80         aSelection = anExtRes->shape();
81     }
82     // update arguments due to the selection value
83     if (aSelection && !aSelection->isNull() && aSelection->isVertex()) {
84       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aSelection));
85       std::shared_ptr<GeomDataAPI_Point2D> aCoordAttr =
86         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(COORD_ID()));
87       aCoordAttr->setValue(sketch()->to2D(aVertex->point()));
88     }
89   }
90 }