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>
22 #include <GeomAPI_Pnt2d.h>
23 #include <GeomAPI_XY.h>
25 #include <SketcherPrs_Factory.h>
29 #define PI 3.1415926535897932
31 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
35 void SketchPlugin_MultiRotation::initAttributes()
37 data()->addAttribute(CENTER_ID(), ModelAPI_AttributeRefAttr::typeId());
39 data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
40 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
41 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
42 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
43 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
44 data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
45 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
46 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
49 void SketchPlugin_MultiRotation::execute()
52 // it is possible, that this method is called before this feature has back reference to sketch
53 // in this case, the execute is performed after this is done
57 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
58 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
62 // Obtain center and angle of rotation
63 AttributeRefAttrPtr aCenter = data()->refattr(CENTER_ID());
64 if (!aCenter || !aCenter->isInitialized())
67 //double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
68 // attribute(ANGLE_ID()))->value();
70 // Convert angle to radians
71 //anAngle *= PI / 180.0;
73 // Wait all objects being created, then send update events
74 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
75 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
77 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
79 std::shared_ptr<ModelAPI_Data> aData = data();
80 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
81 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
82 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
83 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
84 int aCurrentNbCopies = aRefListOfShapes->size() ?
85 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
86 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
87 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
88 std::list<ObjectPtr> anAddition;
89 std::vector<bool> isUsed(anInitialList.size(), false);
90 // collect new items and check the items to remove
91 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
92 ObjectPtr anObject = aRotationObjectRefs->object(anInd);
93 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
94 std::vector<bool>::iterator aUsedIt = isUsed.begin();
95 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
96 if (*anIt == anObject) {
100 if (anIt == anInitialList.end())
101 anAddition.push_back(anObject);
103 // remove unused items
104 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
105 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
106 std::vector<bool>::iterator aUsedIter = isUsed.begin();
107 for (; aUsedIter != isUsed.end(); aUsedIter++) {
109 aRefListOfShapes->remove(*anInitIter);
110 aRefListOfRotated->remove(*aTargetIter++);
111 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
112 aRefListOfRotated->remove(*aTargetIter);
113 // remove the corresponding feature from the sketch
114 ResultConstructionPtr aRC =
115 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
116 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
117 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
119 aDoc->removeFeature(aFeature);
122 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
125 if (anInitIter != anInitialList.end())
128 // change number of copies
129 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
130 bool isAdd = aNbCopies > aCurrentNbCopies;
131 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
132 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
135 aTargetList = aRefListOfRotated->list();
136 aTargetIter = aTargetList.begin();
137 ObjectPtr anObjToCopy = *aTargetIter;
138 while (aTargetIter != aTargetList.end()) {
139 aRefListOfRotated->remove(*aTargetIter);
142 if (ind > aMinCopies && ind <=aMaxCopies) {
143 while (ind <= aMaxCopies) {
145 // Add new shifted item
146 ObjectPtr anObject = copyFeature(anObjToCopy);
147 aTargetList.insert(aTargetIter, anObject);
150 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
151 ObjectPtr anObject = *aRemoveIt;
152 aTargetList.erase(aRemoveIt);
153 aRefListOfRotated->remove(anObject);
154 // remove the corresponding feature from the sketch
155 ResultConstructionPtr aRC =
156 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
157 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
158 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
160 aDoc->removeFeature(aFeature);
165 if (aTargetIter != aTargetList.end())
166 anObjToCopy = *aTargetIter;
170 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
171 aRefListOfRotated->append(*aTargetIter);
174 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
175 for (; anAddIter != anAddition.end(); anAddIter++) {
176 aRefListOfShapes->append(*anAddIter);
177 aRefListOfRotated->append(*anAddIter);
178 for (int i = 0; i < aNbCopies; i++) {
179 ObjectPtr anObject = copyFeature(*anAddIter);
180 aRefListOfRotated->append(anObject);
184 //// if (fabs(anAngle) > 1.e-12) {
185 //// // Recalculate positions of features
186 //// aTargetList = aRefListOfRotated->list();
187 //// aTargetIter = aTargetList.begin();
188 //// while (aTargetIter != aTargetList.end()) {
189 //// ObjectPtr anInitialObject = *aTargetIter++;
190 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
191 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
195 // send events to update the sub-features by the solver
197 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
200 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
205 AISObjectPtr anAIS = thePrevious;
207 anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
213 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
215 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
216 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
217 if (!aFeature || !aResult)
220 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
221 aNewFeature->execute();
223 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
224 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
226 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
227 const std::list<ResultPtr>& aResults = aNewFeature->results();
228 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
229 for (; anIt != aResults.end(); anIt++) {
230 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
232 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
233 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
234 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
235 (aShapeIn->isFace() && aShapeOut->isFace()))
241 /*void SketchPlugin_MultiRotation::rotateFeature(
242 ObjectPtr theInitial, ObjectPtr theTarget,
243 double theCenterX, double theCenterY, double theAngle)
245 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
246 double cosA = std::cos(theAngle);
247 double sinA = std::sin(theAngle);
249 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
250 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
252 // block feature update
253 aTargetFeature->data()->blockSendAttributeUpdated(true);
255 std::list<AttributePtr> anInitAttrList =
256 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
257 std::list<AttributePtr> aTargetAttrList =
258 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
259 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
260 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
261 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
262 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
263 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
264 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
265 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
266 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
267 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
268 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
269 double dx = cosA * aDir->x() - sinA * aDir->y();
270 double dy = sinA * aDir->x() + cosA * aDir->y();
271 aPnt->setX(aCenter->x() + dx);
272 aPnt->setY(aCenter->y() + dy);
274 aPointTo->setValue(aPnt->x(), aPnt->y());
277 // unblock feature update
278 aTargetFeature->data()->blockSendAttributeUpdated(false);
282 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
284 if (theID == ROTATION_LIST_ID()) {
285 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
286 if (aRotationObjectRefs->size() == 0) {
287 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
291 // Clear list of objects
292 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
293 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
294 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
295 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
296 while (aTargetIter != aTargetList.end()) {
298 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
299 aRefListOfRotated->remove(*aTargetIter);
300 // remove the corresponding feature from the sketch
301 ResultConstructionPtr aRC =
302 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
303 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
304 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
306 aDoc->removeFeature(aFeature);
309 aRefListOfRotated->clear();
310 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
311 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
312 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
313 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();