]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Implementation of multi-translation and multi-rotation
authorazv <azv@opencascade.com>
Wed, 22 Apr 2015 08:02:20 +0000 (11:02 +0300)
committerazv <azv@opencascade.com>
Wed, 22 Apr 2015 12:23:50 +0000 (15:23 +0300)
src/SketchPlugin/CMakeLists.txt
src/SketchPlugin/SketchPlugin_MultiRotation.cpp [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_MultiRotation.h [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_MultiTranslation.cpp [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_MultiTranslation.h [new file with mode: 0644]
src/SketchPlugin/SketchPlugin_Plugin.cpp
src/SketchPlugin/SketchPlugin_Validators.cpp
src/SketchPlugin/SketchPlugin_Validators.h
src/SketchPlugin/plugin-Sketch.xml

index f7e3f9aca2bf11eea7cc0bb7d90bb41b198da79b..b4aa3d2cd75278a921a678bbefe014ee28354261 100644 (file)
@@ -28,6 +28,8 @@ SET(PROJECT_HEADERS
     SketchPlugin_ConstraintTangent.h
     SketchPlugin_ConstraintMirror.h
     SketchPlugin_ConstraintFillet.h
+    SketchPlugin_MultiRotation.h
+    SketchPlugin_MultiTranslation.h
     SketchPlugin_ExternalValidator.h
     SketchPlugin_Validators.h
 )
@@ -55,6 +57,8 @@ SET(PROJECT_SOURCES
     SketchPlugin_ConstraintTangent.cpp
     SketchPlugin_ConstraintMirror.cpp
     SketchPlugin_ConstraintFillet.cpp
+    SketchPlugin_MultiRotation.cpp
+    SketchPlugin_MultiTranslation.cpp
     SketchPlugin_ExternalValidator.cpp
     SketchPlugin_Validators.cpp
 )
diff --git a/src/SketchPlugin/SketchPlugin_MultiRotation.cpp b/src/SketchPlugin/SketchPlugin_MultiRotation.cpp
new file mode 100644 (file)
index 0000000..130a8ff
--- /dev/null
@@ -0,0 +1,267 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
+// File:    SketchPlugin_MultiRotation.cpp
+// Created: 21 Apr 2015
+// Author:  Artem ZHIDKOV
+
+#include "SketchPlugin_MultiRotation.h"
+
+#include <GeomDataAPI_Point2D.h>
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
+
+#include <GeomAPI_Pnt2d.h>
+#include <GeomAPI_XY.h>
+
+#define PI 3.1415926535897932
+
+SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
+{
+}
+
+void SketchPlugin_MultiRotation::initAttributes()
+{
+  data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
+  data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
+  data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/);
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
+  AttributeSelectionListPtr aSelection = 
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
+    ROTATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
+  aSelection->setSelectionType("EDGE");
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
+}
+
+void SketchPlugin_MultiRotation::execute()
+{
+  AttributeSelectionListPtr aRotationObjectRefs = selectionList(ROTATION_LIST_ID());
+  int aNbCopies = (int)(std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      attribute(NUMBER_OF_COPIES_ID()))->value());
+
+  // Obtain center and angle of rotation
+  std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      attribute(CENTER_ID()));
+  if (!aCenter || !aCenter->isInitialized())
+    return;
+  double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      attribute(ANGLE_ID()))->value();
+  // Convert angle to radians
+  anAngle *= PI / 180.0;
+
+  // Wait all objects being created, then send update events
+  static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+  bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
+  if (isUpdateFlushed)
+    Events_Loop::loop()->setFlushed(anUpdateEvent, false);
+
+  std::shared_ptr<ModelAPI_Data> aData = data();
+  AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  int aCurrentNbCopies = aRefListOfShapes->size() ?
+      aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
+  std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
+  std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
+  std::list<ResultPtr> anAddition;
+  std::vector<bool> isUsed(anInitialList.size(), false);
+  // collect new items and check the items to remove
+  for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
+    std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aRotationObjectRefs->value(anInd);
+    std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
+    std::vector<bool>::iterator aUsedIt = isUsed.begin();
+    for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
+      if (*anIt == aSelect->context()) {
+        *aUsedIt = true;
+        break;
+      }
+    if (anIt == anInitialList.end())
+      anAddition.push_back(aSelect->context());
+  }
+  // remove unused items
+  std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
+  std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
+  std::vector<bool>::iterator aUsedIter = isUsed.begin();
+  for (; aUsedIter != isUsed.end(); aUsedIter++) {
+    if (!(*aUsedIter)) {
+      aRefListOfShapes->remove(*anInitIter);
+      for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
+        aRefListOfRotated->remove(*aTargetIter);
+        // remove the corresponding feature from the sketch
+        ResultConstructionPtr aRC =
+            std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
+        DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
+        FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
+        if (aFeature)
+          aDoc->removeFeature(aFeature);
+      }
+    } else {
+      for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
+        aTargetIter++;
+    }
+    if (anInitIter != anInitialList.end())
+      anInitIter++;
+  }
+  // change number of copies
+  if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
+    bool isAdd = aNbCopies > aCurrentNbCopies;
+    int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
+    int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
+    int ind = 0;
+
+    aTargetList = aRefListOfRotated->list();
+    aTargetIter = aTargetList.begin();
+    ObjectPtr anObjToCopy = *aTargetIter;
+    while (aTargetIter != aTargetList.end()) {
+      aRefListOfRotated->remove(*aTargetIter);
+      aTargetIter++;
+      ind++;
+      if (ind > aMinCopies && ind <=aMaxCopies) {
+        while (ind <= aMaxCopies) {
+          if (isAdd) {
+            // Add new shifted item
+            ObjectPtr anObject = copyFeature(anObjToCopy);
+            aTargetList.insert(aTargetIter, anObject);
+          } else {
+            // remove object
+            std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter;
+            ObjectPtr anObject = *(--aRemoveIt);
+            aTargetList.erase(aRemoveIt);
+            // remove the corresponding feature from the sketch
+            ResultConstructionPtr aRC =
+                std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
+            DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
+            FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
+            if (aFeature)
+              aDoc->removeFeature(aFeature);
+          }
+          ind++;
+        }
+        ind = 0;
+        if (aTargetIter != aTargetList.end())
+          anObjToCopy = *aTargetIter;
+      }
+    }
+
+    for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
+      aRefListOfRotated->append(*aTargetIter);
+  }
+  // add new items
+  std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
+  for (; anAddIter != anAddition.end(); anAddIter++) {
+    aRefListOfShapes->append(*anAddIter);
+    aRefListOfRotated->append(*anAddIter);
+    for (int i = 0; i < aNbCopies; i++) {
+      ObjectPtr anObject = copyFeature(*anAddIter);
+      aRefListOfRotated->append(anObject);
+    }
+  }
+
+  if (fabs(anAngle) > 1.e-12) {
+    // Recalculate positions of features
+    aTargetList = aRefListOfRotated->list();
+    aTargetIter = aTargetList.begin();
+    while (aTargetIter != aTargetList.end()) {
+      ObjectPtr anInitialObject = *aTargetIter++;
+      for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
+        rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
+    }
+  }
+
+  // send events to update the sub-features by the solver
+  if (isUpdateFlushed)
+    Events_Loop::loop()->setFlushed(anUpdateEvent, true);
+}
+
+AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
+{
+  if (!sketch())
+    return thePrevious;
+
+  AISObjectPtr anAIS = thePrevious;
+  if (!anAIS) {
+// TODO:
+//    anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
+  }
+  return anAIS;
+}
+
+
+ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
+{
+  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
+  FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+  if (!aFeature || !aResult)
+    return ObjectPtr();
+
+  FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
+  aFeature->data()->copyTo(aNewFeature->data());
+  aNewFeature->execute();
+
+  static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
+
+  std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
+  const std::list<ResultPtr>& aResults = aNewFeature->results();
+  std::list<ResultPtr>::const_iterator anIt = aResults.begin();
+  for (; anIt != aResults.end(); anIt++) {
+    ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
+    if (!aRC) continue;
+    std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
+    if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
+        (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
+        (aShapeIn->isFace() && aShapeOut->isFace()))
+      return aRC;
+  }
+  return ObjectPtr();
+}
+
+void SketchPlugin_MultiRotation::rotateFeature(
+    ObjectPtr theInitial, ObjectPtr theTarget,
+    double theCenterX, double theCenterY, double theAngle)
+{
+  std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
+  double cosA = cos(theAngle);
+  double sinA = sin(theAngle);
+
+  FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
+  FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
+
+  // block feature update
+  aTargetFeature->data()->blockSendAttributeUpdated(true);
+
+  std::list<AttributePtr> anInitAttrList =
+      anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr> aTargetAttrList =
+      aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
+  std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
+  for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
+    std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
+    std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
+    std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
+    if (aPnt->distance(aCenter->xy()) > 1.e-7) {
+      std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
+      double dx = cosA * aDir->x() - sinA * aDir->y();
+      double dy = sinA * aDir->x() + cosA * aDir->y();
+      aPnt->setX(aCenter->x() + dx);
+      aPnt->setY(aCenter->y() + dy);
+    }
+    aPointTo->setValue(aPnt->x(), aPnt->y());
+  }
+
+  // unblock feature update
+  aTargetFeature->data()->blockSendAttributeUpdated(false);
+}
+
diff --git a/src/SketchPlugin/SketchPlugin_MultiRotation.h b/src/SketchPlugin/SketchPlugin_MultiRotation.h
new file mode 100644 (file)
index 0000000..e743386
--- /dev/null
@@ -0,0 +1,87 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
+// File:    SketchPlugin_MultiRotation.h
+// Created: 21 Apr 2015
+// Author:  Artem ZHIDKOV
+
+#ifndef SketchPlugin_MultiRotation_H_
+#define SketchPlugin_MultiRotation_H_
+
+#include "SketchPlugin.h"
+#include <SketchPlugin_Sketch.h>
+#include "SketchPlugin_ConstraintBase.h"
+
+/** \class SketchPlugin_MultiRotation
+ *  \ingroup Plugins
+ *  \brief Feature for creation of a number of rotated copies of a list of objects
+ *
+ *  This constraint has two attributes:
+ *  SketchPlugin_Constraint::ENTITY_A() for initial list of objects and
+ *  SketchPlugin_Constraint::ENTITY_B() for the list of created objects
+ *
+ *  The list of created objects contains a number of copies of each object given in
+ *  the NUMBER_OF_COPIES_ID() attribute plus 1 (the initial objects are stored into this
+ *  attribute too). At the start of the list, there are collected N copies
+ *  of first object from initial list, then N copies of second object etc.
+ */
+class SketchPlugin_MultiRotation : public SketchPlugin_ConstraintBase
+{
+ public:
+  /// Multi-rotation kind
+  inline static const std::string& ID()
+  {
+    static const std::string MY_CONSTRAINT_ROTATION_ID("SketchMultiRotation");
+    return MY_CONSTRAINT_ROTATION_ID;
+  }
+  /// \brief Returns the kind of a feature
+  SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
+  {
+    static std::string MY_KIND = SketchPlugin_MultiRotation::ID();
+    return MY_KIND;
+  }
+
+  /// List of objects to be mirrored
+  inline static const std::string& ROTATION_LIST_ID()
+  {
+    static const std::string MY_ROTATION_LIST_ID("MultiRotationList");
+    return MY_ROTATION_LIST_ID;
+  }
+
+  /// Center of rotation
+  inline static const std::string& CENTER_ID()
+  {
+    static const std::string MY_CENTER_ID("MultiRotationCenter");
+    return MY_CENTER_ID;
+  }
+  /// End point of translation
+  inline static const std::string& ANGLE_ID()
+  {
+    static const std::string MY_ANGLE_ID("MultiRotationAngle");
+    return MY_ANGLE_ID;
+  }
+  /// Number of translated objects
+  inline static const std::string& NUMBER_OF_COPIES_ID()
+  {
+    static const std::string MY_NUMBER_OF_COPIES_ID("MultiRotationCopies");
+    return MY_NUMBER_OF_COPIES_ID;
+  }
+
+  /// \brief Creates a new part document if needed
+  SKETCHPLUGIN_EXPORT virtual void execute();
+
+  /// \brief Request for initialization of data model of the feature: adding all attributes
+  SKETCHPLUGIN_EXPORT virtual void initAttributes();
+
+  /// Returns the AIS preview
+  SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
+
+  /// \brief Use plugin manager for features creation
+  SketchPlugin_MultiRotation();
+
+private:
+  ObjectPtr copyFeature(ObjectPtr theObject);
+  void rotateFeature(ObjectPtr theInitial, ObjectPtr theTarget,
+                     double theCenterX, double theCenterY, double theAngle);
+};
+
+#endif
diff --git a/src/SketchPlugin/SketchPlugin_MultiTranslation.cpp b/src/SketchPlugin/SketchPlugin_MultiTranslation.cpp
new file mode 100644 (file)
index 0000000..a59283e
--- /dev/null
@@ -0,0 +1,246 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
+// File:    SketchPlugin_MultiTranslation.cpp
+// Created: 21 Apr 2015
+// Author:  Artem ZHIDKOV
+
+#include "SketchPlugin_MultiTranslation.h"
+
+#include <GeomAPI_XY.h>
+#include <GeomDataAPI_Point2D.h>
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
+#include <ModelAPI_Data.h>
+#include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
+
+SketchPlugin_MultiTranslation::SketchPlugin_MultiTranslation()
+{
+}
+
+void SketchPlugin_MultiTranslation::initAttributes()
+{
+  data()->addAttribute(START_POINT_ID(), GeomDataAPI_Point2D::typeId());
+  data()->addAttribute(END_POINT_ID(), GeomDataAPI_Point2D::typeId());
+  data()->addAttribute(NUMBER_OF_COPIES_ID(), ModelAPI_AttributeDouble::typeId()/*ModelAPI_AttributeInteger::typeId()*/);
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
+  AttributeSelectionListPtr aSelection = 
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
+    TRANSLATION_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
+  aSelection->setSelectionType("EDGE");
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
+}
+
+void SketchPlugin_MultiTranslation::execute()
+{
+  AttributeSelectionListPtr aTranslationObjectRefs = selectionList(TRANSLATION_LIST_ID());
+  int aNbCopies = (int)(std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      attribute(NUMBER_OF_COPIES_ID()))->value());
+
+  // Calculate shift vector
+  std::shared_ptr<GeomDataAPI_Point2D> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      attribute(START_POINT_ID()));
+  std::shared_ptr<GeomDataAPI_Point2D> aEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      attribute(END_POINT_ID()));
+  if (!aStart || !aEnd || !aStart->isInitialized() || !aEnd->isInitialized())
+    return;
+  std::shared_ptr<GeomAPI_XY> aShiftVec(new GeomAPI_XY(aEnd->x() - aStart->x(), aEnd->y() - aStart->y()));
+
+  // Wait all objects being created, then send update events
+  static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
+  bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
+  if (isUpdateFlushed)
+    Events_Loop::loop()->setFlushed(anUpdateEvent, false);
+
+  std::shared_ptr<ModelAPI_Data> aData = data();
+  AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr aRefListOfTranslated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  int aCurrentNbCopies = aRefListOfShapes->size() ?
+      aRefListOfTranslated->size() / aRefListOfShapes->size() - 1 : 0;
+  std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
+  std::list<ObjectPtr> aTargetList = aRefListOfTranslated->list();
+  std::list<ResultPtr> anAddition;
+  std::vector<bool> isUsed(anInitialList.size(), false);
+  // collect new items and check the items to remove
+  for(int anInd = 0; anInd < aTranslationObjectRefs->size(); anInd++) {
+    std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aTranslationObjectRefs->value(anInd);
+    std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
+    std::vector<bool>::iterator aUsedIt = isUsed.begin();
+    for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
+      if (*anIt == aSelect->context()) {
+        *aUsedIt = true;
+        break;
+      }
+    if (anIt == anInitialList.end())
+      anAddition.push_back(aSelect->context());
+  }
+  // remove unused items
+  std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
+  std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
+  std::vector<bool>::iterator aUsedIter = isUsed.begin();
+  for (; aUsedIter != isUsed.end(); aUsedIter++) {
+    if (!(*aUsedIter)) {
+      aRefListOfShapes->remove(*anInitIter);
+      for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
+        aRefListOfTranslated->remove(*aTargetIter);
+        // remove the corresponding feature from the sketch
+        ResultConstructionPtr aRC =
+            std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
+        DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
+        FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
+        if (aFeature)
+          aDoc->removeFeature(aFeature);
+      }
+    } else {
+      for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
+        aTargetIter++;
+    }
+    if (anInitIter != anInitialList.end())
+      anInitIter++;
+  }
+  // change number of copies
+  if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
+    bool isAdd = aNbCopies > aCurrentNbCopies;
+    int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
+    int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
+    int ind = 0;
+
+    aTargetList = aRefListOfTranslated->list();
+    aTargetIter = aTargetList.begin();
+    ObjectPtr anObjToCopy = *aTargetIter;
+    while (aTargetIter != aTargetList.end()) {
+      aRefListOfTranslated->remove(*aTargetIter);
+      aTargetIter++;
+      ind++;
+      if (ind > aMinCopies && ind <=aMaxCopies) {
+        while (ind <= aMaxCopies) {
+          if (isAdd) {
+            // Add new shifted item
+            ObjectPtr anObject = copyFeature(anObjToCopy);
+            aTargetList.insert(aTargetIter, anObject);
+          } else {
+            // remove object
+            std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter;
+            ObjectPtr anObject = *(--aRemoveIt);
+            aTargetList.erase(aRemoveIt);
+            // remove the corresponding feature from the sketch
+            ResultConstructionPtr aRC =
+                std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
+            DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
+            FeaturePtr aFeature =  aDoc ? aDoc->feature(aRC) : FeaturePtr();
+            if (aFeature)
+              aDoc->removeFeature(aFeature);
+          }
+          ind++;
+        }
+        ind = 0;
+        if (aTargetIter != aTargetList.end())
+          anObjToCopy = *aTargetIter;
+      }
+    }
+
+    for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
+      aRefListOfTranslated->append(*aTargetIter);
+  }
+  // add new items
+  std::list<ResultPtr>::iterator anAddIter = anAddition.begin();
+  for (; anAddIter != anAddition.end(); anAddIter++) {
+    aRefListOfShapes->append(*anAddIter);
+    aRefListOfTranslated->append(*anAddIter);
+    for (int i = 0; i < aNbCopies; i++) {
+      ObjectPtr anObject = copyFeature(*anAddIter);
+      aRefListOfTranslated->append(anObject);
+    }
+  }
+
+  // Recalculate positions of features
+  aTargetList = aRefListOfTranslated->list();
+  aTargetIter = aTargetList.begin();
+  while (aTargetIter != aTargetList.end()) {
+    ObjectPtr anInitialObject = *aTargetIter++;
+    for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
+      shiftFeature(anInitialObject, *aTargetIter,
+          aShiftVec->x() * (i + 1), aShiftVec->y() * (i + 1));
+  }
+
+  // send events to update the sub-features by the solver
+  if (isUpdateFlushed)
+    Events_Loop::loop()->setFlushed(anUpdateEvent, true);
+}
+
+AISObjectPtr SketchPlugin_MultiTranslation::getAISObject(AISObjectPtr thePrevious)
+{
+  if (!sketch())
+    return thePrevious;
+
+  AISObjectPtr anAIS = thePrevious;
+  if (!anAIS) {
+// TODO:
+//    anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());
+  }
+  return anAIS;
+}
+
+ObjectPtr SketchPlugin_MultiTranslation::copyFeature(ObjectPtr theObject)
+{
+  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
+  FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
+  if (!aFeature || !aResult)
+    return ObjectPtr();
+
+  FeaturePtr aNewFeature = sketch()->addFeature(aFeature->getKind());
+  aFeature->data()->copyTo(aNewFeature->data());
+  aNewFeature->execute();
+
+  static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
+  ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
+
+  std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
+  const std::list<ResultPtr>& aResults = aNewFeature->results();
+  std::list<ResultPtr>::const_iterator anIt = aResults.begin();
+  for (; anIt != aResults.end(); anIt++) {
+    ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
+    if (!aRC) continue;
+    std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
+    if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
+        (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
+        (aShapeIn->isFace() && aShapeOut->isFace()))
+      return aRC;
+  }
+  return ObjectPtr();
+}
+
+void SketchPlugin_MultiTranslation::shiftFeature(
+    ObjectPtr theInitial, ObjectPtr theTarget, double theDeltaX, double theDeltaY)
+{
+  FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
+  FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
+
+  // block feature update
+  aTargetFeature->data()->blockSendAttributeUpdated(true);
+
+  std::list<AttributePtr> anInitAttrList =
+      anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr> aTargetAttrList =
+      aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
+  std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
+  for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
+    std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
+    std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
+    aPointTo->setValue(aPointFrom->x() + theDeltaX, aPointFrom->y() + theDeltaY);
+  }
+
+  // unblock feature update
+  aTargetFeature->data()->blockSendAttributeUpdated(false);
+}
diff --git a/src/SketchPlugin/SketchPlugin_MultiTranslation.h b/src/SketchPlugin/SketchPlugin_MultiTranslation.h
new file mode 100644 (file)
index 0000000..18e3c4a
--- /dev/null
@@ -0,0 +1,86 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
+
+// File:    SketchPlugin_MultiTranslation.h
+// Created: 21 Apr 2015
+// Author:  Artem ZHIDKOV
+
+#ifndef SketchPlugin_MultiTranslation_H_
+#define SketchPlugin_MultiTranslation_H_
+
+#include "SketchPlugin.h"
+#include <SketchPlugin_Sketch.h>
+#include "SketchPlugin_ConstraintBase.h"
+
+/** \class SketchPlugin_MultiTranslation
+ *  \ingroup Plugins
+ *  \brief Feature for creation of a number of shifted copies of a list of objects
+ *
+ *  This constraint has two attributes:
+ *  SketchPlugin_Constraint::ENTITY_A() for initial list of objects and
+ *  SketchPlugin_Constraint::ENTITY_B() for the list of created objects
+ *
+ *  The list of created objects contains a number of copies of each object given in
+ *  the NUMBER_OF_COPIES_ID() attribute plus 1 (the initial objects are stored into this
+ *  attribute too). At the start of the list, there are collected N copies
+ *  of first object from initial list, then N copies of second object etc.
+ */
+class SketchPlugin_MultiTranslation : public SketchPlugin_ConstraintBase
+{
+ public:
+  /// Multi-translation kind
+  inline static const std::string& ID()
+  {
+    static const std::string MY_CONSTRAINT_TRANSLATION_ID("SketchMultiTranslation");
+    return MY_CONSTRAINT_TRANSLATION_ID;
+  }
+  /// \brief Returns the kind of a feature
+  SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
+  {
+    static std::string MY_KIND = SketchPlugin_MultiTranslation::ID();
+    return MY_KIND;
+  }
+
+  /// List of objects to be mirrored
+  inline static const std::string& TRANSLATION_LIST_ID()
+  {
+    static const std::string MY_TRANSLATION_LIST_ID("MultiTranslationList");
+    return MY_TRANSLATION_LIST_ID;
+  }
+
+  /// Start point of translation
+  inline static const std::string& START_POINT_ID()
+  {
+    static const std::string MY_START_POINT_ID("MultiTranslationStartPoint");
+    return MY_START_POINT_ID;
+  }
+  /// End point of translation
+  inline static const std::string& END_POINT_ID()
+  {
+    static const std::string MY_END_POINT_ID("MultiTranslationEndPoint");
+    return MY_END_POINT_ID;
+  }
+  /// Number of translated objects
+  inline static const std::string& NUMBER_OF_COPIES_ID()
+  {
+    static const std::string MY_NUMBER_OF_COPIES_ID("MultiTranslationCopies");
+    return MY_NUMBER_OF_COPIES_ID;
+  }
+
+  /// \brief Creates a new part document if needed
+  SKETCHPLUGIN_EXPORT virtual void execute();
+
+  /// \brief Request for initialization of data model of the feature: adding all attributes
+  SKETCHPLUGIN_EXPORT virtual void initAttributes();
+
+  /// Returns the AIS preview
+  SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
+
+  /// \brief Use plugin manager for features creation
+  SketchPlugin_MultiTranslation();
+
+private:
+  ObjectPtr copyFeature(ObjectPtr theObject);
+  void shiftFeature(ObjectPtr theInitial, ObjectPtr theTarget, double theDeltaX, double theDeltaY);
+};
+
+#endif
index ff8824f81bbadf568c47ebe980d946d905311f52..904fe1e2200957c9fc4a255efc923214e7844e1a 100644 (file)
@@ -19,6 +19,8 @@
 #include <SketchPlugin_ConstraintRigid.h>
 #include <SketchPlugin_ConstraintTangent.h>
 #include <SketchPlugin_ConstraintVertical.h>
