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();
40 // result for the arc is set only when all obligatory attributes are initialized,
41 // otherwise AIS object is used to visualize the arc's preview
42 if (aSketch && isFeatureValid()) {
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()));
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 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 boost::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
71 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
72 boost::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
73 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
74 anEndAttr->setValue(aProjection);
75 boost::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
77 boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
78 aCenter, aStartPoint, aEndPoint, aNormal);
80 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
82 aConstr2->setShape(aCircleShape);
83 aConstr2->setIsInHistory(false);
84 setResult(aConstr2, 1);
90 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
92 SketchPlugin_Sketch* aSketch = sketch();
94 // if the feature is valid, the execute() method should be performed, AIS object is empty
95 if (!isFeatureValid()) {
96 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
98 // compute a circle point in 3D view
99 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
100 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
101 if (aCenterAttr->isInitialized()) {
102 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
103 // make a visible point
104 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
105 aShapes.push_back(aCenterPointShape);
107 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
108 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
109 if (aStartAttr->isInitialized()) {
110 // make a visible circle
111 boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
112 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
113 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
115 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
116 boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
117 boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
118 aCenter, aStartPoint, aStartPoint, aNormal);
120 aShapes.push_back(aCircleShape);
125 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
126 AISObjectPtr anAIS = thePrevious;
128 anAIS = AISObjectPtr(new GeomAPI_AISObject);
129 anAIS->createShape(aCompound);
133 return AISObjectPtr();
136 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
138 boost::shared_ptr<ModelAPI_Data> aData = data();
139 if (!aData->isValid())
142 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
143 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
144 aPoint1->move(theDeltaX, theDeltaY);
146 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
147 aData->attribute(SketchPlugin_Arc::START_ID()));
148 aPoint2->move(theDeltaX, theDeltaY);
150 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
151 aData->attribute(SketchPlugin_Arc::END_ID()));
152 aPoint3->move(theDeltaX, theDeltaY);
155 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
158 boost::shared_ptr<ModelAPI_Data> aData = data();
160 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
161 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
162 aDelta = aPoint1->pnt()->distance(thePoint);
164 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
165 aData->attribute(SketchPlugin_Arc::START_ID()));
166 double aDistance = aPoint2->pnt()->distance(thePoint);
167 if (aDelta < aDistance)
170 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
171 aData->attribute(SketchPlugin_Arc::END_ID()));
172 aDistance = aPoint3->pnt()->distance(thePoint);
173 if (aDelta < aDistance)
179 bool SketchPlugin_Arc::isFixed() {
180 return data()->selection(EXTERNAL_ID())->context();
183 bool SketchPlugin_Arc::isFeatureValid()
185 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
186 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
187 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
188 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
189 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
190 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
192 return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();