Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchAPI / SketchAPI_MacroEllipse.cpp
index ded0f75ba05b1014cfb03c0377fb69eccbbad5f2..c3ed48080d6f9d365379ccf597a359d7fc3aae32 100644 (file)
 //
 
 #include "SketchAPI_MacroEllipse.h"
+#include "SketchAPI_Line.h"
+#include "SketchAPI_Point.h"
 
 #include <GeomAPI_Pnt2d.h>
 
 #include <ModelHighAPI_RefAttr.h>
 #include <ModelHighAPI_Tools.h>
 
+#include <SketchPlugin_Sketch.h>
+
+#define MAJOR_AXIS_NEGATIVE (std::dynamic_pointer_cast<GeomDataAPI_Point2D>( \
+                             feature()->attribute(SketchPlugin_MacroEllipse::FIRST_POINT_ID())))
+#define MAJOR_AXIS_POSITIVE (std::dynamic_pointer_cast<GeomDataAPI_Point2D>( \
+                             feature()->attribute(SketchPlugin_MacroEllipse::SECOND_POINT_ID())))
+#define PASSED_POINT (std::dynamic_pointer_cast<GeomDataAPI_Point2D>( \
+                      feature()->attribute(SketchPlugin_MacroEllipse::PASSED_POINT_ID())))
+
+#define MAJOR_AXIS_NEGATIVE_REF (feature()->refattr( \
+                                 SketchPlugin_MacroEllipse::FIRST_POINT_REF_ID()))
+#define MAJOR_AXIS_POSITIVE_REF (feature()->refattr( \
+                                 SketchPlugin_MacroEllipse::SECOND_POINT_REF_ID()))
+#define PASSED_POINT_REF (feature()->refattr(SketchPlugin_MacroEllipse::PASSED_POINT_REF_ID()))
+
+
+// find a parent sketch
+static CompositeFeaturePtr sketch(FeaturePtr theFeature)
+{
+  CompositeFeaturePtr aSketch;
+  const std::set<AttributePtr>& aRefs = theFeature->data()->refsToMe();
+  for (std::set<AttributePtr>::const_iterator anIt = aRefs.begin(); anIt != aRefs.end(); ++anIt)
+    if ((*anIt)->id() == SketchPlugin_Sketch::FEATURES_ID()) {
+      aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>((*anIt)->owner());
+      break;
+    }
+  return aSketch;
+}
+
+
 SketchAPI_MacroEllipse::SketchAPI_MacroEllipse(const std::shared_ptr<ModelAPI_Feature>& theFeature)
 : SketchAPI_SketchEntity(theFeature)
 {
@@ -102,10 +134,11 @@ void SketchAPI_MacroEllipse::initializePoints(double theX1, double theY1,
                                               double theX2, double theY2,
                                               double theX3, double theY3)
 {
-  fillAttribute(majorAxisNegativePoint(), theX1, theY1);
-  fillAttribute(majorAxisPositivePoint(), theX2, theY2);
-  fillAttribute(passedPoint(), theX3, theY3);
+  fillAttribute(MAJOR_AXIS_NEGATIVE, theX1, theY1);
+  fillAttribute(MAJOR_AXIS_POSITIVE, theX2, theY2);
+  fillAttribute(PASSED_POINT, theX3, theY3);
 
+  mySketch = sketch(feature());
   execute();
 }
 
@@ -113,10 +146,11 @@ void SketchAPI_MacroEllipse::initializePoints(const std::shared_ptr<GeomAPI_Pnt2
                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint2,
                                               const std::shared_ptr<GeomAPI_Pnt2d>& thePoint3)
 {
-  fillAttribute(thePoint1, mymajorAxisNegativePoint);
-  fillAttribute(thePoint2, mymajorAxisPositivePoint);
-  fillAttribute(thePoint3, mypassedPoint);
+  fillAttribute(thePoint1, MAJOR_AXIS_NEGATIVE);
+  fillAttribute(thePoint2, MAJOR_AXIS_POSITIVE);
+  fillAttribute(thePoint3, PASSED_POINT);
 
+  mySketch = sketch(feature());
   execute();
 }
 
@@ -145,10 +179,94 @@ void SketchAPI_MacroEllipse::initializePoints(
     const ModelHighAPI_RefAttr&           thePassedPointRef)
 {
   fillAttribute(theMajorAxisPoint1, theMajorAxisPoint1Ref,
-                mymajorAxisNegativePoint, mymajorAxisNegativePointRef);
+                MAJOR_AXIS_NEGATIVE, MAJOR_AXIS_NEGATIVE_REF);
   fillAttribute(theMajorAxisPoint2, theMajorAxisPoint2Ref,
-                mymajorAxisPositivePoint, mymajorAxisPositivePointRef);
-  fillAttribute(thePassedPoint, thePassedPointRef, mypassedPoint, mypassedPointRef);
+                MAJOR_AXIS_POSITIVE, MAJOR_AXIS_POSITIVE_REF);
+  fillAttribute(thePassedPoint, thePassedPointRef,
+                PASSED_POINT, PASSED_POINT_REF);
 
