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 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
147 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
148 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
149 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
150 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
151 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
153 return aCenter->isInitialized() && aFirstPnt->isInitialized() &&
154 aSecondPnt->isInitialized() && aThirdPnt->isInitialized();
157 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
159 std::shared_ptr<ModelAPI_Data> aData = data();
160 if (!aData->isValid())
163 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
164 aData->attribute(CENTER_ID()));
165 aPoint->move(theDeltaX, theDeltaY);
167 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
168 aPoint->move(theDeltaX, theDeltaY);
169 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
170 aPoint->move(theDeltaX, theDeltaY);
171 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
172 aPoint->move(theDeltaX, theDeltaY);
175 bool SketchPlugin_Circle::isFixed() {
176 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
179 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
180 // the second condition for unability to move external segments anywhere
181 if (theID == EXTERNAL_ID() || isFixed()) {
182 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
183 // update arguments due to the selection value
184 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
185 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
186 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
187 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
188 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
189 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
190 real(RADIUS_ID())->setValue(aCirc->radius());
193 else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
194 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
195 data()->attribute(CIRCLE_TYPE()))->value();
196 if (aType == CIRCLE_TYPE_THREE_POINTS() && lastResult()) // adjust data from the solver
198 } else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
199 // support the center and radius attributes enev in other mode: solver uses them
200 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
201 data()->attribute(CIRCLE_TYPE()))->value();
202 if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
204 data()->blockSendAttributeUpdated(true); // to modify two attributes at once
205 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
206 int aNbInitialized = 0;
207 for (int i = 1; i <= 3; ++i) {
208 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
209 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
210 if (aCurPnt->isInitialized())
211 aPoints[aNbInitialized++] = aCurPnt->pnt();
214 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
215 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
216 AttributeDoublePtr aRadiusAttr =
217 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
219 if (aNbInitialized == 1)
220 aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
221 else if (aNbInitialized == 2) {
222 std::shared_ptr<GeomAPI_XY> aCoord =
223 aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
224 double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
225 aCenterAttr->setValue(aCoord->x(), aCoord->y());
226 aRadiusAttr->setValue(aRadius);
228 std::shared_ptr<GeomAPI_Circ2d> aCircle(
229 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
231 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
233 double aRadius = aCircle->radius();
234 aCenterAttr->setValue(aCenter->x(), aCenter->y());
235 aRadiusAttr->setValue(aRadius);
238 data()->blockSendAttributeUpdated(false, false);
240 } else if (theID == CIRCLE_TYPE()) { // if switched to 3 points mode, adjust the needed attributes
241 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
242 data()->attribute(CIRCLE_TYPE()))->value();
243 if (aType == CIRCLE_TYPE_THREE_POINTS()) {
249 void SketchPlugin_Circle::adjustThreePoints()
251 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
252 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
253 if (!aCenterAttr->isInitialized())
255 AttributeDoublePtr aRadiusAttr =
256 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
257 if (!aRadiusAttr->isInitialized())
260 data()->blockSendAttributeUpdated(true);
261 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
262 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
263 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
264 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
265 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
266 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
267 double aRadius = aRadiusAttr->value();
269 if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
270 fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
271 fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
272 aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
273 aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
274 aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
276 data()->blockSendAttributeUpdated(false, false);