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>
11 #include <ModelAPI_Session.h>
13 #include <GeomAPI_Circ2d.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomDataAPI_Dir.h>
17 #include <GeomAlgoAPI_PointBuilder.h>
18 #include <GeomAlgoAPI_EdgeBuilder.h>
19 #include <GeomAlgoAPI_CompoundBuilder.h>
21 const double tolerance = 1e-7;
23 SketchPlugin_Arc::SketchPlugin_Arc()
24 : SketchPlugin_Feature()
28 void SketchPlugin_Arc::initAttributes()
30 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::type());
31 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::type());
32 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::type());
33 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
34 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
37 void SketchPlugin_Arc::execute()
39 SketchPlugin_Sketch* aSketch = sketch();
41 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
43 // compute a circle point in 3D view
44 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
45 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
46 // compute the arc start point
47 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
48 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
49 if (aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
50 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
51 // make a visible point
52 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
53 //aShapes.push_back(aCenterPointShape);
54 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
56 aConstr1->setShape(aCenterPointShape);
57 aConstr1->setIsInHistory(false);
58 setResult(aConstr1, 0);
60 // make a visible circle
61 boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
62 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
63 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
65 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
66 boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
68 // compute and change the arc end point
69 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
70 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
71 if (anEndAttr->isInitialized()) {
72 boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
73 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
74 boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
75 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
76 anEndAttr->setValue(aProjection);
78 boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
80 boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
81 aCenter, aStartPoint, aEndPoint, aNormal);
83 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
85 aConstr2->setShape(aCircleShape);
86 aConstr2->setIsInHistory(false);
87 setResult(aConstr2, 1);
88 //aShapes.push_back(aCircleShape);
92 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
94 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr =
95 document()->createConstruction(data());
96 aConstr->setShape(aCompound);
97 aConstr->setIsInHistory(false);
104 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
106 boost::shared_ptr<ModelAPI_Data> aData = data();
107 if (!aData->isValid())
110 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
111 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
112 aPoint1->move(theDeltaX, theDeltaY);
114 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
115 aData->attribute(SketchPlugin_Arc::START_ID()));
116 aPoint2->move(theDeltaX, theDeltaY);
118 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
119 aData->attribute(SketchPlugin_Arc::END_ID()));
120 aPoint3->move(theDeltaX, theDeltaY);
123 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
126 boost::shared_ptr<ModelAPI_Data> aData = data();
128 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
129 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
130 aDelta = aPoint1->pnt()->distance(thePoint);
132 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
133 aData->attribute(SketchPlugin_Arc::START_ID()));
134 double aDistance = aPoint2->pnt()->distance(thePoint);
135 if (aDelta < aDistance)
138 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
139 aData->attribute(SketchPlugin_Arc::END_ID()));
140 aDistance = aPoint3->pnt()->distance(thePoint);
141 if (aDelta < aDistance)
147 bool SketchPlugin_Arc::isFixed() {
148 return data()->selection(EXTERNAL_ID())->context();