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 SketchPlugin_Arc::SketchPlugin_Arc()
33 : SketchPlugin_SketchEntity()
35 myStartUpdate = false;
44 void SketchPlugin_Arc::initDerivedClassAttributes()
46 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
47 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
48 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
49 GeomDataAPI_Point2D>(data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId()));
50 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
53 data()->addAttribute(INVERSED_ID(), ModelAPI_AttributeBoolean::typeId());
54 AttributeBooleanPtr isInversed =
55 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
56 if (!isInversed->isInitialized())
57 isInversed->setValue(false);
59 // get the initial values
60 if (anEndAttr->isInitialized()) {
61 myXEndBefore = anEndAttr->x();
62 myYEndBefore = anEndAttr->y();
66 void SketchPlugin_Arc::execute()
68 SketchPlugin_Sketch* aSketch = sketch();
69 // result for the arc is set only when all obligatory attributes are initialized,
70 // otherwise AIS object is used to visualize the arc's preview
71 if (aSketch && isFeatureValid()) {
72 // compute a circle point in 3D view
73 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
74 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
75 // compute the arc start point
76 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
77 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
79 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
80 // make a visible point
81 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
82 std::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
84 aConstr1->setShape(aCenterPointShape);
85 aConstr1->setIsInHistory(false);
86 setResult(aConstr1, 0);
88 // make a visible circle
89 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
90 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
91 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
92 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
94 // compute and change the arc end point
95 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
96 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
97 /* must be automatically done in attributeChanged
98 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
99 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
100 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
101 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
102 anEndAttr->setValue(aProjection);
104 std::shared_ptr<GeomAPI_Pnt> aEndPoint(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
105 AttributeBooleanPtr isInversed =
106 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()));
108 std::shared_ptr<GeomAPI_Dir> anXDir(new GeomAPI_Dir(aStartPoint->xyz()->decreased(aCenter->xyz())));
109 std::shared_ptr<GeomAPI_Ax2> anAx2(new GeomAPI_Ax2(aCenter, aNormal, anXDir));
110 std::shared_ptr<GeomAPI_Circ> aCirc(new GeomAPI_Circ(anAx2, aCenter->distance(aStartPoint)));
111 double aParameterNew = 0.0;
112 if(aCirc->parameter(aEndPoint, paramTolerance, aParameterNew)) {
113 if(0 <= myParamBefore && myParamBefore <= PI / 2.0
114 && PI * 1.5 <= aParameterNew && aParameterNew <= PI * 2.0) {
115 isInversed->setValue(true);
116 } else if(PI * 1.5 <= myParamBefore && myParamBefore <= PI * 2.0
117 && 0 <= aParameterNew && aParameterNew <= PI / 2.0) {
118 isInversed->setValue(false);
121 myParamBefore = aParameterNew;
123 std::shared_ptr<GeomAPI_Shape> aCircleShape;
124 if(!isInversed->value()) {
125 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStartPoint, aEndPoint, aNormal);
127 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aEndPoint, aStartPoint, aNormal);
131 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
133 aConstr2->setShape(aCircleShape);
134 aConstr2->setIsInHistory(false);
135 setResult(aConstr2, 1);
140 AISObjectPtr SketchPlugin_Arc::getAISObject(AISObjectPtr thePrevious)
142 SketchPlugin_Sketch* aSketch = sketch();
144 // if the feature is valid, the execute() method should be performed, AIS object is empty
145 if (!isFeatureValid()) {
146 // compute a circle point in 3D view
147 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
148 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
149 if (aCenterAttr->isInitialized()) {
150 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
152 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
153 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
154 if (aStartAttr->isInitialized()) {
155 // make a visible circle
156 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
157 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
158 bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0);
160 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
161 std::shared_ptr<GeomAPI_Pnt> aStartPoint(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
162 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircleArc(
163 aCenter, aStartPoint, aStartPoint, aNormal);
165 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
166 // make a visible point
167 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
168 aShapes.push_back(aCenterPointShape);
170 aShapes.push_back(aCircleShape);
171 if (!aShapes.empty())
173 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
174 AISObjectPtr anAIS = thePrevious;
176 anAIS = AISObjectPtr(new GeomAPI_AISObject);
177 anAIS->createShape(aCompound);
187 return AISObjectPtr();
190 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
192 std::shared_ptr<ModelAPI_Data> aData = data();
193 if (!aData->isValid())
196 myStartUpdate = true;
198 std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
199 aData->attribute(SketchPlugin_Arc::START_ID()));
200 aPoint2->move(theDeltaX, theDeltaY);
202 std::shared_ptr<GeomDataAPI_Point2D> aPoint3 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
203 aData->attribute(SketchPlugin_Arc::END_ID()));
204 aPoint3->move(theDeltaX, theDeltaY);
205 myStartUpdate = false;
208 std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
209 aData->attribute(SketchPlugin_Arc::CENTER_ID()));
210 aPoint1->move(theDeltaX, theDeltaY);
213 bool SketchPlugin_Arc::isFixed() {
214 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
217 bool SketchPlugin_Arc::isFeatureValid()
219 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
220 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::CENTER_ID()));
221 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
222 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::START_ID()));
223 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
224 GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Arc::END_ID()));
226 return aCenterAttr->isInitialized() && aStartAttr->isInitialized() && anEndAttr->isInitialized();
229 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
231 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
232 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
233 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
234 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
235 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
236 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
237 // the second condition for unability to move external segments anywhere
238 if (theID == EXTERNAL_ID() || isFixed()) {
239 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
240 // update arguments due to the selection value
241 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
242 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
243 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
245 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
246 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
247 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
252 if (!aCenterAttr->isInitialized())
254 if (!aStartAttr->isInitialized())
256 if (!anEndAttr->isInitialized())
259 // update the points in accordance to the changed point changes
260 if (theID == END_ID() && !myEndUpdate) {
262 // compute and change the arc end point
263 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
264 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
265 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
266 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance) {
267 if (!isStable()) { // issue #855: trying to update only not-updated coordinate if it is possible
268 if (fabs(myXEndBefore - anEndAttr->x()) < 1.e-10) { // keep Y unchanged
269 double aVy = aCenterAttr->y() - anEndAttr->y();
270 double aVy2 = aVy * aVy;
271 double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
273 double aDX = sqrt(aR2 - aVy * aVy);
274 if (anEndAttr->x() > aCenterAttr->x())
275 aProjection->setX(aCenterAttr->x() + aDX);
277 aProjection->setX(aCenterAttr->x() - aDX);
278 aProjection->setY(anEndAttr->y());
280 } else if (fabs(myYEndBefore - anEndAttr->y()) < 1.e-10) { // keep X unchanged
281 double aVx = aCenterAttr->x() - anEndAttr->x();
282 double aVx2 = aVx * aVx;
283 double aR2 = aCircleForArc->radius() * aCircleForArc->radius();
285 double aDY = sqrt(aR2 - aVx * aVx);
286 if (anEndAttr->y() > aCenterAttr->y())
287 aProjection->setY(aCenterAttr->y() + aDY);
289 aProjection->setY(aCenterAttr->y() - aDY);
290 aProjection->setX(anEndAttr->x());
295 anEndAttr->setValue(aProjection);
297 myXEndBefore = anEndAttr->x();
298 myYEndBefore = anEndAttr->y();
300 } else if (theID == START_ID() && !myStartUpdate) {
301 myStartUpdate = true;
302 // compute and change the arc start point
303 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
304 new GeomAPI_Circ2d(aCenterAttr->pnt(), anEndAttr->pnt()));
305 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(aStartAttr->pnt());
306 if (aProjection && aStartAttr->pnt()->distance(aProjection) > tolerance)
307 aStartAttr->setValue(aProjection);
308 myStartUpdate = false;
309 } else if (theID == CENTER_ID() && !myEndUpdate) {
311 // compute and change the arc end point
312 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
313 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
314 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEndAttr->pnt());
315 if (aProjection && anEndAttr->pnt()->distance(aProjection) > tolerance)
316 anEndAttr->setValue(aProjection);
321 void SketchPlugin_Arc::setReversed(bool isReversed)
323 std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->setValue(isReversed);
327 bool SketchPlugin_Arc::isReversed()
329 return std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(attribute(INVERSED_ID()))->value();