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_AttributeSelectionList.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Validator.h>
18 #include <SketchPlugin_Line.h>
19 #include <SketchPlugin_Sketch.h>
21 #include <SketcherPrs_Factory.h>
23 #include <Config_PropManager.h>
25 SketchPlugin_ConstraintMirror::SketchPlugin_ConstraintMirror()
29 void SketchPlugin_ConstraintMirror::initAttributes()
31 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
32 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
33 data()->addAttribute(SketchPlugin_Constraint::ENTITY_C(), ModelAPI_AttributeRefList::typeId());
34 AttributeSelectionListPtr aSelection =
35 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
36 SketchPlugin_ConstraintMirror::MIRROR_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
37 aSelection->setSelectionType("EDGE");
38 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
39 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_C());
42 void SketchPlugin_ConstraintMirror::execute()
44 AttributeSelectionListPtr aMirrorObjectRefs =
45 selectionList(SketchPlugin_ConstraintMirror::MIRROR_LIST_ID());
47 std::shared_ptr<ModelAPI_Data> aData = data();
48 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
49 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
50 AttributeRefListPtr aRefListOfMirrored = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
51 aData->attribute(SketchPlugin_Constraint::ENTITY_C()));
52 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
53 std::list<ObjectPtr> aMirroredList = aRefListOfMirrored->list();
54 std::vector<bool> isUsed(anInitialList.size(), false);
55 // add new items to the list
56 for(int anInd = 0; anInd < aMirrorObjectRefs->size(); anInd++) {
57 std::shared_ptr<ModelAPI_AttributeSelection> aSelect = aMirrorObjectRefs->value(anInd);
58 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
59 std::vector<bool>::iterator aUsedIt = isUsed.begin();
60 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
61 if (*anIt == aSelect->context()) {
65 if (anIt == anInitialList.end())
66 aRefListOfShapes->append(aSelect->context());
68 // remove unused items
69 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
70 std::list<ObjectPtr>::iterator aMirrorIter = aMirroredList.begin();
71 std::vector<bool>::iterator aUsedIter = isUsed.begin();
72 for (; aUsedIter != isUsed.end(); aUsedIter++) {
74 aRefListOfShapes->remove(*anInitIter);
75 if (aMirrorIter != aMirroredList.end()) {
76 aRefListOfMirrored->remove(*aMirrorIter);
77 // remove the corresponding feature from the sketch
78 ResultConstructionPtr aRC =
79 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aMirrorIter);
80 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
81 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
83 aDoc->removeFeature(aFeature);
86 if (anInitIter != anInitialList.end())
88 if (aMirrorIter != aMirroredList.end())
92 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
94 // Check consistency of initial list and mirrored list
95 anInitialList = aRefListOfShapes->list();
96 anInitIter = anInitialList.begin();
97 aMirrorIter = aMirroredList.begin();
98 int indFirstWrong = 0; // index of element starts difference in the lists
99 std::set<int> anInvalidInd; // list of indices of removed features
100 std::shared_ptr<SketchPlugin_Feature> aFeatureIn, aFeatureOut;
101 for ( ; anInitIter != anInitialList.end(); anInitIter++, indFirstWrong++) {
102 // Add features and store indices of objects to remove
103 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
104 ResultConstructionPtr aRCIn;
106 aRCIn = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
108 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(
109 aRCIn->document()->feature(aRCIn));
111 if (aMirrorIter == aMirroredList.end())
112 aFeatureOut = std::shared_ptr<SketchPlugin_Feature>();
114 aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(*aMirrorIter);
116 ResultConstructionPtr aRC =
117 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anInitIter);
119 aFeatureOut = std::dynamic_pointer_cast<SketchPlugin_Feature>(
120 aRC->document()->feature(aRC));
125 break; // the lists are inconsistent
129 if (aMirrorIter != aMirroredList.end())
130 break; // the lists are inconsistent
131 // There is no mirrored object yet, create it
132 FeaturePtr aNewFeature = sketch()->addFeature(aFeatureIn->getKind());
133 aFeatureIn->data()->copyTo(aNewFeature->data());
134 aNewFeature->execute();
135 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
137 std::shared_ptr<GeomAPI_Shape> aShapeIn = aRCIn->shape();
138 const std::list<ResultPtr>& aResults = aNewFeature->results();
139 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
140 for (; anIt != aResults.end(); anIt++) {
141 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
143 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
144 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
145 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
146 (aShapeIn->isFace() && aShapeOut->isFace()))
147 aRefListOfMirrored->append(aRC);
151 if (aFeatureIn->getKind() != aFeatureOut->getKind())
152 break; // the lists are inconsistent
153 if (!aFeatureIn->data()->isValid()) {
154 // initial feature was removed, delete it from lists
155 anInvalidInd.insert(indFirstWrong);
159 // Remove from the list objects already deleted before
160 std::set<int>::reverse_iterator anIt = anInvalidInd.rbegin();
161 for ( ; anIt != anInvalidInd.rend(); anIt++) {
162 if (*anIt < indFirstWrong) indFirstWrong--;
163 aRefListOfShapes->remove(aRefListOfShapes->object(*anIt));
164 aRefListOfMirrored->remove(aRefListOfMirrored->object(*anIt));
166 // If the lists inconsistent, remove all objects from mirrored list starting from indFirstWrong
167 if (anInitIter != anInitialList.end()) {
168 while (aRefListOfMirrored->size() > indFirstWrong)
169 aRefListOfMirrored->remove(aRefListOfMirrored->object(indFirstWrong));
170 // Create mirrored features instead of removed
171 anInitialList = aRefListOfShapes->list();
172 anInitIter = anInitialList.begin();
173 for (int i = 0; i < indFirstWrong; i++) anInitIter++;
174 for ( ; anInitIter != anInitialList.end(); anInitIter++) {
175 aFeatureIn = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anInitIter);
176 FeaturePtr aNewFeature = aFeatureIn->document()->addFeature(aFeatureIn->getKind());
177 aRefListOfMirrored->append(aNewFeature);
182 AISObjectPtr SketchPlugin_ConstraintMirror::getAISObject(AISObjectPtr thePrevious)
187 AISObjectPtr anAIS = thePrevious;
189 anAIS = SketcherPrs_Factory::mirrorConstraint(this, sketch()->coordinatePlane());