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>
9 #include <GeomAPI_Pnt2d.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <GeomDataAPI_Dir.h>
14 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <GeomAlgoAPI_EdgeBuilder.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
18 #include <ModelAPI_AttributeDouble.h>
20 SketchPlugin_Circle::SketchPlugin_Circle()
21 : SketchPlugin_Feature()
25 void SketchPlugin_Circle::initAttributes()
27 data()->addAttribute(SketchPlugin_Circle::CENTER_ID(), GeomDataAPI_Point2D::type());
28 data()->addAttribute(SketchPlugin_Circle::RADIUS_ID(), ModelAPI_AttributeDouble::type());
31 void SketchPlugin_Circle::execute()
35 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Circle::preview()
37 SketchPlugin_Sketch* aSketch = sketch();
39 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
41 // compute a circle point in 3D view
42 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
43 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Circle::CENTER_ID()));
44 AttributeDoublePtr aRadiusAttr =
45 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
46 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
47 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
48 // make a visible point
49 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
50 aShapes.push_back(aCenterPointShape);
52 // make a visible circle
53 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
54 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
55 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
57 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
58 // compute the circle radius
59 double aRadius = aRadiusAttr->value();
61 boost::shared_ptr<GeomAPI_Shape> aCircleShape =
62 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
63 aShapes.push_back(aCircleShape);
66 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
67 setPreview(aCompound);
72 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Circle::getAISObject(
73 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
75 return prepareAISShape(thePrevious);
78 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
80 boost::shared_ptr<ModelAPI_Data> aData = data();
81 if (!aData->isValid())
84 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
85 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Circle::CENTER_ID()));
86 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
89 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
91 boost::shared_ptr<ModelAPI_Data> aData = data();
92 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
93 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Circle::CENTER_ID()));
95 return aPoint->pnt()->distance(thePoint);