+  mySketch = sketch(feature());
   execute();
 }
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::center()
+{
+  if (!myCenter)
+    collectAuxiliary();
+  return myCenter;
+}
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::focus1()
+{
+  if (!myFocus1)
+    collectAuxiliary();
+  return myFocus1;
+}
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::focus2()
+{
+  if (!myFocus2)
+    collectAuxiliary();
+  return myFocus2;
+}
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::majorAxisStart()
+{
+  if (!myMajorAxisStart)
+    collectAuxiliary();
+  return myMajorAxisStart;
+}
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::majorAxisEnd()
+{
+  if (!myMajorAxisEnd)
+    collectAuxiliary();
+  return myMajorAxisEnd;
+}
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::minorAxisStart()
+{
+  if (!myMinorAxisStart)
+    collectAuxiliary();
+  return myMinorAxisStart;
+}
+
+std::shared_ptr<SketchAPI_Point> SketchAPI_MacroEllipse::minorAxisEnd()
+{
+  if (!myMinorAxisEnd)
+    collectAuxiliary();
+  return myMinorAxisEnd;
+}
+
+std::shared_ptr<SketchAPI_Line> SketchAPI_MacroEllipse::majorAxis()
+{
+  if (!myMajorAxis)
+    collectAuxiliary();
+  return myMajorAxis;
+}
+
+std::shared_ptr<SketchAPI_Line> SketchAPI_MacroEllipse::minorAxis()
+{
+  if (!myMinorAxis)
+    collectAuxiliary();
+  return myMinorAxis;
+}
+
+void SketchAPI_MacroEllipse::collectAuxiliary()
+{
+  // collect auxiliary features
+  int aNbSubs = mySketch->numberOfSubs();
+  std::shared_ptr<SketchAPI_Point>* anAuxPoint[] = {
+    &myCenter, &myFocus1, &myFocus2,
+    &myMajorAxisStart, &myMajorAxisEnd,
+    &myMinorAxisStart, &myMinorAxisEnd
+  };
+  std::shared_ptr<SketchAPI_Line>* anAuxLine[] = {&myMajorAxis, &myMinorAxis};
+  for (int aPtInd = 7, aLinInd = 2; (aPtInd > 0 || aLinInd > 0) && aNbSubs >= 0; ) {
+    FeaturePtr aCurFeature = mySketch->subFeature(--aNbSubs);
+    if (aPtInd > 0 && aCurFeature->getKind() == SketchPlugin_Point::ID())
+      anAuxPoint[--aPtInd]->reset(new SketchAPI_Point(aCurFeature));
+    else if (aLinInd > 0 && aCurFeature->getKind() == SketchPlugin_Line::ID())
+      anAuxLine[--aLinInd]->reset(new SketchAPI_Line(aCurFeature));
+  }
+}