1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Point.cpp
4 // Created: 07 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_Point.h"
8 #include "SketchPlugin_Sketch.h"
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>
16 #include <GeomAPI_Pnt2d.h>
17 #include <GeomAPI_Vertex.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAlgoAPI_PointBuilder.h>
23 SketchPlugin_Point::SketchPlugin_Point()
24 : SketchPlugin_SketchEntity()
28 void SketchPlugin_Point::initAttributes()
30 SketchPlugin_SketchEntity::initAttributes();
32 data()->addAttribute(SketchPlugin_Point::COORD_ID(), GeomDataAPI_Point2D::type());
33 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
34 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
37 void SketchPlugin_Point::execute()
39 SketchPlugin_Sketch* aSketch = sketch();
41 // compute a point in 3D view
42 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
43 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
44 data()->attribute(SketchPlugin_Point::COORD_ID()));
45 std::shared_ptr<GeomAPI_Pnt> aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y()));
46 // make a visible point
47 std::shared_ptr<GeomAPI_Shape> aPointShape = GeomAlgoAPI_PointBuilder::point(aPoint3D);
48 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
49 aConstr->setShape(aPointShape);
50 aConstr->setIsInHistory(false);
55 void SketchPlugin_Point::move(double theDeltaX, double theDeltaY)
57 std::shared_ptr<ModelAPI_Data> aData = data();
58 if (!aData->isValid())
61 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
62 aData->attribute(SketchPlugin_Point::COORD_ID()));
63 aPoint1->move(theDeltaX, theDeltaY);
66 double SketchPlugin_Point::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
68 std::shared_ptr<ModelAPI_Data> aData = data();
69 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
70 aData->attribute(SketchPlugin_Point::COORD_ID()));
72 return aPoint->pnt()->distance(thePoint);
75 bool SketchPlugin_Point::isFixed() {
76 return data()->selection(EXTERNAL_ID())->context().get();
79 void SketchPlugin_Point::attributeChanged(const std::string& theID) {
80 if (theID == EXTERNAL_ID()) {
81 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
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()));