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 <ModelAPI_Data.h>
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_AttributeSelection.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
30 #include <GeomAPI_Pnt2d.h>
31 #include <GeomAPI_Circ.h>
32 #include <GeomAPI_Circ2d.h>
33 #include <GeomAPI_Vertex.h>
34 #include <GeomAPI_XY.h>
35 #include <GeomDataAPI_Point2D.h>
36 #include <GeomDataAPI_Dir.h>
37 #include <GeomAlgoAPI_PointBuilder.h>
38 #include <GeomAlgoAPI_EdgeBuilder.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
43 const double tolerance = 1e-7;
46 SketchPlugin_Circle::SketchPlugin_Circle()
47 : SketchPlugin_SketchEntity()
51 void SketchPlugin_Circle::initDerivedClassAttributes()
53 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
54 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
60 void SketchPlugin_Circle::execute()
62 SketchPlugin_Sketch* aSketch = sketch();
67 // Compute a circle in 3D view.
68 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
69 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
70 AttributeDoublePtr aRadiusAttr = real(RADIUS_ID());
71 if(!aCenterAttr->isInitialized() || !aRadiusAttr->isInitialized()) {
75 double aRadius = aRadiusAttr->value();
76 if(aRadius < tolerance) {
80 // Make a visible point.
81 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
83 // Make a visible circle.
84 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
85 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
86 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
87 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
89 std::shared_ptr<GeomAPI_Shape> aCircleShape =
90 GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius);
92 std::shared_ptr<ModelAPI_ResultConstruction> aResult = document()->createConstruction(data(), 1);
93 aResult->setShape(aCircleShape);
94 aResult->setIsInHistory(false);
95 setResult(aResult, 1);
98 bool SketchPlugin_Circle::isFixed() {
99 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
102 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
103 // the second condition for unability to move external segments anywhere
104 if (theID == EXTERNAL_ID() || isFixed()) {
105 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
107 // empty shape in selection shows that the shape is equal to context
108 ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
110 aSelection = anExtRes->shape();
112 // update arguments due to the selection value
113 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
114 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
115 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
116 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
117 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
118 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
119 real(RADIUS_ID())->setValue(aCirc->radius());