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& CIRCLE_TYPE_CENTER_AND_RADIUS()
34 static const std::string TYPE("CenterRadius");
37 static const std::string& CIRCLE_TYPE_THREE_POINTS()
39 static const std::string TYPE("ThreePoints");
43 static const std::string& FIRST_POINT_ID()
45 static const std::string FIRST_PNT("FirstPoint");
48 static const std::string& SECOND_POINT_ID()
50 static const std::string SECOND_PNT("SecondPoint");
53 static const std::string& THIRD_POINT_ID()
55 static const std::string THIRD_PNT("ThirdPoint");
58 static const std::string& POINT_ID(int theIndex)
61 case 1: return FIRST_POINT_ID();
62 case 2: return SECOND_POINT_ID();
63 case 3: return THIRD_POINT_ID();
66 static const std::string DUMMY;
72 SketchPlugin_Circle::SketchPlugin_Circle()
73 : SketchPlugin_SketchEntity()
77 void SketchPlugin_Circle::initDerivedClassAttributes()
79 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
80 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
81 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
82 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
84 data()->addAttribute(CIRCLE_TYPE(), ModelAPI_AttributeString::typeId());
85 data()->addAttribute(FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
86 data()->addAttribute(SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
87 data()->addAttribute(THIRD_POINT_ID(), GeomDataAPI_Point2D::typeId());
88 std::dynamic_pointer_cast<ModelAPI_AttributeString>(
89 data()->attribute(CIRCLE_TYPE()))->setValue(CIRCLE_TYPE_CENTER_AND_RADIUS());
92 void SketchPlugin_Circle::execute()
94 SketchPlugin_Sketch* aSketch = sketch();
96 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
98 // compute a circle point in 3D view
99 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
100 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
101 AttributeDoublePtr aRadiusAttr =
102 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
103 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
104 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
105 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
106 // make a visible point
107 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
109 // make a visible circle
110 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
111 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
112 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
113 // compute the circle radius
114 double aRadius = aRadiusAttr->value();
116 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
117 aCenter, aNormal, aRadius);
118 aShapes.push_back(aCircleShape);
119 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
121 aConstr2->setShape(aCircleShape);
122 aConstr2->setIsInHistory(false);
123 setResult(aConstr2, 1);
130 AISObjectPtr SketchPlugin_Circle::getAISObject(AISObjectPtr thePrevious)
132 SketchPlugin_Sketch* aSketch = sketch();
133 if (aSketch && !isFeatureValid()) {
134 // compute a circle point in 3D view
135 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
136 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
137 AttributeDoublePtr aRadiusAttr =
138 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
139 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
140 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
142 // make a visible circle
143 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
144 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
145 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
147 double aRadius = aRadiusAttr->value();
148 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
149 aCenter, aNormal, aRadius);
150 if (aCircleShape && aRadius != 0) {
151 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
152 // make a visible point
153 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
154 aShapes.push_back(aCenterPointShape);
155 aShapes.push_back(aCircleShape);
157 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
158 AISObjectPtr anAIS = thePrevious;
160 anAIS = AISObjectPtr(new GeomAPI_AISObject);
161 anAIS->createShape(aCompound);
167 return AISObjectPtr();
170 bool SketchPlugin_Circle::isFeatureValid()
172 std::shared_ptr<GeomDataAPI_Point2D> aCenter =
173 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
174 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
175 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
176 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
177 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
178 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
179 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
181 return aCenter->isInitialized() && aFirstPnt->isInitialized() &&
182 aSecondPnt->isInitialized() && aThirdPnt->isInitialized();
185 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
187 std::shared_ptr<ModelAPI_Data> aData = data();
188 if (!aData->isValid())
191 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
192 aData->attribute(CENTER_ID()));
193 aPoint->move(theDeltaX, theDeltaY);
195 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
196 aPoint->move(theDeltaX, theDeltaY);
197 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
198 aPoint->move(theDeltaX, theDeltaY);
199 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
200 aPoint->move(theDeltaX, theDeltaY);
203 bool SketchPlugin_Circle::isFixed() {
204 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
207 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
208 // the second condition for unability to move external segments anywhere
209 if (theID == EXTERNAL_ID() || isFixed()) {
210 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
211 // update arguments due to the selection value
212 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
213 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
214 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
215 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
216 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
217 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
218 real(RADIUS_ID())->setValue(aCirc->radius());
221 else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
222 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
223 data()->attribute(CIRCLE_TYPE()))->value();
224 if (aType == CIRCLE_TYPE_THREE_POINTS())
227 // check the execute() was called and the shape was built
233 else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
234 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
235 data()->attribute(CIRCLE_TYPE()))->value();
236 if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
239 data()->blockSendAttributeUpdated(true);
241 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
242 int aNbInitialized = 0;
243 for (int i = 1; i <= 3; ++i) {
244 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
245 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
246 if (aCurPnt->isInitialized())
247 aPoints[aNbInitialized++] = aCurPnt->pnt();
250 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
251 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
252 AttributeDoublePtr aRadiusAttr =
253 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
255 if (aNbInitialized == 1)
256 aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
257 else if (aNbInitialized == 2) {
258 std::shared_ptr<GeomAPI_XY> aCoord =
259 aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
260 double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
261 aCenterAttr->setValue(aCoord->x(), aCoord->y());
262 aRadiusAttr->setValue(aRadius);
264 std::shared_ptr<GeomAPI_Circ2d> aCircle(
265 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
267 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
269 double aRadius = aCircle->radius();
270 aCenterAttr->setValue(aCenter->x(), aCenter->y());
271 aRadiusAttr->setValue(aRadius);
275 data()->blockSendAttributeUpdated(false);
279 void SketchPlugin_Circle::adjustThreePoints()
281 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
282 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
283 if (!aCenterAttr->isInitialized())
285 AttributeDoublePtr aRadiusAttr =
286 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
287 if (!aRadiusAttr->isInitialized())
290 data()->blockSendAttributeUpdated(true);
291 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
292 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
293 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
294 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
295 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
296 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
297 double aRadius = aRadiusAttr->value();
299 if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
300 fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
301 fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
302 aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
303 aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
304 aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
306 data()->blockSendAttributeUpdated(false);