1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_Arc.h"
22 #include "SketchPlugin_Sketch.h"
23 #include <SketchPlugin_ConstraintCoincidence.h>
24 #include <SketchPlugin_ConstraintTangent.h>
26 #include <Events_Loop.h>
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_AttributeString.h>
33 #include <ModelAPI_Events.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Session.h>
36 #include <ModelAPI_Tools.h>
38 #include <GeomAPI_Ax2.h>
39 #include <GeomAPI_Circ2d.h>
40 #include <GeomAPI_Circ.h>
41 #include <GeomAPI_Dir2d.h>
42 #include <GeomAPI_Dir.h>
43 #include <GeomAPI_Lin2d.h>
44 #include <GeomAPI_Lin.h>
45 #include <GeomAPI_Pnt2d.h>
46 #include <GeomAPI_Vertex.h>
47 #include <GeomAPI_XY.h>
48 #include <GeomDataAPI_Point2D.h>
49 #include <GeomDataAPI_Dir.h>
50 #include <GeomAlgoAPI_PointBuilder.h>
51 #include <GeomAlgoAPI_EdgeBuilder.h>
52 #include <GeomAlgoAPI_CompoundBuilder.h>
56 const double tolerance = 1e-7;
57 const double paramTolerance = 1.e-4;
58 const double PI = 3.141592653589793238463;
61 SketchPlugin_Arc::SketchPlugin_Arc()
62 : SketchPlugin_SketchEntity()
67 void SketchPlugin_Arc::initDerivedClassAttributes()
69 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
70 data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
71 data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
73 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
76 AttributeBooleanPtr isReversed = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
77 data()->addAttribute(REVERSED_ID(), ModelAPI_AttributeBoolean::typeId()));
79 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
80 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
82 // set after all to avoid in attributeChanged reference to not existing attributes
83 if (!isReversed->isInitialized()) {
84 isReversed->setValue(false);
88 void SketchPlugin_Arc::execute()
90 SketchPlugin_Sketch* aSketch = sketch();
95 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
96 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
97 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
98 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(START_ID()));
99 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
100 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(END_ID()));
101 if(!aCenterAttr->isInitialized() || !aStartAttr->isInitialized() || !anEndAttr->isInitialized()) {
105 // Make a visible point.
106 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
108 // Make a visible arc.
109 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
110 std::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
111 std::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
112 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
113 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
114 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
116 if (myParamBefore == 0) { // parameter has not been calculate yet
117 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(
118 new GeomAPI_Circ2d(aCenterAttr->pnt(), aStartAttr->pnt()));
119 aCircleForArc->parameter(anEndAttr->pnt(), paramTolerance, myParamBefore);
122 bool isReversed = boolean(REVERSED_ID())->value();
124 GeomEdgePtr anArcShape;
125 if (fabs(myParamBefore - 2.0 * PI) < paramTolerance) {
126 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aStart->distance(aCenter));
129 anArcShape = isReversed ?
130 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
131 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
134 // calculate tolerances for start and end points of the arc and set them to the result shape
135 // (this is done to fix gaps which appear because of inaccurate computation of arcs in PlaneGCS,
136 // which leads to difference in SketchPlugin_Arc attributes and boundary points of result shape)
138 for (int ind = 0; ind < 2; ++ind) {
139 bool isFirst = ind == 0;
140 GeomPointPtr anArcBndPoint = isFirst == isReversed ? anEnd : aStart;
141 GeomPointPtr aShapePoint = isFirst ? anArcShape->firstPoint() : anArcShape->lastPoint();
142 double aDistance = anArcBndPoint->distance(aShapePoint);
143 // avoid setting too high tolerance because it may be caused by incomplete update of an arc
144 if (aDistance > tolerance && aDistance < 100. * tolerance) {
146 anArcShape->setFirstPointTolerance(aDistance);
148 anArcShape->setLastPointTolerance(aDistance);
153 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
154 aResult->setShape(anArcShape);
155 aResult->setIsInHistory(false);
156 setResult(aResult, 1);
159 bool SketchPlugin_Arc::isFixed()
161 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
164 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
166 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
167 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
168 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
169 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
170 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
171 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
173 // The second condition for unability to move external segments anywhere.
174 if(theID == EXTERNAL_ID() || isFixed()) {
175 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
177 // empty shape in selection shows that the shape is equal to context
178 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
180 aSelection = anExtRes->shape();
183 // update arguments due to the selection value
184 if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
185 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
186 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
188 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
189 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
190 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
191 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
192 data()->blockSendAttributeUpdated(aWasBlocked, false);
194 std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
195 std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
198 double anEndParam = 0.0;
199 aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
200 myParamBefore = anEndParam;
202 double aMidParam = anEndParam / 2.0;
203 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
204 aCircle2d->D0(aMidParam, aMidPnt2d);
205 std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
206 double aStartParam = 0.0;
207 aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
208 aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
209 aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
212 anEndParam -= aStartParam;
213 aMidParam -= aStartParam;
214 if (anEndParam < 0.0)
215 anEndParam += 2.0 * PI;
217 aMidParam += 2.0 * PI;
219 aWasBlocked = data()->blockSendAttributeUpdated(true);
220 if(aMidParam < anEndParam) {
225 data()->blockSendAttributeUpdated(aWasBlocked, false);
228 } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
229 if(!aCenterAttr->isInitialized()
230 || !aStartAttr->isInitialized()
231 || !anEndAttr->isInitialized()) {
234 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
235 std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
236 std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
237 double aRadius = aCenter->distance(aStart);
238 if (aRadius < tolerance)
240 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
242 // Do not recalculate REVERSED flag if the arc is not consistent
243 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
244 if (aProjection && anEnd->distance(aProjection) <= tolerance) {
245 double aParameterNew = 0.0;
246 if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
247 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
248 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
249 if(!boolean(REVERSED_ID())->value()) {
250 boolean(REVERSED_ID())->setValue(true);
252 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
253 if(boolean(REVERSED_ID())->value()) {
254 boolean(REVERSED_ID())->setValue(false);
257 data()->blockSendAttributeUpdated(aWasBlocked, false);
259 if (fabs(aParameterNew) < paramTolerance ||
260 fabs(aParameterNew - 2.0 * PI) < paramTolerance)
261 aParameterNew = 2.0 * PI;
262 myParamBefore = aParameterNew;
268 if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
269 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
270 if(anEndAttr->isInitialized()) {
271 if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
274 GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
275 double aStartParam, anEndParam;
276 aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
277 aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
278 anAngle = (anEndParam - aStartParam) / PI * 180.0;
279 if(isReversed()) anAngle = 360.0 - anAngle;
284 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
285 real(RADIUS_ID())->setValue(aRadius);
286 real(ANGLE_ID())->setValue(anAngle);
287 data()->blockSendAttributeUpdated(aWasBlocked, false);
290 void SketchPlugin_Arc::setReversed(bool isReversed)
292 boolean(REVERSED_ID())->setValue(isReversed);
295 bool SketchPlugin_Arc::isReversed()
297 return boolean(REVERSED_ID())->value();