+#include <SketchPlugin_MultiRotation.h>
+#include <SketchPlugin_MultiTranslation.h>
 #include <SketchPlugin_Validators.h>
 #include <SketchPlugin_ExternalValidator.h>
 
@@ -63,6 +65,8 @@ SketchPlugin_Plugin::SketchPlugin_Plugin()
                               new SketchPlugin_MirrorAttrValidator);
   aFactory->registerValidator("SketchPlugin_CoincidenceAttr",
                               new SketchPlugin_CoincidenceAttrValidator);
+  aFactory->registerValidator("SketchPlugin_CopyValidator",
+                              new SketchPlugin_CopyValidator);
 
   // register this plugin
   ModelAPI_Session::get()->registerPlugin(this);
@@ -131,6 +135,10 @@ FeaturePtr SketchPlugin_Plugin::createFeature(string theFeatureID)
     return FeaturePtr(new SketchPlugin_ConstraintMirror);
   } else if (theFeatureID == SketchPlugin_ConstraintFillet::ID()) {
     return FeaturePtr(new SketchPlugin_ConstraintFillet);
+  } else if (theFeatureID == SketchPlugin_MultiTranslation::ID()) {
+    return FeaturePtr(new SketchPlugin_MultiTranslation);
+  } else if (theFeatureID == SketchPlugin_MultiRotation::ID()) {
+    return FeaturePtr(new SketchPlugin_MultiRotation);
   }
   // feature of such kind is not found
   return FeaturePtr();
