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()->
49 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
50 ModelAPI_Session::get()->validators()->
51 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
54 void SketchPlugin_MultiRotation::execute()
57 // it is possible, that this method is called before this feature has back reference to sketch
58 // in this case, the execute is performed after this is done
62 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
63 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
67 // Obtain center and angle of rotation
68 AttributeRefAttrPtr aCenter = data()->refattr(CENTER_ID());
69 if (!aCenter || !aCenter->isInitialized())
72 //double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
73 // attribute(ANGLE_ID()))->value();
75 // Convert angle to radians
76 //anAngle *= PI / 180.0;
78 // Wait all objects being created, then send update events
79 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
80 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
82 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
84 std::shared_ptr<ModelAPI_Data> aData = data();
85 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
86 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
87 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
88 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
89 int aCurrentNbCopies = aRefListOfShapes->size() ?
90 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
91 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
92 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
93 std::list<ObjectPtr> anAddition;
94 std::vector<bool> isUsed(anInitialList.size(), false);
95 // collect new items and check the items to remove
96 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
97 ObjectPtr anObject = aRotationObjectRefs->object(anInd);
98 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
99 std::vector<bool>::iterator aUsedIt = isUsed.begin();
100 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
101 if (*anIt == anObject) {
105 if (anIt == anInitialList.end())
106 anAddition.push_back(anObject);
108 // remove unused items
109 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
110 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
111 std::vector<bool>::iterator aUsedIter = isUsed.begin();
112 std::set<FeaturePtr> aFeaturesToBeRemoved;
113 for (; aUsedIter != isUsed.end(); aUsedIter++) {
115 aRefListOfShapes->remove(*anInitIter);
116 aRefListOfRotated->remove(*aTargetIter++);
117 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
118 i++, aTargetIter++) {
119 aRefListOfRotated->remove(*aTargetIter);
120 // remove the corresponding feature from the sketch
121 ResultConstructionPtr aRC =
122 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
123 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
124 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
126 aFeaturesToBeRemoved.insert(aFeature);
129 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
132 if (anInitIter != anInitialList.end())
135 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
136 // change number of copies
137 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
138 bool isAdd = aNbCopies > aCurrentNbCopies;
139 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
140 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
143 aTargetList = aRefListOfRotated->list();
144 aTargetIter = aTargetList.begin();
145 ObjectPtr anObjToCopy = *aTargetIter;
146 std::set<FeaturePtr> aFeaturesToBeRemoved;
147 while (aTargetIter != aTargetList.end()) {
148 aRefListOfRotated->remove(*aTargetIter);
151 if (ind > aMinCopies && ind <=aMaxCopies) {
152 while (ind <= aMaxCopies) {
154 // Add new shifted item
155 ObjectPtr anObject = copyFeature(anObjToCopy);
156 aTargetList.insert(aTargetIter, anObject);
159 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
160 ObjectPtr anObject = *aRemoveIt;
161 aTargetList.erase(aRemoveIt);
162 aRefListOfRotated->remove(anObject);
163 // remove the corresponding feature from the sketch
164 ResultConstructionPtr aRC =
165 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
166 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
167 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
169 aFeaturesToBeRemoved.insert(aFeature);
174 if (aTargetIter != aTargetList.end())
175 anObjToCopy = *aTargetIter;
178 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
180 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
181 aRefListOfRotated->append(*aTargetIter);
184 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
185 for (; anAddIter != anAddition.end(); anAddIter++) {
186 aRefListOfShapes->append(*anAddIter);
187 aRefListOfRotated->append(*anAddIter);
188 for (int i = 0; i < aNbCopies; i++) {
189 ObjectPtr anObject = copyFeature(*anAddIter);
190 aRefListOfRotated->append(anObject);
194 //// if (fabs(anAngle) > 1.e-12) {
195 //// // Recalculate positions of features
196 //// aTargetList = aRefListOfRotated->list();
197 //// aTargetIter = aTargetList.begin();
198 //// while (aTargetIter != aTargetList.end()) {
199 //// ObjectPtr anInitialObject = *aTargetIter++;
200 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
201 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
205 // send events to update the sub-features by the solver
207 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
210 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
215 AISObjectPtr anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane(),
220 void SketchPlugin_MultiRotation::erase()
222 static Events_Loop* aLoop = Events_Loop::loop();
223 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
225 // Set copy attribute to false on all copied features.
226 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
227 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
228 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
229 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
231 if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
232 for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
233 ObjectPtr anObject = aRefListOfRotated->object(anIndex);
234 if(aRefListOfShapes->isInList(anObject)) {
235 // Don't modify attribute of original features, just skip.
239 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
241 FeaturePtr aFeature = aRes->document()->feature(aRes);
243 AttributeBooleanPtr aBooleanAttr =
244 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
245 if(aBooleanAttr.get()) {
246 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
247 aBooleanAttr->setValue(false);
248 // Redisplay object as it is not copy anymore.
249 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
257 SketchPlugin_ConstraintBase::erase();
260 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
262 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
263 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
264 if (!aFeature || !aResult)
267 FeaturePtr aNewFeature =
268 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
269 aNewFeature->execute();
271 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
272 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
274 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
275 const std::list<ResultPtr>& aResults = aNewFeature->results();
276 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
277 for (; anIt != aResults.end(); anIt++) {
278 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
280 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
281 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
282 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
283 (aShapeIn->isFace() && aShapeOut->isFace()))
289 /*void SketchPlugin_MultiRotation::rotateFeature(
290 ObjectPtr theInitial, ObjectPtr theTarget,
291 double theCenterX, double theCenterY, double theAngle)
293 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
294 double cosA = std::cos(theAngle);
295 double sinA = std::sin(theAngle);
297 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
298 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
300 // block feature update
301 aTargetFeature->data()->blockSendAttributeUpdated(true);
303 std::list<AttributePtr> anInitAttrList =
304 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
305 std::list<AttributePtr> aTargetAttrList =
306 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
307 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
308 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
309 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
310 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
311 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
312 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
313 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
314 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
315 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
316 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
317 double dx = cosA * aDir->x() - sinA * aDir->y();
318 double dy = sinA * aDir->x() + cosA * aDir->y();
319 aPnt->setX(aCenter->x() + dx);
320 aPnt->setY(aCenter->y() + dy);
322 aPointTo->setValue(aPnt->x(), aPnt->y());
325 // unblock feature update
326 aTargetFeature->data()->blockSendAttributeUpdated(false);
330 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
332 if (theID == ROTATION_LIST_ID()) {
333 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
334 if (aRotationObjectRefs->size() == 0) {
335 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
339 // Clear list of objects
340 AttributeRefListPtr aRefListOfRotated = reflist(SketchPlugin_Constraint::ENTITY_B());
341 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
342 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
343 std::set<FeaturePtr> aFeaturesToBeRemoved;
344 while (aTargetIter != aTargetList.end()) {
346 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
347 aRefListOfRotated->remove(*aTargetIter);
348 // remove the corresponding feature from the sketch
349 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
351 aFeaturesToBeRemoved.insert(aFeature);
354 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
356 aRefListOfRotated->clear();
357 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();