1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_MultiRotation.cpp
4 // Created: 21 Apr 2015
5 // Author: Artem ZHIDKOV
7 #include "SketchPlugin_MultiRotation.h"
8 #include "SketchPlugin_Tools.h"
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_AttributeInteger.h>
15 #include <ModelAPI_Data.h>
16 #include <ModelAPI_ResultConstruction.h>
17 #include <ModelAPI_AttributeRefList.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Session.h>
20 #include <ModelAPI_Validator.h>
21 #include <ModelAPI_Tools.h>
23 #include <SketchPlugin_SketchEntity.h>
25 #include <GeomAPI_Pnt2d.h>
26 #include <GeomAPI_XY.h>
28 #include <SketcherPrs_Factory.h>
32 #define PI 3.1415926535897932
34 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
38 void SketchPlugin_MultiRotation::initAttributes()
40 data()->addAttribute(CENTER_ID(), ModelAPI_AttributeRefAttr::typeId());
42 data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
43 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
44 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
45 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
46 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
47 data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
48 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
49 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
52 void SketchPlugin_MultiRotation::execute()
55 // it is possible, that this method is called before this feature has back reference to sketch
56 // in this case, the execute is performed after this is done
60 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
61 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
65 // Obtain center and angle of rotation
66 AttributeRefAttrPtr aCenter = data()->refattr(CENTER_ID());
67 if (!aCenter || !aCenter->isInitialized())
70 //double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
71 // attribute(ANGLE_ID()))->value();
73 // Convert angle to radians
74 //anAngle *= PI / 180.0;
76 // Wait all objects being created, then send update events
77 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
78 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
80 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
82 std::shared_ptr<ModelAPI_Data> aData = data();
83 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
84 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
85 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
86 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
87 int aCurrentNbCopies = aRefListOfShapes->size() ?
88 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
89 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
90 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
91 std::list<ObjectPtr> anAddition;
92 std::vector<bool> isUsed(anInitialList.size(), false);
93 // collect new items and check the items to remove
94 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
95 ObjectPtr anObject = aRotationObjectRefs->object(anInd);
96 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
97 std::vector<bool>::iterator aUsedIt = isUsed.begin();
98 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
99 if (*anIt == anObject) {
103 if (anIt == anInitialList.end())
104 anAddition.push_back(anObject);
106 // remove unused items
107 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
108 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
109 std::vector<bool>::iterator aUsedIter = isUsed.begin();
110 std::set<FeaturePtr> aFeaturesToBeRemoved;
111 for (; aUsedIter != isUsed.end(); aUsedIter++) {
113 aRefListOfShapes->remove(*anInitIter);
114 aRefListOfRotated->remove(*aTargetIter++);
115 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
116 aRefListOfRotated->remove(*aTargetIter);
117 // remove the corresponding feature from the sketch
118 ResultConstructionPtr aRC =
119 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
120 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
121 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
123 aFeaturesToBeRemoved.insert(aFeature);
124 //aDoc->removeFeature(aFeature);
127 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
130 if (anInitIter != anInitialList.end())
133 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
134 // change number of copies
135 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
136 bool isAdd = aNbCopies > aCurrentNbCopies;
137 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
138 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
141 aTargetList = aRefListOfRotated->list();
142 aTargetIter = aTargetList.begin();
143 ObjectPtr anObjToCopy = *aTargetIter;
144 std::set<FeaturePtr> aFeaturesToBeRemoved;
145 while (aTargetIter != aTargetList.end()) {
146 aRefListOfRotated->remove(*aTargetIter);
149 if (ind > aMinCopies && ind <=aMaxCopies) {
150 while (ind <= aMaxCopies) {
152 // Add new shifted item
153 ObjectPtr anObject = copyFeature(anObjToCopy);
154 aTargetList.insert(aTargetIter, anObject);
157 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
158 ObjectPtr anObject = *aRemoveIt;
159 aTargetList.erase(aRemoveIt);
160 aRefListOfRotated->remove(anObject);
161 // remove the corresponding feature from the sketch
162 ResultConstructionPtr aRC =
163 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
164 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
165 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
167 aFeaturesToBeRemoved.insert(aFeature);
168 //aDoc->removeFeature(aFeature);
173 if (aTargetIter != aTargetList.end())
174 anObjToCopy = *aTargetIter;
177 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
179 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
180 aRefListOfRotated->append(*aTargetIter);
183 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
184 for (; anAddIter != anAddition.end(); anAddIter++) {
185 aRefListOfShapes->append(*anAddIter);
186 aRefListOfRotated->append(*anAddIter);
187 for (int i = 0; i < aNbCopies; i++) {
188 ObjectPtr anObject = copyFeature(*anAddIter);
189 aRefListOfRotated->append(anObject);
193 //// if (fabs(anAngle) > 1.e-12) {
194 //// // Recalculate positions of features
195 //// aTargetList = aRefListOfRotated->list();
196 //// aTargetIter = aTargetList.begin();
197 //// while (aTargetIter != aTargetList.end()) {
198 //// ObjectPtr anInitialObject = *aTargetIter++;
199 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
200 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
204 // send events to update the sub-features by the solver
206 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
209 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
214 AISObjectPtr anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane(),
219 void SketchPlugin_MultiRotation::erase()
221 static Events_Loop* aLoop = Events_Loop::loop();
222 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
224 // Set copy attribute to false on all copied features.
225 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
226 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
227 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
228 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
230 if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
231 for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
232 ObjectPtr anObject = aRefListOfRotated->object(anIndex);
233 if(aRefListOfShapes->isInList(anObject)) {
234 // Don't modify attribute of original features, just skip.
238 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
240 FeaturePtr aFeature = aRes->document()->feature(aRes);
242 AttributeBooleanPtr aBooleanAttr = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
243 if(aBooleanAttr.get()) {
244 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
245 aBooleanAttr->setValue(false);
246 // Redisplay object as it is not copy anymore.
247 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
255 SketchPlugin_ConstraintBase::erase();
258 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
260 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
261 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
262 if (!aFeature || !aResult)
265 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
266 aNewFeature->execute();
268 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
269 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
271 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
272 const std::list<ResultPtr>& aResults = aNewFeature->results();
273 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
274 for (; anIt != aResults.end(); anIt++) {
275 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
277 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
278 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
279 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
280 (aShapeIn->isFace() && aShapeOut->isFace()))
286 /*void SketchPlugin_MultiRotation::rotateFeature(
287 ObjectPtr theInitial, ObjectPtr theTarget,
288 double theCenterX, double theCenterY, double theAngle)
290 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
291 double cosA = std::cos(theAngle);
292 double sinA = std::sin(theAngle);
294 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
295 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
297 // block feature update
298 aTargetFeature->data()->blockSendAttributeUpdated(true);
300 std::list<AttributePtr> anInitAttrList =
301 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
302 std::list<AttributePtr> aTargetAttrList =
303 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
304 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
305 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
306 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
307 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
308 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
309 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
310 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
311 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
312 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
313 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
314 double dx = cosA * aDir->x() - sinA * aDir->y();
315 double dy = sinA * aDir->x() + cosA * aDir->y();
316 aPnt->setX(aCenter->x() + dx);
317 aPnt->setY(aCenter->y() + dy);
319 aPointTo->setValue(aPnt->x(), aPnt->y());
322 // unblock feature update
323 aTargetFeature->data()->blockSendAttributeUpdated(false);
327 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
329 if (theID == ROTATION_LIST_ID()) {
330 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
331 if (aRotationObjectRefs->size() == 0) {
332 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
336 // Clear list of objects
337 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
338 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
339 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
340 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
341 std::set<FeaturePtr> aFeaturesToBeRemoved;
342 while (aTargetIter != aTargetList.end()) {
344 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
345 aRefListOfRotated->remove(*aTargetIter);
346 // remove the corresponding feature from the sketch
347 ResultConstructionPtr aRC =
348 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
349 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
350 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
352 aFeaturesToBeRemoved.insert(aFeature);
353 //aDoc->removeFeature(aFeature);
356 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
358 aRefListOfRotated->clear();
359 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
360 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
361 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
362 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();