index 674f9de12ee3be1107ddb2c399d67adb3ad86993..68857aa2623b8687ddeb06fb4aa5709950f87096 100644 (file)
@@ -280,3 +280,37 @@ bool SketchPlugin_CoincidenceAttrValidator::isValid(
   return false;
 }
 
+
+bool SketchPlugin_CopyValidator::isValid(
+  const AttributePtr& theAttribute, const std::list<std::string>& theArguments ) const
+{
+  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
+  AttributeSelectionListPtr aSelAttr = 
+    std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+  if (!aSelAttr)
+    return false;
+
+  AttributeRefListPtr aRefListOfInitial = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aFeature->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr aRefListOfCopied = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aFeature->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  std::list<ObjectPtr> anInitialObjects = aRefListOfInitial->list();
+  std::list<ObjectPtr> aCopiedObjects = aRefListOfCopied->list();
+
+  std::list<ObjectPtr>::iterator anObjIter;
+  for(int anInd = 0; anInd < aSelAttr->size(); anInd++) {
+    std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aSelAttr->value(anInd);
+    anObjIter = anInitialObjects.begin();
+    for (; anObjIter != anInitialObjects.end(); anObjIter++)
+      if (aSelect->context() == *anObjIter)
+        break;
+    if (anObjIter != anInitialObjects.end())
+      continue;
+    anObjIter = aCopiedObjects.begin();
+    for (; anObjIter != aCopiedObjects.end(); anObjIter++)
+      if (aSelect->context() == *anObjIter)
+        return false;
+  }
+  return true;
+}
+
index 82e0e1fce07eae3f0c64ac77a28b8457737d68f3..cd7a7d5336dba141f1df7bb9b44f8302130aad57 100644 (file)
@@ -109,4 +109,22 @@ class SketchPlugin_CoincidenceAttrValidator : public ModelAPI_AttributeValidator
                        const std::list<std::string>& theArguments) const;
 };
 
