1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_ConstraintMirror.cpp
4 // Created: 17 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_ConstraintMirror.h"
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_Session.h>
15 #include <SketchPlugin_Line.h>
16 #include <SketchPlugin_Sketch.h>
18 #include <SketcherPrs_Factory.h>
20 #include <Config_PropManager.h>
22 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
26 void SketchPlugin_ConstraintMirror::initAttributes()
28 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
29 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::type());
30 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::type());
33 void SketchPlugin_ConstraintMirror::execute()
35 // Objects to be mirrored will be created here
36 std::shared_ptr<ModelAPI_Data> aData = data();
37 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
38 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
39 if (!aRefListOfShapes->isInitialized())
42 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
43 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
44 // Check consistency of initial list and mirrored list
45 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
46 std::list<ObjectPtr> aMirroredList = aRefListOfMirrored->list();
47 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
48 std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
49 int indFirstWrong = 0; // index of element starts difference in the lists
50 std::set<int> anInvalidInd; // list of indices of removed features
51 std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
52 for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
53 // Add features and store indices of objects to remove
54 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
55 aFeatureOut = aMirrorIter != aMirroredList.end() ?
56 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter) :
57 std::shared_ptr<SketchPlugin_Feature>();
60 break; // the lists are inconsistent
64 if (aMirrorIter != aMirroredList.end())
65 break; // the lists are inconsistent
66 // There is no mirrored object yet, create it
67 FeaturePtr aNewFeature = sketch()->addFeature(aFeatureIn->getKind());
68 aFeatureIn->data()->copyTo(aNewFeature->data());
69 aRefListOfMirrored->append(aNewFeature);
72 if (aFeatureIn->getKind() != aFeatureOut->getKind())
73 break; // the lists are inconsistent
74 if (!aFeatureIn->data()->isValid()) {
75 // initial feature was removed, delete it from lists
76 anInvalidInd.insert(indFirstWrong);
80 // Remove from the list objects already deleted before
81 std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
82 for ( ; anIt != anInvalidInd.rend(); anIt++) {
83 if (*anIt < indFirstWrong) indFirstWrong--;
84 aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
85 aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
87 // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
88 if (anInitIter != anInitialList.end()) {
89 while (aRefListOfMirrored->size() > indFirstWrong)
90 aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
91 // Create mirrored features instead of removed
92 anInitialList = aRefListOfShapes->list();
93 anInitIter = anInitialList.begin();
94 for (int i = 0; i < indFirstWrong; i++) anInitIter++;
95 for ( ; anInitIter != anInitialList.end(); anInitIter++) {
96 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
97 FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
98 aRefListOfMirrored->append(aNewFeature);
103 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
108 AISObjectPtr anAIS = thePrevious;
109 /// TODO: Equal constraint presentation should be put here