1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Circle.cpp
4 // Created: 26 May 2014
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_Circle.h"
8 #include "SketchPlugin_Sketch.h"
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultConstruction.h>
11 #include <ModelAPI_AttributeSelection.h>
12 #include <ModelAPI_Validator.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeString.h>
15 #include <ModelAPI_Session.h>
17 #include <GeomAPI_Pnt2d.h>
18 #include <GeomAPI_Circ.h>
19 #include <GeomAPI_Circ2d.h>
20 #include <GeomAPI_XY.h>
21 #include <GeomDataAPI_Point2D.h>
22 #include <GeomDataAPI_Dir.h>
23 #include <GeomAlgoAPI_PointBuilder.h>
24 #include <GeomAlgoAPI_EdgeBuilder.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
29 const double tolerance = 1e-7;
32 static const std::string& POINT_ID(int theIndex)
35 case 1: return SketchPlugin_Circle::FIRST_POINT_ID();
36 case 2: return SketchPlugin_Circle::SECOND_POINT_ID();
37 case 3: return SketchPlugin_Circle::THIRD_POINT_ID();
40 static const std::string DUMMY;
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());
55 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
56 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
58 data()->addAttribute(CIRCLE_TYPE(), ModelAPI_AttributeString::typeId());
59 data()->addAttribute(FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
60 data()->addAttribute(SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
61 data()->addAttribute(THIRD_POINT_ID(), GeomDataAPI_Point2D::typeId());
62 std::dynamic_pointer_cast<ModelAPI_AttributeString>(
63 data()->attribute(CIRCLE_TYPE()))->setValue(CIRCLE_TYPE_CENTER_AND_RADIUS());
66 void SketchPlugin_Circle::execute()
68 SketchPlugin_Sketch* aSketch = sketch();
70 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
72 // compute a circle point in 3D view
73 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
74 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
75 AttributeDoublePtr aRadiusAttr =
76 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
77 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
78 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
79 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
80 // make a visible point
81 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
83 // make a visible circle
84 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
85 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
86 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
87 // compute the circle radius
88 double aRadius = aRadiusAttr->value();
90 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
91 aCenter, aNormal, aRadius);
92 aShapes.push_back(aCircleShape);
93 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
95 aConstr2->setShape(aCircleShape);
96 aConstr2->setIsInHistory(false);
97 setResult(aConstr2, 1);
102 AISObjectPtr SketchPlugin_Circle::getAISObject(AISObjectPtr thePrevious)
104 SketchPlugin_Sketch* aSketch = sketch();
105 if (aSketch && !isFeatureValid()) {
106 // compute a circle point in 3D view
107 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
108 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
109 AttributeDoublePtr aRadiusAttr =
110 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
111 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
112 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
114 // make a visible circle
115 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
116 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
117 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
119 double aRadius = aRadiusAttr->value();
120 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
121 aCenter, aNormal, aRadius);
122 if (aCircleShape && aRadius != 0) {
123 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
124 // make a visible point
125 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
126 aShapes.push_back(aCenterPointShape);
127 aShapes.push_back(aCircleShape);
129 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
130 AISObjectPtr anAIS = thePrevious;
132 anAIS = AISObjectPtr(new GeomAPI_AISObject);
133 anAIS->createShape(aCompound);
139 return AISObjectPtr();
142 bool SketchPlugin_Circle::isFeatureValid()
144 std::shared_ptr<GeomDataAPI_Point2D> aCenter =
145 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
146 bool aValid = aCenter->isInitialized();
148 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
149 data()->attribute(CIRCLE_TYPE()))->value();
150 if (aType == CIRCLE_TYPE_THREE_POINTS()) {
151 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
152 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
153 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
154 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
155 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
156 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
158 aFirstPnt->isInitialized() &&
159 aSecondPnt->isInitialized() &&
160 aThirdPnt->isInitialized();
165 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
167 std::shared_ptr<ModelAPI_Data> aData = data();
168 if (!aData->isValid())
171 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
172 aData->attribute(CENTER_ID()));
173 aPoint->move(theDeltaX, theDeltaY);
175 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
176 aPoint->move(theDeltaX, theDeltaY);
177 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
178 aPoint->move(theDeltaX, theDeltaY);
179 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
180 aPoint->move(theDeltaX, theDeltaY);
183 bool SketchPlugin_Circle::isFixed() {
184 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
187 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
188 // the second condition for unability to move external segments anywhere
189 if (theID == EXTERNAL_ID() || isFixed()) {
190 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
191 // update arguments due to the selection value
192 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
193 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
194 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
195 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
196 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
197 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
198 real(RADIUS_ID())->setValue(aCirc->radius());
201 else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
202 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
203 data()->attribute(CIRCLE_TYPE()))->value();
204 if (aType == CIRCLE_TYPE_THREE_POINTS() && lastResult()) // adjust data from the solver
206 } else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
207 // support the center and radius attributes enev in other mode: solver uses them
208 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
209 data()->attribute(CIRCLE_TYPE()))->value();
210 if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
212 data()->blockSendAttributeUpdated(true); // to modify two attributes at once
213 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
214 int aNbInitialized = 0;
215 for (int i = 1; i <= 3; ++i) {
216 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
217 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
218 if (aCurPnt->isInitialized())
219 aPoints[aNbInitialized++] = aCurPnt->pnt();
222 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
223 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
224 AttributeDoublePtr aRadiusAttr =
225 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
227 if (aNbInitialized == 1)
228 aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
229 else if (aNbInitialized == 2) {
230 std::shared_ptr<GeomAPI_XY> aCoord =
231 aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
232 double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
233 aCenterAttr->setValue(aCoord->x(), aCoord->y());
234 aRadiusAttr->setValue(aRadius);
236 std::shared_ptr<GeomAPI_Circ2d> aCircle(
237 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
239 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
241 double aRadius = aCircle->radius();
242 aCenterAttr->setValue(aCenter->x(), aCenter->y());
243 aRadiusAttr->setValue(aRadius);
246 data()->blockSendAttributeUpdated(false, false);
248 } else if (theID == CIRCLE_TYPE()) { // if switched to 3 points mode, adjust the needed attributes
249 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
250 data()->attribute(CIRCLE_TYPE()))->value();
251 if (aType == CIRCLE_TYPE_THREE_POINTS()) {
257 void SketchPlugin_Circle::adjustThreePoints()
259 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
260 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
261 if (!aCenterAttr->isInitialized())
263 AttributeDoublePtr aRadiusAttr =
264 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
265 if (!aRadiusAttr->isInitialized())
268 data()->blockSendAttributeUpdated(true);
269 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
270 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
271 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
272 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
273 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
274 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
275 double aRadius = aRadiusAttr->value();
277 if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
278 fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
279 fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
280 aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
281 aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
282 aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
284 data()->blockSendAttributeUpdated(false, false);