1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Circle.cpp
4 // Created: 26 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_Circle.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultConstruction.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_Validator.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Session.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Circ.h>
19 #include <GeomAPI_Circ2d.h>
20 #include <GeomAPI_Vertex.h>
21 #include <GeomAPI_XY.h>
22 #include <GeomDataAPI_Point2D.h>
23 #include <GeomDataAPI_Dir.h>
24 #include <GeomAlgoAPI_PointBuilder.h>
25 #include <GeomAlgoAPI_EdgeBuilder.h>
26 #include <GeomAlgoAPI_CompoundBuilder.h>
30 const double tolerance = 1e-7;
33 SketchPlugin_Circle::SketchPlugin_Circle()
34 : SketchPlugin_SketchEntity()
38 void SketchPlugin_Circle::initDerivedClassAttributes()
40 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
41 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
43 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
44 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
47 void SketchPlugin_Circle::execute()
49 SketchPlugin_Sketch* aSketch = sketch();
54 // Compute a circle in 3D view.
55 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
56 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
57 AttributeDoublePtr aRadiusAttr = real(RADIUS_ID());
58 if(!aCenterAttr->isInitialized() || !aRadiusAttr->isInitialized()) {
62 double aRadius = aRadiusAttr->value();
63 if(aRadius < tolerance) {
67 // Make a visible point.
68 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
70 // Make a visible circle.
71 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
72 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
73 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
74 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
76 std::shared_ptr<GeomAPI_Shape> aCircleShape =
77 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
79 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
80 aResult->setShape(aCircleShape);
81 aResult->setIsInHistory(false);
82 setResult(aResult, 1);
85 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
87 std::shared_ptr<ModelAPI_Data> aData = data();
88 if(!aData->isValid()) {
92 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
93 aData->attribute(CENTER_ID()));
94 if(aPoint->isInitialized()) {
95 aPoint->move(theDeltaX, theDeltaY);
99 bool SketchPlugin_Circle::isFixed() {
100 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
103 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
104 // the second condition for unability to move external segments anywhere
105 if (theID == EXTERNAL_ID() || isFixed()) {
106 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
108 // empty shape in selection shows that the shape is equal to context
109 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
111 aSelection = anExtRes->shape();
113 // update arguments due to the selection value
114 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
115 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
116 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
117 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
118 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
119 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
120 real(RADIUS_ID())->setValue(aCirc->radius());