+
+/**\class SketchPlugin_CopyValidator
+ * \ingroup Validators
+ * \brief Validator for the constraints which create features.
+ *
+ * Applicable only for features, which creates another features. It verifies the produced
+ * features of current constraint don't become into the list of initial features
+ */
+class SketchPlugin_CopyValidator : public ModelAPI_AttributeValidator
+{
+ public:
+  //! returns true if attribute is valid
+  //! \param theAttribute the checked attribute
+  //! \param theArguments arguments of the attribute (not used)
+  virtual bool isValid(const AttributePtr& theAttribute,
+                       const std::list<std::string>& theArguments) const;
+};
+
 #endif
index 6474268be2fcd1c6a6cf2e5ac398ffc7ed49d7cb..13d388d38c85519366bcecd92f54b613ee0e2e5f 100644 (file)
@@ -5,7 +5,7 @@
     <group id="Basic">
       <feature
         id="Sketch"
-        nested="SketchPoint SketchLine SketchCircle SketchArc SketchConstraintLength SketchConstraintRadius SketchConstraintDistance SketchConstraintParallel SketchConstraintPerpendicular SketchConstraintRigid SketchConstraintHorizontal SketchConstraintVertical SketchConstraintEqual SketchConstraintTangent SketchConstraintFillet SketchConstraintCoincidence SketchConstraintMirror"
+        nested="SketchPoint SketchLine SketchCircle SketchArc SketchConstraintLength SketchConstraintRadius SketchConstraintDistance SketchConstraintParallel SketchConstraintPerpendicular SketchConstraintRigid SketchConstraintHorizontal SketchConstraintVertical SketchConstraintEqual SketchConstraintTangent SketchConstraintFillet SketchConstraintCoincidence SketchConstraintMirror SketchMultiRotation SketchMultiTranslation"
         when_nested="accept abort"
         title="Sketch"
         tooltip="Create sketch"
             <validator id="SketchPlugin_MirrorAttr" />
         </sketch_multi_selector>
       </feature>
