]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Issue #1664: In the Sketcher, add the function Split a segment. Split feature impleme...
authornds <nds@opencascade.com>
Wed, 27 Jul 2016 04:50:02 +0000 (07:50 +0300)
committernds <nds@opencascade.com>
Wed, 27 Jul 2016 04:50:02 +0000 (07:50 +0300)
src/SketchPlugin/SketchPlugin_ConstraintSplit.cpp
src/SketchPlugin/SketchPlugin_ConstraintSplit.h

index af88d14d0a561d4da10c56f6cf5c45ca45701bfb..779ceca4640bd367371dd77c2a4c1343a0108f32 100755 (executable)
@@ -9,13 +9,18 @@
 //#include <GeomAPI_Circ2d.h>
 //#include <GeomAPI_Dir2d.h>
 //#include <GeomAPI_Lin2d.h>
-//#include <GeomAPI_Pnt2d.h>
+#include <GeomAPI_Pnt2d.h>
 //#include <GeomAPI_XY.h>
 #include <GeomDataAPI_Point2D.h>
 //#include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_AttributeReference.h>
+#include <ModelAPI_AttributeString.h>
 //#include <ModelAPI_AttributeRefList.h>
 #include <ModelAPI_AttributeRefAttr.h>
+
+#include <SketchPlugin_Line.h>
+#include <SketchPlugin_Arc.h>
+
 //#include <ModelAPI_Data.h>
 //#include <ModelAPI_Events.h>
 //#include <ModelAPI_Session.h>
