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>
9 #include <ModelAPI_AttributeSelection.h>
10 #include <ModelAPI_Validator.h>
12 #include <GeomAPI_Circ2d.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomAlgoAPI_PointBuilder.h>
17 #include <GeomAlgoAPI_EdgeBuilder.h>
18 #include <GeomAlgoAPI_CompoundBuilder.h>
20 const double tolerance = 1e-7;
22 SketchPlugin_Arc::SketchPlugin_Arc()
23 : SketchPlugin_Feature()
27 void SketchPlugin_Arc::initAttributes()
29 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::type());
30 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::type());
31 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::type());
32 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
33 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
36 void SketchPlugin_Arc::execute()
38 SketchPlugin_Sketch* aSketch = sketch();
40 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
42 // compute a circle point in 3D view
43 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
44 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
45 // compute the arc start point
46 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
47 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
48 if (aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
49 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
50 // make a visible point
51 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
52 //aShapes.push_back(aCenterPointShape);
53 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
55 aConstr1->setShape(aCenterPointShape);
56 aConstr1->setIsInHistory(false);
57 setResult(aConstr1, 0);
59 // make a visible circle
60 boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
61 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
62 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
64 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
65 boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
67 // compute and change the arc end point
68 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
69 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
70 if (anEndAttr->isInitialized()) {
71 boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
72 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
73 boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
74 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
75 anEndAttr->setValue(aProjection);
77 boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
79 boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
80 aCenter, aStartPoint, aEndPoint, aNormal);
82 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
84 aConstr2->setShape(aCircleShape);
85 aConstr2->setIsInHistory(false);
86 setResult(aConstr2, 1);
87 //aShapes.push_back(aCircleShape);
91 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
93 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr =
94 document()->createConstruction(data());
95 aConstr->setShape(aCompound);
96 aConstr->setIsInHistory(false);
103 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
105 boost::shared_ptr<ModelAPI_Data> aData = data();
106 if (!aData->isValid())
109 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
110 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
111 aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
113 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
114 aData->attribute(SketchPlugin_Arc::START_ID()));
115 aPoint2->setValue(aPoint2->x() + theDeltaX, aPoint2->y() + theDeltaY);
117 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
118 aData->attribute(SketchPlugin_Arc::END_ID()));
119 aPoint3->setValue(aPoint3->x() + theDeltaX, aPoint3->y() + theDeltaY);
122 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
125 boost::shared_ptr<ModelAPI_Data> aData = data();
127 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
129 aDelta = aPoint1->pnt()->distance(thePoint);
131 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
132 aData->attribute(SketchPlugin_Arc::START_ID()));
133 double aDistance = aPoint2->pnt()->distance(thePoint);
134 if (aDelta < aDistance)
137 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
138 aData->attribute(SketchPlugin_Arc::END_ID()));
139 aDistance = aPoint3->pnt()->distance(thePoint);
140 if (aDelta < aDistance)
146 bool SketchPlugin_Arc::isFixed() {
147 return data()->selection(EXTERNAL_ID())->context();