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>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Dir.h>
13 #include <GeomAlgoAPI_PointBuilder.h>
14 #include <GeomAlgoAPI_EdgeBuilder.h>
15 #include <GeomAlgoAPI_CompoundBuilder.h>
16 #include <ModelAPI_AttributeDouble.h>
18 SketchPlugin_Circle::SketchPlugin_Circle()
19 : SketchPlugin_Feature()
23 void SketchPlugin_Circle::initAttributes()
25 data()->addAttribute(CIRCLE_ATTR_CENTER, GeomDataAPI_Point2D::type());
26 data()->addAttribute(CIRCLE_ATTR_RADIUS, ModelAPI_AttributeDouble::type());
29 void SketchPlugin_Circle::execute()
31 SketchPlugin_Sketch* aSketch = sketch();
33 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
35 // compute a circle point in 3D view
36 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
37 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CIRCLE_ATTR_CENTER));
38 AttributeDoublePtr aRadiusAttr =
39 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CIRCLE_ATTR_RADIUS));
40 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
41 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
42 // make a visible point
43 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
44 aShapes.push_back(aCenterPointShape);
46 // make a visible circle
47 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
48 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
49 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
51 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
52 // compute the circle radius
53 double aRadius = aRadiusAttr->value();
55 boost::shared_ptr<GeomAPI_Shape> aCircleShape =
56 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
57 aShapes.push_back(aCircleShape);
60 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
62 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction();
63 aConstr->setShape(aCompound);
64 results().push_back(aConstr);
68 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
70 boost::shared_ptr<ModelAPI_Data> aData = data();
71 if (!aData->isValid())
74 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
75 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
76 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
79 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
81 boost::shared_ptr<ModelAPI_Data> aData = data();
82 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
83 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
85 return aPoint->pnt()->distance(thePoint);