@@ -80,16 +85,46 @@ void SketchPlugin_ConstraintSplit::initAttributes()
 
 void SketchPlugin_ConstraintSplit::execute()
 {
-/*  std::shared_ptr<ModelAPI_Data> aData = data();
+  std::shared_ptr<ModelAPI_Data> aData = data();
 
   // Check the base objects are initialized.
-  AttributeRefAttrListPtr aPointsRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(
-    aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
-  if(!aPointsRefList->isInitialized()) {
-    setError("Error: List of points is not initialized.");
+  AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                                            aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  if(!aBaseObjectAttr->isInitialized()) {
+    setError("Error: Base object is not initialized.");
+    return;
+  }
+  AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributePoint2DPtr aLastPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  if (!aFirstPointAttr.get() || !aFirstPointAttr->isInitialized() ||
+      !aLastPointAttr.get() || !aLastPointAttr->isInitialized()) {
+    setError("Error: Sub-shape is not initialized.");
     return;
   }
 
+  AttributePoint2DPtr aStartPointAttr = getFeaturePoint(true);
+  AttributePoint2DPtr anEndPointAttr = getFeaturePoint(false);
+  if (!aStartPointAttr.get() && !anEndPointAttr.get()) {
+    setError("Error: Circle is not processed."); /// TODO
+    return;
+  }
+
+  /// if first point is closer to last point, wrap first and last values
+  if (aStartPointAttr->pnt()->distance(aFirstPointAttr->pnt()) >
+      anEndPointAttr->pnt()->distance(aLastPointAttr->pnt())) {
+    AttributePoint2DPtr aTmpPoint = aFirstPointAttr;
+    aFirstPointAttr = aLastPointAttr;
+    aLastPointAttr = aTmpPoint;
+  }
+  FeaturePtr aSplitFeature = createFeature(aFirstPointAttr, aLastPointAttr);
+
+  std::set<FeaturePtr> aLeftFeatures;
+  if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt()))
+    aLeftFeatures.insert(createFeature(aStartPointAttr, aFirstPointAttr));
+  if (!aLastPointAttr->pnt()->isEqual(anEndPointAttr->pnt()))
+    aLeftFeatures.insert(createFeature(aLastPointAttr, anEndPointAttr));
+
+  /*
   // Get fillet radius.
   double aFilletRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
     aData->attribute(SketchPlugin_Constraint::VALUE()))->value();
@@ -1087,3 +1122,79 @@ std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
   return aCoincides;
 }
 */
+
+std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
+                                                                  const AttributePtr& theAttribute)
+{
+  AttributePoint2DPtr aPointAttribute;
+
+  if (theAttribute->id() == ModelAPI_AttributeRefAttr::typeId()) {
+    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
+    if (aRefAttr.get() && aRefAttr->isInitialized()) {
+      AttributePtr anAttribute = aRefAttr->attr();
+      if (anAttribute.get() && anAttribute->id() == GeomDataAPI_Point2D::typeId())
+        aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
+    }
+  }
+  return aPointAttribute;
+}
+
+AttributePoint2DPtr SketchPlugin_ConstraintSplit::getFeaturePoint(const bool& theStartPoint)
+{
+  AttributePoint2DPtr aPointAttribute;
+
+  AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                                           data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
+
+  std::string aFeatureKind = aBaseFeature->getKind();
+  std::string anAttributeName;
+  if (aFeatureKind == SketchPlugin_Line::ID())
+    anAttributeName = theStartPoint ? SketchPlugin_Line::START_ID()
+                                    : SketchPlugin_Line::END_ID();
+  else if (aFeatureKind == SketchPlugin_Arc::ID()) {
+    anAttributeName = theStartPoint ? SketchPlugin_Arc::START_ID()
+                                    : SketchPlugin_Arc::END_ID();
+  }
+  if (!anAttributeName.empty())
+    aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                                         aBaseFeature->attribute(anAttributeName));
+  return aPointAttribute;
+}
+
+FeaturePtr SketchPlugin_ConstraintSplit::createFeature(const AttributePoint2DPtr& theStartPointAttr,
+                                                       const AttributePoint2DPtr& theEndPointAttr)
+{
+  FeaturePtr aFeature;
+
+  AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                                           data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
+
+  std::string aFeatureKind = aBaseFeature->getKind();
+  aFeature = sketch()->addFeature(aFeatureKind);
+
+  if (aFeatureKind == SketchPlugin_Line::ID()) {
+    AttributePoint2DPtr aStartAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                              aFeature->attribute(SketchPlugin_Line::START_ID()));
+    aStartAttribute->setValue(theStartPointAttr->pnt());
+
+    AttributePoint2DPtr anEndAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                              aFeature->attribute(SketchPlugin_Line::END_ID()));
+    anEndAttribute->setValue(theEndPointAttr->pnt());
+  }
+  else if (aFeatureKind == SketchPlugin_Arc::ID()) {
+    AttributeStringPtr anArcType = std::dynamic_pointer_cast<ModelAPI_AttributeString>(
+             data()->addAttribute(SketchPlugin_Arc::ARC_TYPE(), ModelAPI_AttributeString::typeId()));
+
+    AttributePoint2DPtr aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                                  aFeature->attribute(SketchPlugin_Arc::START_ID()));
+    aPointAttribute->setValue(theStartPointAttr->pnt());
+
+    AttributePoint2DPtr anEndAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                              aFeature->attribute(SketchPlugin_Arc::END_ID()));
+    anEndAttribute->setValue(theEndPointAttr->pnt());
+  }
+
+  return aFeature;
+}
index e0888099058768fa4df4594adb55674370f7e784..02c7df346ddb441f37756fc14c4054899e88cd44 100755 (executable)
@@ -11,6 +11,8 @@
 #include <SketchPlugin_Sketch.h>
 #include "SketchPlugin_ConstraintBase.h"
 
+class GeomDataAPI_Point2D;
+
 /** \class SketchPlugin_ConstraintSplit
  *  \ingroup Plugins
  *  \brief Feature for creation of a new constraint filleting two objects which have coincident point
@@ -70,8 +72,22 @@ class SketchPlugin_ConstraintSplit : public SketchPlugin_ConstraintBase
   //};
 
 private:
-  /// \ Removes all produced features and restore base edges.
-  //void clearResults();
+  /// Returns geom point attribute of the feature
+  /// \param theStartPoint
+  /// \return geometical point
+  std::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(const bool& theStartPoint);
+
+  /// Returns cast of attribute to geometrical point if the attribute is a ref attr attribute
+  /// \param theAttribute an attribute
+  /// \param geom point 2D or NULL
+  std::shared_ptr<GeomDataAPI_Point2D> getPointOfRefAttr(const AttributePtr& theAttribute);
+
+  /// Creates a new feature in the base shape type with bounding points given in parameters
+  /// \param theStartPointAttr an attribute of the start point
+  /// \param theEndPointAttr an attribute of the end point
+  FeaturePtr SketchPlugin_ConstraintSplit::createFeature(
+                          const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
+                          const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
 
 private:
   //std::set<AttributePtr> myNewPoints; ///< set of new points