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 // compute a circle point in 3D view
97 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
98 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
99 if (aCenterAttr->isInitialized()) {
100 boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
102 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
103 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
104 if (aStartAttr->isInitialized()) {
105 // make a visible circle
106 boost::shared_ptr<GeomDataAPI_Dir> aNDir = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
107 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
108 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
110 boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
111 boost::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
112 boost::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
113 aCenter, aStartPoint, aStartPoint, aNormal);
115 std::list<boost::shared_ptr<GeomAPI_Shape> > aShapes;
116 // make a visible point
117 boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
118 aShapes.push_back(aCenterPointShape);
120 aShapes.push_back(aCircleShape);
121 if (!aShapes.empty())
123 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
124 AISObjectPtr anAIS = thePrevious;
126 anAIS = AISObjectPtr(new GeomAPI_AISObject);
127 anAIS->createShape(aCompound);
136 return AISObjectPtr();
139 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
141 boost::shared_ptr<ModelAPI_Data> aData = data();
142 if (!aData->isValid())
145 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
147 aPoint1->move(theDeltaX, theDeltaY);
149 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
150 aData->attribute(SketchPlugin_Arc::START_ID()));
151 aPoint2->move(theDeltaX, theDeltaY);
153 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154 aData->attribute(SketchPlugin_Arc::END_ID()));
155 aPoint3->move(theDeltaX, theDeltaY);
158 double SketchPlugin_Arc::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
161 boost::shared_ptr<ModelAPI_Data> aData = data();
163 boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
165 aDelta = aPoint1->pnt()->distance(thePoint);
167 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
168 aData->attribute(SketchPlugin_Arc::START_ID()));
169 double aDistance = aPoint2->pnt()->distance(thePoint);
170 if (aDelta < aDistance)
173 boost::shared_ptr<GeomDataAPI_Point2D> aPoint3 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
174 aData->attribute(SketchPlugin_Arc::END_ID()));
175 aDistance = aPoint3->pnt()->distance(thePoint);
176 if (aDelta < aDistance)
182 bool SketchPlugin_Arc::isFixed() {
183 return data()->selection(EXTERNAL_ID())->context();
186 bool SketchPlugin_Arc::isFeatureValid()
188 boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
189 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
190 boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
191 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
192 boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
193 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
195 return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();