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>
27 const double tolerance = 1e-7;
30 static const std::string& CIRCLE_TYPE_CENTER_AND_RADIUS()
32 static const std::string TYPE("CenterRadius");
35 static const std::string& CIRCLE_TYPE_THREE_POINTS()
37 static const std::string TYPE("ThreePoints");
41 static const std::string& FIRST_POINT_ID()
43 static const std::string FIRST_PNT("FirstPoint");
46 static const std::string& SECOND_POINT_ID()
48 static const std::string SECOND_PNT("SecondPoint");
51 static const std::string& THIRD_POINT_ID()
53 static const std::string THIRD_PNT("ThirdPoint");
56 static const std::string& POINT_ID(int theIndex)
59 case 1: return FIRST_POINT_ID();
60 case 2: return SECOND_POINT_ID();
61 case 3: return THIRD_POINT_ID();
64 static const std::string DUMMY;
70 SketchPlugin_Circle::SketchPlugin_Circle()
71 : SketchPlugin_SketchEntity()
75 void SketchPlugin_Circle::initDerivedClassAttributes()
77 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
78 data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
79 data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
80 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
82 data()->addAttribute(CIRCLE_TYPE(), ModelAPI_AttributeString::typeId());
83 data()->addAttribute(FIRST_POINT_ID(), GeomDataAPI_Point2D::typeId());
84 data()->addAttribute(SECOND_POINT_ID(), GeomDataAPI_Point2D::typeId());
85 data()->addAttribute(THIRD_POINT_ID(), GeomDataAPI_Point2D::typeId());
86 std::dynamic_pointer_cast<ModelAPI_AttributeString>(
87 data()->attribute(CIRCLE_TYPE()))->setValue(CIRCLE_TYPE_CENTER_AND_RADIUS());
90 void SketchPlugin_Circle::execute()
92 SketchPlugin_Sketch* aSketch = sketch();
94 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
96 // compute a circle point in 3D view
97 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
98 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
99 AttributeDoublePtr aRadiusAttr =
100 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
101 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
102 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
103 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
104 // make a visible point
105 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
107 // make a visible circle
108 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
109 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
110 std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z()));
111 // compute the circle radius
112 double aRadius = aRadiusAttr->value();
114 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
115 aCenter, aNormal, aRadius);
116 aShapes.push_back(aCircleShape);
117 std::shared_ptr<ModelAPI_ResultConstruction> aConstr2 = document()->createConstruction(
119 aConstr2->setShape(aCircleShape);
120 aConstr2->setIsInHistory(false);
121 setResult(aConstr2, 1);
128 AISObjectPtr SketchPlugin_Circle::getAISObject(AISObjectPtr thePrevious)
130 SketchPlugin_Sketch* aSketch = sketch();
131 if (aSketch && !isFeatureValid()) {
132 // compute a circle point in 3D view
133 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
134 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
135 AttributeDoublePtr aRadiusAttr =
136 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
137 if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
138 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
140 // make a visible circle
141 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
142 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
143 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
145 double aRadius = aRadiusAttr->value();
146 std::shared_ptr<GeomAPI_Shape> aCircleShape = GeomAlgoAPI_EdgeBuilder::lineCircle(
147 aCenter, aNormal, aRadius);
148 if (aCircleShape && aRadius != 0) {
149 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
150 // make a visible point
151 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
152 aShapes.push_back(aCenterPointShape);
153 aShapes.push_back(aCircleShape);
155 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
156 AISObjectPtr anAIS = thePrevious;
158 anAIS = AISObjectPtr(new GeomAPI_AISObject);
159 anAIS->createShape(aCompound);
165 return AISObjectPtr();
168 bool SketchPlugin_Circle::isFeatureValid()
170 std::shared_ptr<GeomDataAPI_Point2D> aCenter =
171 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
172 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
173 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
174 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
175 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
176 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
177 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
179 return aCenter->isInitialized() && aFirstPnt->isInitialized() &&
180 aSecondPnt->isInitialized() && aThirdPnt->isInitialized();
183 void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
185 std::shared_ptr<ModelAPI_Data> aData = data();
186 if (!aData->isValid())
189 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
190 aData->attribute(CENTER_ID()));
191 aPoint->move(theDeltaX, theDeltaY);
193 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(FIRST_POINT_ID()));
194 aPoint->move(theDeltaX, theDeltaY);
195 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SECOND_POINT_ID()));
196 aPoint->move(theDeltaX, theDeltaY);
197 aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(THIRD_POINT_ID()));
198 aPoint->move(theDeltaX, theDeltaY);
201 bool SketchPlugin_Circle::isFixed() {
202 return data()->selection(EXTERNAL_ID())->context().get() != NULL;
205 void SketchPlugin_Circle::attributeChanged(const std::string& theID) {
206 // the second condition for unability to move external segments anywhere
207 if (theID == EXTERNAL_ID() || isFixed()) {
208 std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
209 // update arguments due to the selection value
210 if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
211 std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
212 std::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
213 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
214 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
215 aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
216 real(RADIUS_ID())->setValue(aCirc->radius());
219 else if (theID == CENTER_ID() || theID == RADIUS_ID()) {
220 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
221 data()->attribute(CIRCLE_TYPE()))->value();
222 if (aType == CIRCLE_TYPE_THREE_POINTS())
225 // check the execute() was called and the shape was built
231 else if (theID == FIRST_POINT_ID() || theID == SECOND_POINT_ID() || theID == THIRD_POINT_ID()) {
232 std::string aType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
233 data()->attribute(CIRCLE_TYPE()))->value();
234 if (aType == CIRCLE_TYPE_CENTER_AND_RADIUS())
237 data()->blockSendAttributeUpdated(true);
239 std::shared_ptr<GeomAPI_Pnt2d> aPoints[3];
240 int aNbInitialized = 0;
241 for (int i = 1; i <= 3; ++i) {
242 std::shared_ptr<GeomDataAPI_Point2D> aCurPnt =
243 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(POINT_ID(i)));
244 if (aCurPnt->isInitialized())
245 aPoints[aNbInitialized++] = aCurPnt->pnt();
248 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
249 GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
250 AttributeDoublePtr aRadiusAttr =
251 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
253 if (aNbInitialized == 1)
254 aCenterAttr->setValue(aPoints[0]->x(), aPoints[0]->y());
255 else if (aNbInitialized == 2) {
256 std::shared_ptr<GeomAPI_XY> aCoord =
257 aPoints[0]->xy()->added(aPoints[1]->xy())->multiplied(0.5);
258 double aRadius = aPoints[0]->distance(aPoints[1]) * 0.5;
259 aCenterAttr->setValue(aCoord->x(), aCoord->y());
260 aRadiusAttr->setValue(aRadius);
262 std::shared_ptr<GeomAPI_Circ2d> aCircle(
263 new GeomAPI_Circ2d(aPoints[0], aPoints[1], aPoints[2]));
265 std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCircle->center();
267 double aRadius = aCircle->radius();
268 aCenterAttr->setValue(aCenter->x(), aCenter->y());
269 aRadiusAttr->setValue(aRadius);
273 data()->blockSendAttributeUpdated(false);
277 void SketchPlugin_Circle::adjustThreePoints()
279 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
280 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
281 if (!aCenterAttr->isInitialized())
283 AttributeDoublePtr aRadiusAttr =
284 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(attribute(RADIUS_ID()));
285 if (!aRadiusAttr->isInitialized())
288 data()->blockSendAttributeUpdated(true);
289 std::shared_ptr<GeomDataAPI_Point2D> aFirstPnt =
290 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(FIRST_POINT_ID()));
291 std::shared_ptr<GeomDataAPI_Point2D> aSecondPnt =
292 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(SECOND_POINT_ID()));
293 std::shared_ptr<GeomDataAPI_Point2D> aThirdPnt =
294 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(THIRD_POINT_ID()));
295 double aRadius = aRadiusAttr->value();
297 if (fabs(aFirstPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
298 fabs(aSecondPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance ||
299 fabs(aThirdPnt->pnt()->distance(aCenterAttr->pnt()) - aRadius) > tolerance) {
300 aFirstPnt->setValue(aCenterAttr->x() + aRadius, aCenterAttr->y());
301 aSecondPnt->setValue(aCenterAttr->x(), aCenterAttr->y() + aRadius);
302 aThirdPnt->setValue(aCenterAttr->x() - aRadius, aCenterAttr->y());
304 data()->blockSendAttributeUpdated(false);