+      <!--  SketchMultiTranslation  -->
+      <feature
+        id="SketchMultiTranslation"
+        title="Translation" 
+        tooltip="Make a number of shifted copies of a group of objects">
+        <sketch_multi_selector id="MultiTranslationList"
+            label="List of objects"
+            tooltip="Select list of translating objects"
+            type_choice="Edges"
+            use_external="true">
+          <validator id="SketchPlugin_CopyValidator" />
+        </sketch_multi_selector>
+        <groupbox title="Direction">
+          <sketch-2dpoint_selector
+              id="MultiTranslationStartPoint"
+              title="Start point"
+              tooltip="Start point of translation"/>
+          <sketch-2dpoint_selector
+              id="MultiTranslationEndPoint"
+              title="End point"
+              tooltip="Final point of translation"/>
+        </groupbox>
+        <doublevalue_editor id="MultiTranslationCopies"
+            label="Number of copies"
+            tooltip="Number of copies" 
+            default="1" min="1">
+          <validator id="GeomValidators_Positive"/>
+        </doublevalue_editor>
+      </feature>
+      <!--  SketchMultiRotation  -->
+      <feature
+        id="SketchMultiRotation"
+        title="Rotation" 
+        tooltip="Make a number of rotated copies of a group of objects">
+        <sketch_multi_selector id="MultiRotationList"
+            label="List of objects"
+            tooltip="Select list of rotating objects"
+            type_choice="Edges"
+            use_external="true">
+          <validator id="SketchPlugin_CopyValidator" />
+        </sketch_multi_selector>
+        <sketch-2dpoint_selector
+            id="MultiRotationCenter"
+            title="Center of rotation"
+            tooltip="Center of rotation"/>
+        <doublevalue_editor id="MultiRotationAngle"
+            label="Angle"
+            tooltip="Rotation angle"/>
+        <doublevalue_editor id="MultiRotationCopies"
+            label="Number of copies"
+            tooltip="Number of copies" 
+            default="1" min="1">
+          <validator id="GeomValidators_Positive"/>
+        </doublevalue_editor>
+      </feature>
     </group>
   </workbench>
 </plugin>