1 // Copyright (C) 2014-2023 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketchPlugin_Arc.h"
21 #include "SketchPlugin_Sketch.h"
22 #include <SketchPlugin_ConstraintCoincidence.h>
23 #include <SketchPlugin_ConstraintTangent.h>
25 #include <Events_Loop.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeRefAttr.h>
30 #include <ModelAPI_AttributeSelection.h>
31 #include <ModelAPI_AttributeString.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
37 #include <GeomAPI_Ax2.h>
38 #include <GeomAPI_Circ2d.h>
39 #include <GeomAPI_Circ.h>
40 #include <GeomAPI_Dir2d.h>
41 #include <GeomAPI_Dir.h>
42 #include <GeomAPI_Lin2d.h>
43 #include <GeomAPI_Lin.h>
44 #include <GeomAPI_Pnt2d.h>
45 #include <GeomAPI_Vertex.h>
46 #include <GeomAPI_XY.h>
47 #include <GeomDataAPI_Point2D.h>
48 #include <GeomDataAPI_Dir.h>
49 #include <GeomAlgoAPI_PointBuilder.h>
50 #include <GeomAlgoAPI_EdgeBuilder.h>
51 #include <GeomAlgoAPI_CompoundBuilder.h>
55 static const double tolerance = 1e-7;
56 static const double paramTolerance = 1.e-4;
57 static const double PI = 3.141592653589793238463;
60 SketchPlugin_Arc::SketchPlugin_Arc()
61 : SketchPlugin_SketchEntity()
66 void SketchPlugin_Arc::initDerivedClassAttributes()
68 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
69 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
70 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
72 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
75 AttributeBooleanPtr isReversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
76 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
78 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
79 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
81 // set after all to avoid in attributeChanged reference to not existing attributes
82 if (!isReversed->isInitialized()) {
83 isReversed->setValue(false);
87 void SketchPlugin_Arc::execute()
89 SketchPlugin_Sketch* aSketch = sketch();
94 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
95 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
96 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
97 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
98 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
99 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
100 if(!aCenterAttr->isInitialized() || !aStartAttr->isInitialized() || !anEndAttr->isInitialized()) {
104 // Make a visible point.
105 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
107 // Make a visible arc.
108 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
109 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
110 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
111 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
112 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
113 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
115 if (myParamBefore == 0) { // parameter has not been calculate yet
116 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
117 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
118 aCircleForArc->parameter(anEndAttr->pnt(), paramTolerance, myParamBefore);
121 bool isReversed = boolean(REVERSED_ID())->value();
123 GeomEdgePtr anArcShape;
124 if (fabs(myParamBefore - 2.0 * PI) < paramTolerance) {
125 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aStart->distance(aCenter));
128 anArcShape = isReversed ?
129 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
130 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
133 // calculate tolerances for start and end points of the arc and set them to the result shape
134 // (this is done to fix gaps which appear because of inaccurate computation of arcs in PlaneGCS,
135 // which leads to difference in SketchPlugin_Arc attributes and boundary points of result shape)
137 for (int ind = 0; ind < 2; ++ind) {
138 bool isFirst = ind == 0;
139 GeomPointPtr anArcBndPoint = isFirst == isReversed ? anEnd : aStart;
140 GeomPointPtr aShapePoint = isFirst ? anArcShape->firstPoint() : anArcShape->lastPoint();
141 double aDistance = anArcBndPoint->distance(aShapePoint);
142 // avoid setting too high tolerance because it may be caused by incomplete update of an arc
143 if (aDistance > tolerance && aDistance < 100. * tolerance) {
145 anArcShape->setFirstPointTolerance(aDistance);
147 anArcShape->setLastPointTolerance(aDistance);
152 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
153 aResult->setShape(anArcShape);
154 aResult->setIsInHistory(false);
155 setResult(aResult, 1);
158 bool SketchPlugin_Arc::isFixed()
160 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
163 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
165 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
166 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
167 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
168 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
169 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
170 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
172 // The second condition for unability to move external segments anywhere.
173 if(theID == EXTERNAL_ID() || isFixed()) {
174 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
176 // empty shape in selection shows that the shape is equal to context
177 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
179 aSelection = anExtRes->shape();
182 // update arguments due to the selection value
183 if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
184 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
185 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
187 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
188 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
189 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
190 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
191 data()->blockSendAttributeUpdated(aWasBlocked, false);
193 std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
194 std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
197 double anEndParam = 0.0;
198 aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
199 myParamBefore = anEndParam;
201 double aMidParam = anEndParam / 2.0;
202 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
203 aCircle2d->D0(aMidParam, aMidPnt2d);
204 std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
205 double aStartParam = 0.0;
206 aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
207 aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
208 aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
211 anEndParam -= aStartParam;
212 aMidParam -= aStartParam;
213 if (anEndParam < 0.0)
214 anEndParam += 2.0 * PI;
216 aMidParam += 2.0 * PI;
218 aWasBlocked = data()->blockSendAttributeUpdated(true);
219 if(aMidParam < anEndParam) {
224 data()->blockSendAttributeUpdated(aWasBlocked, false);
227 } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
228 if(!aCenterAttr->isInitialized()
229 || !aStartAttr->isInitialized()
230 || !anEndAttr->isInitialized()) {
233 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
234 std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
235 std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
236 double aRadius = aCenter->distance(aStart);
237 if (aRadius < tolerance)
239 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
241 // Do not recalculate REVERSED flag if the arc is not consistent
242 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
243 if (aProjection && anEnd->distance(aProjection) <= tolerance) {
244 double aParameterNew = 0.0;
245 if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
246 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
247 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
248 if(!boolean(REVERSED_ID())->value()) {
249 boolean(REVERSED_ID())->setValue(true);
251 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
252 if(boolean(REVERSED_ID())->value()) {
253 boolean(REVERSED_ID())->setValue(false);
256 data()->blockSendAttributeUpdated(aWasBlocked, false);
258 if (fabs(aParameterNew) < paramTolerance ||
259 fabs(aParameterNew - 2.0 * PI) < paramTolerance)
260 aParameterNew = 2.0 * PI;
261 myParamBefore = aParameterNew;
267 if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
268 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
269 if(anEndAttr->isInitialized()) {
270 if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
273 GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
274 double aStartParam, anEndParam;
275 aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
276 aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
277 anAngle = (anEndParam - aStartParam) / PI * 180.0;
278 if(isReversed()) anAngle = 360.0 - anAngle;
283 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
284 real(RADIUS_ID())->setValue(aRadius);
285 real(ANGLE_ID())->setValue(anAngle);
286 data()->blockSendAttributeUpdated(aWasBlocked, false);
289 void SketchPlugin_Arc::setReversed(bool isReversed)
291 boolean(REVERSED_ID())->setValue(isReversed);
294 bool SketchPlugin_Arc::isReversed()
296 return boolean(REVERSED_ID())->value();