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 <SketchPlugin_ConstraintCoincidence.h>
10 #include <SketchPlugin_ConstraintTangent.h>
12 #include <Events_Loop.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_ResultConstruction.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Session.h>
22 #include <ModelAPI_Tools.h>
24 #include <GeomAPI_Ax2.h>
25 #include <GeomAPI_Circ2d.h>
26 #include <GeomAPI_Circ.h>
27 #include <GeomAPI_Dir2d.h>
28 #include <GeomAPI_Dir.h>
29 #include <GeomAPI_Lin2d.h>
30 #include <GeomAPI_Lin.h>
31 #include <GeomAPI_Pnt2d.h>
32 #include <GeomAPI_Vertex.h>
33 #include <GeomAPI_XY.h>
34 #include <GeomDataAPI_Point2D.h>
35 #include <GeomDataAPI_Dir.h>
36 #include <GeomAlgoAPI_PointBuilder.h>
37 #include <GeomAlgoAPI_EdgeBuilder.h>
38 #include <GeomAlgoAPI_CompoundBuilder.h>
42 const double tolerance = 1e-7;
43 const double paramTolerance = 1.e-4;
44 const double PI = 3.141592653589793238463;
47 SketchPlugin_Arc::SketchPlugin_Arc()
48 : SketchPlugin_SketchEntity()
53 void SketchPlugin_Arc::initDerivedClassAttributes()
55 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
56 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
57 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
59 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
60 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
62 AttributeBooleanPtr isReversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
63 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
65 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
66 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
68 // set after all to avoid in attributeChanged reference to not existing attributes
69 if (!isReversed->isInitialized()) {
70 isReversed->setValue(false);
74 void SketchPlugin_Arc::execute()
76 SketchPlugin_Sketch* aSketch = sketch();
81 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
82 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
83 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
84 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
85 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
86 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
87 if(!aCenterAttr->isInitialized() || !aStartAttr->isInitialized() || !anEndAttr->isInitialized()) {
91 // Make a visible point.
92 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
94 // Make a visible arc.
95 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
96 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
97 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
98 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
99 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
100 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
102 GeomShapePtr anArcShape = boolean(REVERSED_ID())->value() ?
103 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
104 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
106 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
107 aResult->setShape(anArcShape);
108 aResult->setIsInHistory(false);
109 setResult(aResult, 1);
112 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
114 std::shared_ptr<ModelAPI_Data> aData = data();
115 if(!aData->isValid()) {
119 bool aWasBlocked = aData->blockSendAttributeUpdated(true);
121 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
122 attribute(CENTER_ID()));
123 if(aCenter->isInitialized()) {
124 aCenter->move(theDeltaX, theDeltaY);
127 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
128 attribute(START_ID()));
129 if(aStart->isInitialized()) {
130 aStart->move(theDeltaX, theDeltaY);
133 std::shared_ptr<GeomDataAPI_Point2D> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
134 attribute(END_ID()));
135 if(anEnd->isInitialized()) {
136 anEnd->move(theDeltaX, theDeltaY);
139 aData->blockSendAttributeUpdated(aWasBlocked);
142 bool SketchPlugin_Arc::isFixed()
144 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
147 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
149 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
150 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
151 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
152 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
153 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
154 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
156 // The second condition for unability to move external segments anywhere.
157 if(theID == EXTERNAL_ID() || isFixed()) {
158 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
160 // empty shape in selection shows that the shape is equal to context
161 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
163 aSelection = anExtRes->shape();
166 // update arguments due to the selection value
167 if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
168 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
169 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
171 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
172 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
173 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
174 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
175 data()->blockSendAttributeUpdated(aWasBlocked, false);
177 std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
178 std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
181 double anEndParam = 0.0;
182 aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
183 myParamBefore = anEndParam;
185 double aMidParam = anEndParam / 2.0;
186 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
187 aCircle2d->D0(aMidParam, aMidPnt2d);
188 std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
189 double aStartParam = 0.0;
190 aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
191 aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
192 aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
193 aWasBlocked = data()->blockSendAttributeUpdated(true);
194 if(aStartParam < aMidParam && aMidParam < anEndParam) {
199 data()->blockSendAttributeUpdated(aWasBlocked, false);
202 } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
203 if(!aCenterAttr->isInitialized()
204 || !aStartAttr->isInitialized()
205 || !anEndAttr->isInitialized()) {
208 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
209 std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
210 std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
211 double aRadius = aCenter->distance(aStart);
212 if (aRadius < tolerance)
214 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
216 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
217 // The Arc end point is projected
218 // on the circle formed by center and start points
219 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
220 if (aProjection && anEnd->distance(aProjection) > tolerance) {
221 anEndAttr->setValue(aProjection);
224 data()->blockSendAttributeUpdated(aWasBlocked, false);
226 double aParameterNew = 0.0;
227 if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
228 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
229 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
230 if(!boolean(REVERSED_ID())->value()) {
231 boolean(REVERSED_ID())->setValue(true);
233 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
234 if(boolean(REVERSED_ID())->value()) {
235 boolean(REVERSED_ID())->setValue(false);
238 data()->blockSendAttributeUpdated(aWasBlocked, false);
240 myParamBefore = aParameterNew;
245 if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
246 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
247 if(anEndAttr->isInitialized()) {
248 if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
251 GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
252 double aStartParam, anEndParam;
253 aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
254 aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
255 anAngle = (anEndParam - aStartParam) / PI * 180.0;
256 if(isReversed()) anAngle = 360.0 - anAngle;
261 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
262 real(RADIUS_ID())->setValue(aRadius);
263 real(ANGLE_ID())->setValue(anAngle);
264 data()->blockSendAttributeUpdated(aWasBlocked, false);
267 void SketchPlugin_Arc::setReversed(bool isReversed)
269 boolean(REVERSED_ID())->setValue(isReversed);
272 bool SketchPlugin_Arc::isReversed()
274 return boolean(REVERSED_ID())->value();