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 GeomShapePtr anArcShape;
123 if (fabs(myParamBefore - 2.0 * PI) < paramTolerance) {
124 anArcShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aStart->distance(aCenter));
127 anArcShape = boolean(REVERSED_ID())->value() ?
128 GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, anEnd, aStart, aNormal)
129 : GeomAlgoAPI_EdgeBuilder::lineCircleArc(aCenter, aStart, anEnd, aNormal);
132 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
133 aResult->setShape(anArcShape);
134 aResult->setIsInHistory(false);
135 setResult(aResult, 1);
138 void SketchPlugin_Arc::move(double theDeltaX, double theDeltaY)
140 std::shared_ptr<ModelAPI_Data> aData = data();
141 if(!aData->isValid()) {
145 bool aWasBlocked = aData->blockSendAttributeUpdated(true);
147 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
148 attribute(CENTER_ID()));
149 if(aCenter->isInitialized()) {
150 aCenter->move(theDeltaX, theDeltaY);
153 std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154 attribute(START_ID()));
155 if(aStart->isInitialized()) {
156 aStart->move(theDeltaX, theDeltaY);
159 std::shared_ptr<GeomDataAPI_Point2D> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
160 attribute(END_ID()));
161 if(anEnd->isInitialized()) {
162 anEnd->move(theDeltaX, theDeltaY);
165 aData->blockSendAttributeUpdated(aWasBlocked);
168 bool SketchPlugin_Arc::isFixed()
170 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
173 void SketchPlugin_Arc::attributeChanged(const std::string& theID)
175 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
176 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
177 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
178 GeomDataAPI_Point2D>(data()->attribute(START_ID()));
179 std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
180 GeomDataAPI_Point2D>(data()->attribute(END_ID()));
182 // The second condition for unability to move external segments anywhere.
183 if(theID == EXTERNAL_ID() || isFixed()) {
184 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
186 // empty shape in selection shows that the shape is equal to context
187 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
189 aSelection = anExtRes->shape();
192 // update arguments due to the selection value
193 if(aSelection && !aSelection->isNull() && aSelection->isEdge()) {
194 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
195 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
197 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
198 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
199 aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
200 anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
201 data()->blockSendAttributeUpdated(aWasBlocked, false);
203 std::shared_ptr<GeomAPI_Circ2d> aCircle2d =
204 std::shared_ptr<GeomAPI_Circ2d>(new GeomAPI_Circ2d(aCenterAttr->pnt(),
207 double anEndParam = 0.0;
208 aCircle2d->parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
209 myParamBefore = anEndParam;
211 double aMidParam = anEndParam / 2.0;
212 std::shared_ptr<GeomAPI_Pnt2d> aMidPnt2d;
213 aCircle2d->D0(aMidParam, aMidPnt2d);
214 std::shared_ptr<GeomAPI_Pnt> aMinPnt = sketch()->to3D(aMidPnt2d->x(), aMidPnt2d->y());
215 double aStartParam = 0.0;
216 aCirc->parameter(anEdge->firstPoint(), paramTolerance, aStartParam);
217 aCirc->parameter(aMinPnt, paramTolerance, aMidParam);
218 aCirc->parameter(anEdge->lastPoint(), paramTolerance, anEndParam);
221 anEndParam -= aStartParam;
222 aMidParam -= aStartParam;
223 if (anEndParam < 0.0)
224 anEndParam += 2.0 * PI;
226 aMidParam += 2.0 * PI;
228 aWasBlocked = data()->blockSendAttributeUpdated(true);
229 if(aMidParam < anEndParam) {
234 data()->blockSendAttributeUpdated(aWasBlocked, false);
237 } else if(theID == CENTER_ID() || theID == START_ID() || theID == END_ID()) {
238 if(!aCenterAttr->isInitialized()
239 || !aStartAttr->isInitialized()
240 || !anEndAttr->isInitialized()) {
243 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
244 std::shared_ptr<GeomAPI_Pnt2d> aStart = aStartAttr->pnt();
245 std::shared_ptr<GeomAPI_Pnt2d> anEnd = anEndAttr->pnt();
246 double aRadius = aCenter->distance(aStart);
247 if (aRadius < tolerance)
249 std::shared_ptr<GeomAPI_Circ2d> aCircleForArc(new GeomAPI_Circ2d(aCenter, aStart));
251 // Do not recalculate REVERSED flag if the arc is not consistent
252 std::shared_ptr<GeomAPI_Pnt2d> aProjection = aCircleForArc->project(anEnd);
253 if (aProjection && anEnd->distance(aProjection) <= tolerance) {
254 double aParameterNew = 0.0;
255 if(aCircleForArc->parameter(anEnd, paramTolerance, aParameterNew)) {
256 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
257 if(myParamBefore <= PI / 2.0 && aParameterNew >= PI * 1.5) {
258 if(!boolean(REVERSED_ID())->value()) {
259 boolean(REVERSED_ID())->setValue(true);
261 } else if(myParamBefore >= PI * 1.5 && aParameterNew <= PI / 2.0) {
262 if(boolean(REVERSED_ID())->value()) {
263 boolean(REVERSED_ID())->setValue(false);
266 data()->blockSendAttributeUpdated(aWasBlocked, false);
268 if (fabs(aParameterNew) < paramTolerance ||
269 fabs(aParameterNew - 2.0 * PI) < paramTolerance)
270 aParameterNew = 2.0 * PI;
271 myParamBefore = aParameterNew;
277 if(aCenterAttr->isInitialized() && aStartAttr->isInitialized()) {
278 aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
279 if(anEndAttr->isInitialized()) {
280 if(aStartAttr->pnt()->isEqual(anEndAttr->pnt())) {
283 GeomAPI_Circ2d aCircleForArc(aCenterAttr->pnt(), aStartAttr->pnt());
284 double aStartParam, anEndParam;
285 aCircleForArc.parameter(aStartAttr->pnt(), paramTolerance, aStartParam);
286 aCircleForArc.parameter(anEndAttr->pnt(), paramTolerance, anEndParam);
287 anAngle = (anEndParam - aStartParam) / PI * 180.0;
288 if(isReversed()) anAngle = 360.0 - anAngle;
293 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
294 real(RADIUS_ID())->setValue(aRadius);
295 real(ANGLE_ID())->setValue(anAngle);
296 data()->blockSendAttributeUpdated(aWasBlocked, false);
299 void SketchPlugin_Arc::setReversed(bool isReversed)
301 boolean(REVERSED_ID())->setValue(isReversed);
304 bool SketchPlugin_Arc::isReversed()
306 return boolean(REVERSED_ID())->value();