1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Arc.cpp
4 // Created: 26 Apr 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_Arc.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultConstruction.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_Validator.h>
13 #include <ModelAPI_Session.h>
15 #include <GeomAPI_Ax2.h>
16 #include <GeomAPI_Circ2d.h>
17 #include <GeomAPI_Circ.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomDataAPI_Point2D.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomAlgoAPI_PointBuilder.h>
22 #include <GeomAlgoAPI_EdgeBuilder.h>
23 #include <GeomAlgoAPI_CompoundBuilder.h>
27 const double tolerance = 1e-7;
28 const double paramTolerance = 1.e-4;
29 const double PI =3.141592653589793238463;
32 static const std::string& INVERSED_ID()
34 static const std::string MY_INVERSED_ID("InversedArc");
35 return MY_INVERSED_ID;
38 SketchPlugin_Arc::SketchPlugin_Arc()
39 : SketchPlugin_SketchEntity()
41 myStartUpdate = false;
50 void SketchPlugin_Arc::initAttributes()
52 SketchPlugin_SketchEntity::initAttributes();
54 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
55 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
56 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
57 GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
58 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
61 data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
63 // get the initial values
64 if (anEndAttr->isInitialized()) {
65 myXEndBefore = anEndAttr->x();
66 myYEndBefore = anEndAttr->y();
70 void SketchPlugin_Arc::execute()
72 SketchPlugin_Sketch* aSketch = sketch();
73 // result for the arc is set only when all obligatory attributes are initialized,
74 // otherwise AIS object is used to visualize the arc's preview
75 if (aSketch && isFeatureValid()) {
76 // compute a circle point in 3D view
77 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
78 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
79 // compute the arc start point
80 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
81 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
83 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
84 // make a visible point
85 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
86 std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
88 aConstr1->setShape(aCenterPointShape);
89 aConstr1->setIsInHistory(false);
90 setResult(aConstr1, 0);
92 // make a visible circle
93 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
94 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
95 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
96 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
98 // compute and change the arc end point
99 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
100 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
101 /* must be automatically done in attributeChanged
102 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
103 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
104 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
105 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
106 anEndAttr->setValue(aProjection);
108 std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
109 AttributeBooleanPtr isInversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
111 std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
112 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
113 std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
114 double aParameterNew = 0.0;
115 if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
116 if(0 < myParamBefore && myParamBefore <= PI / 2.0
117 && PI * 1.5 < aParameterNew && aParameterNew <= PI * 2.0) {
118 isInversed->setValue(true);
119 } else if(PI * 1.5 < myParamBefore && myParamBefore <= PI * 2.0
120 && 0 < aParameterNew && aParameterNew <= PI / 2.0) {
121 isInversed->setValue(false);
124 myParamBefore = aParameterNew;
126 std::shared_ptr<GeomAPI_Shape> aCircleShape;
127 if(!isInversed->value()) {
128 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
130 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
134 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
136 aConstr2->setShape(aCircleShape);
137 aConstr2->setIsInHistory(false);
138 setResult(aConstr2, 1);
143 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
145 SketchPlugin_Sketch* aSketch = sketch();
147 // if the feature is valid, the execute() method should be performed, AIS object is empty
148 if (!isFeatureValid()) {
149 // compute a circle point in 3D view
150 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
151 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
152 if (aCenterAttr->isInitialized()) {
153 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
155 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
156 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
157 if (aStartAttr->isInitialized()) {
158 // make a visible circle
159 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
160 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
161 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
163 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
164 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
165 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
166 aCenter, aStartPoint, aStartPoint, aNormal);
168 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
169 // make a visible point
170 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
171 aShapes.push_back(aCenterPointShape);
173 aShapes.push_back(aCircleShape);
174 if (!aShapes.empty())
176 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
177 AISObjectPtr anAIS = thePrevious;
179 anAIS = AISObjectPtr(new GeomAPI_AISObject);
180 anAIS->createShape(aCompound);
190 return AISObjectPtr();
193 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
195 std::shared_ptr<ModelAPI_Data> aData = data();
196 if (!aData->isValid())
199 myStartUpdate = true;
201 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
202 aData->attribute(SketchPlugin_Arc::START_ID()));
203 aPoint2->move(theDeltaX, theDeltaY);
205 std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
206 aData->attribute(SketchPlugin_Arc::END_ID()));
207 aPoint3->move(theDeltaX, theDeltaY);
208 myStartUpdate = false;
211 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
212 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
213 aPoint1->move(theDeltaX, theDeltaY);
216 bool SketchPlugin_Arc::isFixed() {
217 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
220 bool SketchPlugin_Arc::isFeatureValid()
222 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
223 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
224 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
225 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
226 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
227 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
229 return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
232 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
234 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
235 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
236 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
237 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
238 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
239 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
240 // the second condition for unability to move external segments anywhere
241 if (theID == EXTERNAL_ID() || isFixed()) {
242 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
243 // update arguments due to the selection value
244 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
245 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
246 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
248 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
249 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
250 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
255 if (!aCenterAttr->isInitialized())
257 if (!aStartAttr->isInitialized())
259 if (!anEndAttr->isInitialized())
262 // update the points in accordance to the changed point changes
263 if (theID == END_ID() && !myEndUpdate) {
265 // compute and change the arc end point
266 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
267 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
268 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
269 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance) {
270 // issue #855: trying to update only not-updated coordinate if it is possible
272 if (abs(myXEndBefore - anEndAttr->x()) < 1.e-10) { // keep Y unchanged
273 double aVy = aCenterAttr->y() - anEndAttr->y();
274 double aVy2 = aVy * aVy;
275 double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
277 double aDX = sqrt(aR2 - aVy * aVy);
278 if (anEndAttr->x() > aCenterAttr->x())
279 aProjection->setX(aCenterAttr->x() + aDX);
281 aProjection->setX(aCenterAttr->x() - aDX);
282 aProjection->setY(anEndAttr->y());
284 } else if (abs(myYEndBefore - anEndAttr->y()) < 1.e-10) { // keep X unchanged
285 double aVx = aCenterAttr->x() - anEndAttr->x();
286 double aVx2 = aVx * aVx;
287 double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
289 double aDY = sqrt(aR2 - aVx * aVx);
290 if (anEndAttr->y() > aCenterAttr->y())
291 aProjection->setY(aCenterAttr->y() + aDY);
293 aProjection->setY(aCenterAttr->y() - aDY);
294 aProjection->setX(anEndAttr->x());
298 anEndAttr->setValue(aProjection);
300 myXEndBefore = anEndAttr->x();
301 myYEndBefore = anEndAttr->y();
303 } else if (theID == START_ID() && !myStartUpdate) {
304 myStartUpdate = true;
305 // compute and change the arc start point
306 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
307 new GeomAPI_Circ2d(aCenterAttr->pnt(), anEndAttr->pnt()));
308 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aStartAttr->pnt());
309 if (aProjection && aStartAttr->pnt()->distance(aProjection) > tolerance)
310 aStartAttr->setValue(aProjection);
311 myStartUpdate = false;
312 } else if (theID == CENTER_ID() && !myEndUpdate) {
314 // compute and change the arc end point
315 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
316 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
317 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
318 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
319 anEndAttr->setValue(aProjection);