1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchAPI_Arc.cpp
4 // Created: 09 June 2016
5 // Author: Dmitry Bobylev
7 #include "SketchAPI_Arc.h"
9 #include <GeomAPI_Pnt2d.h>
11 #include <ModelHighAPI_Double.h>
12 #include <ModelHighAPI_Dumper.h>
13 #include <ModelHighAPI_Selection.h>
14 #include <ModelHighAPI_Tools.h>
16 #include <SketchPlugin_ConstraintCoincidence.h>
17 #include <SketchPlugin_ConstraintTangent.h>
19 /// Obtain constraints prepared by tangent arc
20 static std::list<FeaturePtr> tangentArcConstraints(const FeaturePtr& theArc);
22 //================================================================================================
23 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature> & theFeature)
24 : SketchAPI_SketchEntity(theFeature)
29 //================================================================================================
30 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
31 double theCenterX, double theCenterY,
32 double theStartX, double theStartY,
33 double theEndX, double theEndY,
35 : SketchAPI_SketchEntity(theFeature)
38 setByCenterStartEnd(theCenterX, theCenterY, theStartX,
39 theStartY, theEndX, theEndY, theInversed);
43 //================================================================================================
44 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
45 const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
46 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
47 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
49 : SketchAPI_SketchEntity(theFeature)
52 setByCenterStartEnd(theCenter, theStart, theEnd, theInversed);
56 //================================================================================================
57 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
58 double theStartX, double theStartY,
59 double theEndX, double theEndY,
60 double thePassedX, double thePassedY)
61 : SketchAPI_SketchEntity(theFeature)
64 setByStartEndPassed(theStartX, theStartY, theEndX, theEndY, thePassedX, thePassedY);
68 //===============================================================================================
69 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
70 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
71 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
72 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
73 : SketchAPI_SketchEntity(theFeature)
76 setByStartEndPassed(theStart, theEnd, thePassed);
80 //================================================================================================
81 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
82 const ModelHighAPI_RefAttr& theTangentPoint,
83 double theEndX, double theEndY,
85 : SketchAPI_SketchEntity(theFeature)
88 setByTangent(theTangentPoint, theEndX, theEndY, theInversed);
92 //================================================================================================
93 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
94 const ModelHighAPI_RefAttr& theTangentPoint,
95 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
97 : SketchAPI_SketchEntity(theFeature)
100 setByTangent(theTangentPoint, theEnd, theInversed);
104 //================================================================================================
105 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
106 const ModelHighAPI_Selection& theExternal)
107 : SketchAPI_SketchEntity(theFeature)
110 setByExternal(theExternal);
114 //================================================================================================
115 SketchAPI_Arc::SketchAPI_Arc(const std::shared_ptr<ModelAPI_Feature>& theFeature,
116 const std::string& theExternalName)
117 : SketchAPI_SketchEntity(theFeature)
120 setByExternalName(theExternalName);
124 //================================================================================================
125 SketchAPI_Arc::~SketchAPI_Arc()
130 //================================================================================================
131 void SketchAPI_Arc::setByCenterStartEnd(double theCenterX, double theCenterY,
132 double theStartX, double theStartY,
133 double theEndX, double theEndY,
136 fillAttribute(SketchPlugin_Arc::ARC_TYPE_CENTER_START_END(), myarcType);
137 fillAttribute(center(), theCenterX, theCenterY);
138 fillAttribute(startPoint(), theStartX, theStartY);
139 fillAttribute(endPoint(), theEndX, theEndY);
140 fillAttribute(theInversed, myinversed);
145 //================================================================================================
146 void SketchAPI_Arc::setByCenterStartEnd(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
147 const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
148 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
151 fillAttribute(SketchPlugin_Arc::ARC_TYPE_CENTER_START_END(), myarcType);
152 fillAttribute(theCenter, mycenter);
153 fillAttribute(theStart, mystartPoint);
154 fillAttribute(theEnd, myendPoint);
155 fillAttribute(theInversed, myinversed);
160 //================================================================================================
161 void SketchAPI_Arc::setByStartEndPassed(double theStartX, double theStartY,
162 double theEndX, double theEndY,
163 double thePassedX, double thePassedY)
165 fillAttribute(SketchPlugin_Arc::ARC_TYPE_THREE_POINTS(), myarcType);
166 fillAttribute(startPoint(), theStartX, theStartY);
167 fillAttribute(endPoint(), theEndX, theEndY);
168 fillAttribute(passedPoint(), thePassedX, thePassedY);
173 //================================================================================================
174 void SketchAPI_Arc::setByStartEndPassed(const std::shared_ptr<GeomAPI_Pnt2d>& theStart,
175 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
176 const std::shared_ptr<GeomAPI_Pnt2d>& thePassed)
178 fillAttribute(SketchPlugin_Arc::ARC_TYPE_THREE_POINTS(), myarcType);
179 fillAttribute(theStart, mystartPoint);
180 fillAttribute(theEnd, myendPoint);
181 fillAttribute(thePassed, mypassedPoint);
186 //================================================================================================
187 void SketchAPI_Arc::setByTangent(const ModelHighAPI_RefAttr& theTangentPoint,
188 double theEndX, double theEndY,
191 fillAttribute(SketchPlugin_Arc::ARC_TYPE_TANGENT(), myarcType);
192 fillAttribute(theTangentPoint, mytangentPoint);
193 fillAttribute(endPoint(), theEndX, theEndY);
194 fillAttribute(theInversed, myinversed);
199 //================================================================================================
200 void SketchAPI_Arc::setByTangent(const ModelHighAPI_RefAttr& theTangentPoint,
201 const std::shared_ptr<GeomAPI_Pnt2d>& theEnd,
204 fillAttribute(SketchPlugin_Arc::ARC_TYPE_TANGENT(), myarcType);
205 fillAttribute(theTangentPoint, mytangentPoint);
206 fillAttribute(theEnd, myendPoint);
207 fillAttribute(theInversed, myinversed);
212 //================================================================================================
213 void SketchAPI_Arc::setByExternal(const ModelHighAPI_Selection & theExternal)
215 fillAttribute(theExternal, external());
220 //===============================================================================================
221 void SketchAPI_Arc::setByExternalName(const std::string & theExternalName)
223 fillAttribute(ModelHighAPI_Selection("EDGE", theExternalName), external());
228 //===============================================================================================
229 void SketchAPI_Arc::setRadius(double theRadius)
231 fillAttribute(ModelHighAPI_Double(theRadius), myradius);
236 //================================================================================================
237 void SketchAPI_Arc::setAngle(double theAngle)
239 fillAttribute(ModelHighAPI_Double(theAngle), myangle);
244 //================================================================================================
245 void SketchAPI_Arc::dump(ModelHighAPI_Dumper& theDumper) const
248 return; // no need to dump copied feature
250 FeaturePtr aBase = feature();
251 const std::string& aSketchName = theDumper.parentName(aBase);
253 AttributeSelectionPtr anExternal = aBase->selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
254 if (anExternal->context()) {
256 theDumper << aBase << " = " << aSketchName << ".addArc(" << anExternal << ")" << std::endl;
258 AttributeStringPtr aType = arcType();
259 if (aType->value() == SketchPlugin_Arc::ARC_TYPE_CENTER_START_END()) {
260 // arc given by center and start, end points
261 theDumper << aBase << " = " << aSketchName << ".addArc(" << center() << ", "
262 << startPoint() << ", " << endPoint() << ", " << inversed() << ")" << std::endl;
263 } else if (aType->value() == SketchPlugin_Arc::ARC_TYPE_THREE_POINTS()) {
264 // arc given by three points
265 theDumper << aBase << " = " << aSketchName << ".addArc(" << startPoint() << ", "
266 << endPoint() << ", " << passedPoint() << ")" << std::endl;
268 // do not dump coincidence and tangency constraint built by tangent arc
269 std::list<FeaturePtr> aConstraintsToSkip = tangentArcConstraints(aBase);
270 std::list<FeaturePtr>::iterator anIt = aConstraintsToSkip.begin();
271 for (; anIt != aConstraintsToSkip.end(); ++anIt)
272 theDumper.doNotDumpFeature(*anIt);
275 AttributeRefAttrPtr aTangentPoint = tangentPoint();
276 theDumper << aBase << " = " << aSketchName << ".addArc("
277 << aTangentPoint << ", " << endPoint() << ", " << inversed() << ")" << std::endl;
280 // dump "auxiliary" flag if necessary
281 SketchAPI_SketchEntity::dump(theDumper);
285 // ==================== Auxiliary functions ===============================
286 std::list<FeaturePtr> tangentArcConstraints(const FeaturePtr& theArc)
288 std::list<FeaturePtr> aConstraints;
290 std::set<FeaturePtr> aCoincidences;
291 std::set<FeaturePtr> aTangencies;
293 const std::set<AttributePtr>& aBaseRefs = theArc->data()->refsToMe();
294 std::set<AttributePtr>::const_iterator anIt = aBaseRefs.begin();
295 for (; anIt != aBaseRefs.end(); ++anIt) {
296 FeaturePtr anOwner = ModelAPI_Feature::feature((*anIt)->owner());
297 if (anOwner->getKind() == SketchPlugin_ConstraintCoincidence::ID())
298 aCoincidences.insert(anOwner);
300 const std::set<AttributePtr>& aBaseResultRefs = theArc->lastResult()->data()->refsToMe();
301 for (anIt = aBaseResultRefs.begin(); anIt != aBaseResultRefs.end(); ++anIt) {
302 FeaturePtr anOwner = ModelAPI_Feature::feature((*anIt)->owner());
303 if (anOwner->getKind() == SketchPlugin_ConstraintTangent::ID())
304 aTangencies.insert(anOwner);
307 AttributePtr aTangentPoint = theArc->refattr(SketchPlugin_Arc::TANGENT_POINT_ID())->attr();
309 FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentPoint->owner());
311 const std::set<AttributePtr>& aTgRefs = aTangentFeature->data()->refsToMe();
312 for (anIt = aTgRefs.begin(); anIt != aTgRefs.end(); ++anIt) {
313 FeaturePtr anOwner = ModelAPI_Feature::feature((*anIt)->owner());
314 if (aCoincidences.find(anOwner) != aCoincidences.end()) {
315 // check the coincidence is correct
316 AttributePtr aConstrained1 = anOwner->refattr(SketchPlugin_Constraint::ENTITY_A())->attr();
317 AttributePtr aConstrained2 = anOwner->refattr(SketchPlugin_Constraint::ENTITY_B())->attr();
318 AttributePtr anArcStartPoint = theArc->attribute(SketchPlugin_Arc::START_ID());
319 if ((aConstrained1 == anArcStartPoint && aConstrained2 == aTangentPoint) ||
320 (aConstrained1 == aTangentPoint && aConstrained2 == anArcStartPoint)) {
321 aConstraints.push_back(anOwner);
322 break; // search first applicable coincidence only
327 const std::set<AttributePtr>& aTgResultRefs =
328 aTangentFeature->lastResult()->data()->refsToMe();
329 for (anIt = aTgResultRefs.begin(); anIt != aTgResultRefs.end(); ++anIt) {
330 FeaturePtr anOwner = ModelAPI_Feature::feature((*anIt)->owner());
331 if (aTangencies.find(anOwner) != aTangencies.end()) {
332 aConstraints.push_back(anOwner);
333 break; // search first tangency only