1 // File: SketchPlugin_Circle.cpp
2 // Created: 26 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_Circle.h"
6 #include "SketchPlugin_Sketch.h"
7 #include <ModelAPI_Data.h>
8 #include <ModelAPI_ResultConstruction.h>
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_Session.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_Circ.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <GeomDataAPI_Dir.h>
18 #include <GeomAlgoAPI_PointBuilder.h>
19 #include <GeomAlgoAPI_EdgeBuilder.h>
20 #include <GeomAlgoAPI_CompoundBuilder.h>
22 SketchPlugin_Circle::SketchPlugin_Circle()
23 : SketchPlugin_Feature()
27 void SketchPlugin_Circle::initAttributes()
29 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::type());
30 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::type());
31 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
32 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
35 void SketchPlugin_Circle::execute()
37 SketchPlugin_Sketch* aSketch = sketch();
39 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
41 // compute a circle point in 3D view
42 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
43 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
44 AttributeDoublePtr aRadiusAttr =
45 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
46 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
47 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
48 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
49 // make a visible point
50 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
51 std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
53 aConstr1->setShape(aCenterPointShape);
54 aConstr1->setIsInHistory(false);
55 setResult(aConstr1, 0);
57 // make a visible circle
58 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
59 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
60 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
62 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
63 // compute the circle radius
64 double aRadius = aRadiusAttr->value();
66 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
67 aCenter, aNormal, aRadius);
68 aShapes.push_back(aCircleShape);
69 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
71 aConstr2->setShape(aCircleShape);
72 aConstr2->setIsInHistory(false);
73 setResult(aConstr2, 1);
79 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
81 std::shared_ptr<ModelAPI_Data> aData = data();
82 if (!aData->isValid())
85 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
86 aData->attribute(CENTER_ID()));
87 aPoint1->move(theDeltaX, theDeltaY);
90 double SketchPlugin_Circle::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
92 std::shared_ptr<ModelAPI_Data> aData = data();
93 std::shared_ptr<GeomDataAPI_Point2D> aPoint =
94 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CENTER_ID()));
96 return aPoint->pnt()->distance(thePoint);
99 bool SketchPlugin_Circle::isFixed() {
100 return data()->selection(EXTERNAL_ID())->context();
103 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
104 if (theID == EXTERNAL_ID()) {
105 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
106 // update arguments due to the selection value
107 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
108 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
109 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
110 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
111 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
112 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
113 real(RADIUS_ID())->setValue(aCirc->radius());