1 // File: SketchPlugin_Arc.cpp
2 // Created: 26 Apr 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchPlugin_Arc.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 SketchPlugin_Arc::SketchPlugin_Arc()
19 : SketchPlugin_Feature()
23 void SketchPlugin_Arc::initAttributes()
25 data()->addAttribute(ARC_ATTR_CENTER, GeomDataAPI_Point2D::type());
26 data()->addAttribute(ARC_ATTR_START, GeomDataAPI_Point2D::type());
27 data()->addAttribute(ARC_ATTR_END, GeomDataAPI_Point2D::type());
30 void SketchPlugin_Arc::execute()
34 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Arc::preview()
36 SketchPlugin_Sketch* aSketch = sketch();
38 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
40 // compute a circle point in 3D view
41 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
42 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_CENTER));
43 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
44 // make a visible point
45 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
46 aShapes.push_back(aCenterPointShape);
48 // make a visible circle
49 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
50 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aSketch->data()->attribute(SKETCH_ATTR_NORM));
51 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
53 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
54 // compute the arc start point
55 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
56 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_START));
57 boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
59 // compute the arc end point
60 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
61 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ARC_ATTR_END));
62 boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
64 boost::shared_ptr<GeomAPI_Shape> aCircleShape =
65 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
67 aShapes.push_back(aCircleShape);
69 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
70 setPreview(aCompound);
75 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
77 boost::shared_ptr<ModelAPI_Data> aData = data();
78 if (!aData->isValid())
81 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
82 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
83 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
85 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
86 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
87 aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
89 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
90 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
91 aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
94 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
97 boost::shared_ptr<ModelAPI_Data> aData = data();
99 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
100 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
101 aDelta = aPoint1->pnt()->distance(thePoint);
103 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
104 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
105 double aDistance = aPoint2->pnt()->distance(thePoint);
106 if (aDelta < aDistance)
109 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
110 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_END));
111 aDistance = aPoint3->pnt()->distance(thePoint);
112 if (aDelta < aDistance)