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>
8 #include <ModelAPI_ResultConstruction.h>
10 #include <GeomAPI_Circ2d.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomDataAPI_Dir.h>
14 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <GeomAlgoAPI_EdgeBuilder.h>
16 #include <GeomAlgoAPI_CompoundBuilder.h>
18 const double tolerance = 1e-7;
20 SketchPlugin_Arc::SketchPlugin_Arc()
21 : SketchPlugin_Feature()
25 void SketchPlugin_Arc::initAttributes()
27 data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::type());
28 data()->addAttribute(SketchPlugin_Arc::START_ID(), GeomDataAPI_Point2D::type());
29 data()->addAttribute(SketchPlugin_Arc::END_ID(), GeomDataAPI_Point2D::type());
32 void SketchPlugin_Arc::execute()
34 SketchPlugin_Sketch* aSketch = sketch();
36 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
38 // compute a circle point in 3D view
39 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
40 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
41 data()->attribute(SketchPlugin_Arc::CENTER_ID()));
42 // compute the arc start point
43 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
44 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
45 data()->attribute(SketchPlugin_Arc::START_ID()));
46 if (aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
47 boost::shared_ptr<GeomAPI_Pnt> aCenter(
48 aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
49 // make a visible point
50 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
51 aShapes.push_back(aCenterPointShape);
53 // make a visible circle
54 boost::shared_ptr<GeomDataAPI_Dir> aNDir =
55 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
56 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
57 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
59 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
60 boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
62 // compute and change the arc end point
63 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
64 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(
65 SketchPlugin_Arc::END_ID()));
66 if (anEndAttr->isInitialized())
68 boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
69 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
70 boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
71 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
72 anEndAttr->setValue(aProjection);
74 boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
76 boost::shared_ptr<GeomAPI_Shape> aCircleShape =
77 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
79 aShapes.push_back(aCircleShape);
81 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
83 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr =
84 document()->createConstruction(data());
85 aConstr->setShape(aCompound);
86 aConstr->setIsInHistory(false);
92 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
94 boost::shared_ptr<ModelAPI_Data> aData = data();
95 if (!aData->isValid())
98 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
99 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
100 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
101 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
103 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
104 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
105 aData->attribute(SketchPlugin_Arc::START_ID()));
106 aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
108 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
109 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110 aData->attribute(SketchPlugin_Arc::END_ID()));
111 aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
114 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
117 boost::shared_ptr<ModelAPI_Data> aData = data();
119 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
120 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
122 aDelta = aPoint1->pnt()->distance(thePoint);
124 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
125 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
126 aData->attribute(SketchPlugin_Arc::START_ID()));
127 double aDistance = aPoint2->pnt()->distance(thePoint);
128 if (aDelta < aDistance)
131 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 =
132 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aData->attribute(SketchPlugin_Arc::END_ID()));
134 aDistance = aPoint3->pnt()->distance(thePoint);
135 if (aDelta < aDistance)