1 // Copyright (C) 2017 CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 // File: SketchPlugin_MacroEllipse.cpp
22 // Created: 26 April 2017
23 // Author: Artem ZHIDKOV
25 #include <SketchPlugin_MacroEllipse.h>
27 #include <SketchPlugin_Ellipse.h>
28 #include <SketchPlugin_Tools.h>
29 #include <SketchPlugin_Sketch.h>
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_EventReentrantMessage.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_Events.h>
38 #include <GeomDataAPI_Point2D.h>
40 #include <GeomAPI_Dir2d.h>
41 #include <GeomAPI_Pnt2d.h>
42 #include <GeomAPI_Ellipse2d.h>
43 #include <GeomAPI_Vertex.h>
45 #include <GeomAlgoAPI_CompoundBuilder.h>
46 #include <GeomAlgoAPI_EdgeBuilder.h>
47 #include <GeomAlgoAPI_PointBuilder.h>
50 SketchPlugin_MacroEllipse::SketchPlugin_MacroEllipse()
51 : SketchPlugin_SketchEntity(),
57 void SketchPlugin_MacroEllipse::initAttributes()
59 data()->addAttribute(CENTER_POINT_ID(), GeomDataAPI_Point2D::typeId());
60 data()->addAttribute(CENTER_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
61 data()->addAttribute(MAJOR_AXIS_POINT_ID(), GeomDataAPI_Point2D::typeId());
62 data()->addAttribute(MAJOR_AXIS_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
63 data()->addAttribute(PASSED_POINT_ID(), GeomDataAPI_Point2D::typeId());
64 data()->addAttribute(PASSED_POINT_REF_ID(), ModelAPI_AttributeRefAttr::typeId());
66 data()->addAttribute(MAJOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
67 data()->addAttribute(MINOR_RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
68 data()->addAttribute(AUXILIARY_ID(), ModelAPI_AttributeBoolean::typeId());
70 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CENTER_POINT_REF_ID());
71 ModelAPI_Session::get()->validators()->
72 registerNotObligatory(getKind(), MAJOR_AXIS_POINT_REF_ID());
73 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PASSED_POINT_REF_ID());
76 void SketchPlugin_MacroEllipse::execute()
78 FeaturePtr anEllipse = createEllipseFeature();
79 constraintsForEllipse(anEllipse);
81 // message to init reentrant operation
82 static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
83 ReentrantMessagePtr aMessage(new ModelAPI_EventReentrantMessage(anId, this));
84 aMessage->setCreatedFeature(anEllipse);
85 Events_Loop::loop()->send(aMessage);
88 void SketchPlugin_MacroEllipse::attributeChanged(const std::string& theID)
90 static const int NB_POINTS = 3;
91 std::string aPointAttrName[NB_POINTS] = { CENTER_POINT_ID(),
92 MAJOR_AXIS_POINT_ID(),
94 std::string aPointRefName[NB_POINTS] = { CENTER_POINT_REF_ID(),
95 MAJOR_AXIS_POINT_REF_ID(),
96 PASSED_POINT_REF_ID() };
98 int aNbInitialized = 0;
99 std::shared_ptr<GeomAPI_Pnt2d> anEllipsePoints[NB_POINTS];
101 for (int aPntIndex = 0; aPntIndex < NB_POINTS; ++aPntIndex) {
102 AttributePtr aPointAttr = attribute(aPointAttrName[aPntIndex]);
103 if (!aPointAttr->isInitialized())
106 AttributeRefAttrPtr aPointRef = refattr(aPointRefName[aPntIndex]);
107 // calculate ellipse parameters
108 std::shared_ptr<GeomAPI_Pnt2d> aPassedPoint;
109 std::shared_ptr<GeomAPI_Shape> aTangentCurve;
110 SketchPlugin_Tools::convertRefAttrToPointOrTangentCurve(
111 aPointRef, aPointAttr, aTangentCurve, aPassedPoint);
113 anEllipsePoints[aNbInitialized++] = aPassedPoint;
116 std::shared_ptr<GeomAPI_Ellipse2d> anEllipse;
117 if (aNbInitialized == 2) {
118 std::shared_ptr<GeomAPI_Dir2d> aXDir(new GeomAPI_Dir2d(
119 anEllipsePoints[1]->x() - anEllipsePoints[0]->x(),
120 anEllipsePoints[1]->y() - anEllipsePoints[0]->y()));
121 double aMajorRad = anEllipsePoints[1]->distance(anEllipsePoints[0]);
122 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
123 new GeomAPI_Ellipse2d(anEllipsePoints[0], aXDir, aMajorRad, 0.5 * aMajorRad));
124 } else if (aNbInitialized == 3) {
125 anEllipse = std::shared_ptr<GeomAPI_Ellipse2d>(
126 new GeomAPI_Ellipse2d(anEllipsePoints[0], anEllipsePoints[1], anEllipsePoints[2]));
129 if (!anEllipse || anEllipse->implPtr<void>() == 0)
132 myCenter = anEllipse->center();
133 myFocus = anEllipse->firstFocus();
134 myMajorRadius = anEllipse->majorRadius();
135 myMinorRadius = anEllipse->minorRadius();
137 AttributeDoublePtr aMajorRadiusAttr = real(MAJOR_RADIUS_ID());
138 AttributeDoublePtr aMinorRadiusAttr = real(MINOR_RADIUS_ID());
140 bool aWasBlocked = data()->blockSendAttributeUpdated(true);
141 // center attribute is used in processEvent() to set reference to reentrant arc
142 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_POINT_ID()))->setValue(myCenter);
143 aMajorRadiusAttr->setValue(myMajorRadius);
144 aMinorRadiusAttr->setValue(myMinorRadius);
145 data()->blockSendAttributeUpdated(aWasBlocked, false);
148 std::string SketchPlugin_MacroEllipse::processEvent(
149 const std::shared_ptr<Events_Message>& theMessage)
151 std::string aFilledAttributeName;
153 ReentrantMessagePtr aReentrantMessage =
154 std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
155 if (aReentrantMessage) {
156 FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
157 ObjectPtr anObject = aReentrantMessage->selectedObject();
158 AttributePtr anAttribute = aReentrantMessage->selectedAttribute();
159 std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = aReentrantMessage->clickedPoint();
161 if (aClickedPoint && (anObject || anAttribute)) {
162 aFilledAttributeName = CENTER_POINT_ID();
163 std::string aReferenceAttributeName = CENTER_POINT_REF_ID();
165 // fill 2d point attribute
166 AttributePoint2DPtr aPointAttr =
167 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(aFilledAttributeName));
168 aPointAttr->setValue(aClickedPoint);
170 // fill reference attribute
171 AttributeRefAttrPtr aRefAttr =
172 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(attribute(aReferenceAttributeName));
174 if (!anAttribute->owner() || !anAttribute->owner()->data()->isValid()) {
175 if (aCreatedFeature && anAttribute->id() == CENTER_POINT_ID())
176 anAttribute = aCreatedFeature->attribute(SketchPlugin_Ellipse::CENTER_ID());
178 aRefAttr->setAttr(anAttribute);
180 else if (anObject.get()) {
181 // if presentation of previous reentrant macro arc is used, the object is invalid,
182 // we should use result of previous feature of the message(Arc)
183 if (!anObject->data()->isValid())
184 anObject = aCreatedFeature->lastResult();
185 aRefAttr->setObject(anObject);
188 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
190 return aFilledAttributeName;
193 void SketchPlugin_MacroEllipse::constraintsForEllipse(FeaturePtr theEllipseFeature)
195 // Create constraints.
196 SketchPlugin_Tools::createCoincidenceOrTangency(
197 this, CENTER_POINT_REF_ID(),
198 theEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()),
200 SketchPlugin_Tools::createCoincidenceOrTangency(
201 this, MAJOR_AXIS_POINT_REF_ID(), AttributePtr(),
202 theEllipseFeature->lastResult(), true);
203 SketchPlugin_Tools::createCoincidenceOrTangency(
204 this, PASSED_POINT_REF_ID(), AttributePtr(),
205 theEllipseFeature->lastResult(), true);
208 FeaturePtr SketchPlugin_MacroEllipse::createEllipseFeature()
210 FeaturePtr aEllipseFeature = sketch()->addFeature(SketchPlugin_Ellipse::ID());
212 AttributePoint2DPtr aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
213 aEllipseFeature->attribute(SketchPlugin_Ellipse::CENTER_ID()));
214 aCenterAttr->setValue(myCenter->x(), myCenter->y());
216 AttributePoint2DPtr aFocusAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217 aEllipseFeature->attribute(SketchPlugin_Ellipse::FOCUS_ID()));
218 aFocusAttr->setValue(myFocus->x(), myFocus->y());
220 aEllipseFeature->real(SketchPlugin_Ellipse::MAJOR_RADIUS_ID())->setValue(myMajorRadius);
221 aEllipseFeature->real(SketchPlugin_Ellipse::MINOR_RADIUS_ID())->setValue(myMinorRadius);
223 aEllipseFeature->boolean(SketchPlugin_Ellipse::AUXILIARY_ID())->setValue(
224 boolean(AUXILIARY_ID())->value());
226 aEllipseFeature->execute();
227 return aEllipseFeature;
230 AISObjectPtr SketchPlugin_MacroEllipse::getAISObject(AISObjectPtr thePrevious)
232 SketchPlugin_Sketch* aSketch = sketch();
233 if (!aSketch || !myCenter || myMajorRadius == 0)
234 return AISObjectPtr();
236 std::shared_ptr<GeomDataAPI_Dir> aNDir = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
237 aSketch->data()->attribute(SketchPlugin_Sketch::NORM_ID()));
239 // Compute a ellipse in 3D view.
240 std::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(myCenter->x(), myCenter->y()));
241 std::shared_ptr<GeomAPI_Pnt> aFocus(aSketch->to3D(myFocus->x(), myFocus->y()));
242 std::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
243 std::shared_ptr<GeomAPI_Dir> aMajorAxis(new GeomAPI_Dir(aFocus->x() - aCenter->x(),
244 aFocus->y() - aCenter->y(), aFocus->z() - aCenter->z()));
246 std::shared_ptr<GeomAPI_Shape> anEllipseShape =
247 GeomAlgoAPI_EdgeBuilder::ellipse(aCenter, aNormal, aMajorAxis, myMajorRadius, myMinorRadius);
248 GeomShapePtr aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
249 if (!anEllipseShape.get() || !aCenterPointShape.get())
250 return AISObjectPtr();
252 std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
253 aShapes.push_back(anEllipseShape);
254 aShapes.push_back(aCenterPointShape);
256 std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
257 AISObjectPtr anAIS = thePrevious;
259 anAIS.reset(new GeomAPI_AISObject());
260 anAIS->createShape(aCompound);