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_Circle.h"
21 #include "SketchPlugin_Sketch.h"
22 #include "SketchPlugin_Tools.h"
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeReference.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_AttributeString.h>
31 #include <ModelAPI_Session.h>
32 #include <SketchPlugin_Point.h>
33 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_Circ.h>
37 #include <GeomAPI_Dir2d.h>
38 #include <GeomAPI_Circ2d.h>
39 #include <GeomAPI_Vertex.h>
40 #include <GeomAPI_XY.h>
41 #include <GeomDataAPI_Point2D.h>
42 #include <GeomDataAPI_Dir.h>
43 #include <GeomAlgoAPI_PointBuilder.h>
44 #include <GeomAlgoAPI_EdgeBuilder.h>
45 #include <GeomAlgoAPI_CompoundBuilder.h>
49 const double tolerance = 1e-7;
50 const double PI = 3.141592653589793238463;
53 SketchPlugin_Circle::SketchPlugin_Circle()
54 : SketchPlugin_SketchEntity()
58 void SketchPlugin_Circle::initDerivedClassAttributes()
60 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
61 data()->addAttribute(ROTATE_ID(), GeomDataAPI_Point2D::typeId());
62 data()->addAttribute(ROTATE_REF_ID(), ModelAPI_AttributeReference::typeId());
63 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
64 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
66 // While exist addCircle without creating point.
67 AttributeIntegerPtr aVerAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(
68 data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId()));
69 aVerAttr->setIsArgument(false);
70 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
71 if (!aVerAttr->isInitialized()) {
72 // this is a newly created feature (not read from file),
73 // so, initialize the latest version
74 aVerAttr->setValue(SketchPlugin_Circle::THE_VERSION_1);
77 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
78 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
79 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ROTATE_ID());
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ROTATE_REF_ID());
81 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), ANGLE_ID());
84 void SketchPlugin_Circle::execute()
86 SketchPlugin_Sketch* aSketch = sketch();
91 // Compute a circle in 3D view.
92 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
93 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
94 AttributeDoublePtr aRadiusAttr = real(RADIUS_ID());
95 AttributeDoublePtr anAngleAttr = real(ANGLE_ID());
96 if(!aCenterAttr->isInitialized() || !aRadiusAttr->isInitialized()) {
100 double aRadius = aRadiusAttr->value();
101 if(aRadius < tolerance) {
105 if (!anAngleAttr->isInitialized())
106 anAngleAttr->setValue(0);
108 // Make a visible center point.
109 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
111 // Make a visible circle.
112 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
113 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
114 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
115 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
117 std::shared_ptr<GeomAPI_Edge> aCircleShape;
118 if (integer(VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0)
120 double aValAn = anAngleAttr->value();
121 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius, aValAn / 180 * PI);
123 // Update corrdinates for point on circle
124 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
125 GeomPnt2dPtr aCircleSewPoint = aSketch->to2D(aCircleShape->firstPoint());
126 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(ROTATE_ID()))
127 ->setValue(aCircleSewPoint->x(), aCircleSewPoint->y());
128 data()->blockSendAttributeUpdated(aWasBlocked, false);
131 aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
133 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
134 aResult->setShape(aCircleShape);
135 aResult->setIsInHistory(false);
136 setResult(aResult, 1);
139 bool SketchPlugin_Circle::isFixed() {
140 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
143 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
144 // the second condition for unability to move external segments anywhere
145 if (theID == EXTERNAL_ID() || isFixed()) {
146 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
148 // empty shape in selection shows that the shape is equal to context
149 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
151 aSelection = anExtRes->shape();
153 // update arguments due to the selection value
154 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
155 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
156 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
157 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
158 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
159 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
161 real(RADIUS_ID())->setValue(aCirc->radius());
162 real(ANGLE_ID())->setValue(0);
165 else if (integer(VERSION_ID())->value() > SketchPlugin_Circle::THE_VERSION_0 &&
166 theID == ROTATE_ID())
168 // get cricle from results
169 std::shared_ptr<GeomAPI_Shape> aSelection;
170 ResultPtr aCircleRes = lastResult();
172 aSelection = aCircleRes->shape();
173 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
174 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelection));
175 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
177 // Get point and project it on line
178 std::shared_ptr<GeomDataAPI_Point2D> aNewPntRot =
179 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(ROTATE_ID()));
180 GeomPointPtr aPntRot3D = aCirc->project( sketch()->to3D(aNewPntRot->x(), aNewPntRot->y()));
183 GeomPointPtr aNorm = sketch()->to3D(real(RADIUS_ID())->value(), 0);
184 double aStartParam, anEndParam;
185 aCirc->parameter(aPntRot3D, 1.e-4, anEndParam);
186 aCirc->parameter(aNorm, 1.e-4, aStartParam);
188 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
189 real(ANGLE_ID())->setValue((anEndParam - aStartParam) / PI * 180.0);
190 data()->blockSendAttributeUpdated(aWasBlocked, false);