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_AttributeDouble.h>
12 #include <ModelAPI_AttributeString.h>
13 #include <ModelAPI_AttributeInteger.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_ResultConstruction.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_Events.h>
18 #include <ModelAPI_Session.h>
19 #include <ModelAPI_Validator.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_XY.h>
24 #include <SketcherPrs_Factory.h>
28 #define PI 3.1415926535897932
30 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
35 void SketchPlugin_MultiRotation::initAttributes()
37 data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::typeId());
39 data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
40 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
41 data()->addAttribute(ANGLE_FULL_ID(), ModelAPI_AttributeDouble::typeId());
42 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
43 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
44 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
45 data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
46 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
47 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
50 void SketchPlugin_MultiRotation::execute()
53 // it is possible, that this method is called before this feature has back reference to sketch
54 // in this case, the execute is performed after this is done
58 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
59 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
63 // Obtain center and angle of rotation
64 std::shared_ptr<GeomDataAPI_Point2D> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
65 attribute(CENTER_ID()));
66 if (!aCenter || !aCenter->isInitialized())
69 if (attribute(ANGLE_ID())->isInitialized() && !attribute(ANGLE_FULL_ID())->isInitialized()) {
71 SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_ID()), attribute(ANGLE_FULL_ID()),
76 // make a visible points
77 SketchPlugin_Sketch::createPoint2DResult(this, sketch(), CENTER_ID(), 0);
79 double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
80 attribute(ANGLE_ID()))->value();
82 // Convert angle to radians
83 anAngle *= PI / 180.0;
85 // Wait all objects being created, then send update events
86 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
87 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
89 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
91 std::shared_ptr<ModelAPI_Data> aData = data();
92 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
93 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
94 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
95 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
96 int aCurrentNbCopies = aRefListOfShapes->size() ?
97 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
98 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
99 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
100 std::list<ObjectPtr> anAddition;
101 std::vector<bool> isUsed(anInitialList.size(), false);
102 // collect new items and check the items to remove
103 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
104 ObjectPtr anObject = aRotationObjectRefs->object(anInd);
105 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
106 std::vector<bool>::iterator aUsedIt = isUsed.begin();
107 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
108 if (*anIt == anObject) {
112 if (anIt == anInitialList.end())
113 anAddition.push_back(anObject);
115 // remove unused items
116 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
117 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
118 std::vector<bool>::iterator aUsedIter = isUsed.begin();
119 for (; aUsedIter != isUsed.end(); aUsedIter++) {
121 aRefListOfShapes->remove(*anInitIter);
122 aRefListOfRotated->remove(*aTargetIter++);
123 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
124 aRefListOfRotated->remove(*aTargetIter);
125 // remove the corresponding feature from the sketch
126 ResultConstructionPtr aRC =
127 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
128 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
129 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
131 aDoc->removeFeature(aFeature);
134 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
137 if (anInitIter != anInitialList.end())
140 // change number of copies
141 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
142 bool isAdd = aNbCopies > aCurrentNbCopies;
143 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
144 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
147 aTargetList = aRefListOfRotated->list();
148 aTargetIter = aTargetList.begin();
149 ObjectPtr anObjToCopy = *aTargetIter;
150 while (aTargetIter != aTargetList.end()) {
151 aRefListOfRotated->remove(*aTargetIter);
154 if (ind > aMinCopies && ind <=aMaxCopies) {
155 while (ind <= aMaxCopies) {
157 // Add new shifted item
158 ObjectPtr anObject = copyFeature(anObjToCopy);
159 aTargetList.insert(aTargetIter, anObject);
162 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
163 ObjectPtr anObject = *aRemoveIt;
164 aTargetList.erase(aRemoveIt);
165 aRefListOfRotated->remove(anObject);
166 // remove the corresponding feature from the sketch
167 ResultConstructionPtr aRC =
168 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
169 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
170 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
172 aDoc->removeFeature(aFeature);
177 if (aTargetIter != aTargetList.end())
178 anObjToCopy = *aTargetIter;
182 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
183 aRefListOfRotated->append(*aTargetIter);
186 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
187 for (; anAddIter != anAddition.end(); anAddIter++) {
188 aRefListOfShapes->append(*anAddIter);
189 aRefListOfRotated->append(*anAddIter);
190 for (int i = 0; i < aNbCopies; i++) {
191 ObjectPtr anObject = copyFeature(*anAddIter);
192 aRefListOfRotated->append(anObject);
196 //// if (fabs(anAngle) > 1.e-12) {
197 //// // Recalculate positions of features
198 //// aTargetList = aRefListOfRotated->list();
199 //// aTargetIter = aTargetList.begin();
200 //// while (aTargetIter != aTargetList.end()) {
201 //// ObjectPtr anInitialObject = *aTargetIter++;
202 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
203 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
207 // send events to update the sub-features by the solver
209 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
212 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
217 AISObjectPtr anAIS = thePrevious;
219 anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane());
225 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
227 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
228 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
229 if (!aFeature || !aResult)
232 FeaturePtr aNewFeature = SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch());
233 aNewFeature->execute();
235 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
236 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
238 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
239 const std::list<ResultPtr>& aResults = aNewFeature->results();
240 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
241 for (; anIt != aResults.end(); anIt++) {
242 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
244 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
245 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
246 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
247 (aShapeIn->isFace() && aShapeOut->isFace()))
253 void SketchPlugin_MultiRotation::rotateFeature(
254 ObjectPtr theInitial, ObjectPtr theTarget,
255 double theCenterX, double theCenterY, double theAngle)
257 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
258 double cosA = std::cos(theAngle);
259 double sinA = std::sin(theAngle);
261 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
262 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
264 // block feature update
265 aTargetFeature->data()->blockSendAttributeUpdated(true);
267 std::list<AttributePtr> anInitAttrList =
268 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
269 std::list<AttributePtr> aTargetAttrList =
270 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
271 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
272 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
273 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
274 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
275 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
276 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
277 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
278 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
279 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
280 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
281 double dx = cosA * aDir->x() - sinA * aDir->y();
282 double dy = sinA * aDir->x() + cosA * aDir->y();
283 aPnt->setX(aCenter->x() + dx);
284 aPnt->setY(aCenter->y() + dy);
286 aPointTo->setValue(aPnt->x(), aPnt->y());
289 // unblock feature update
290 aTargetFeature->data()->blockSendAttributeUpdated(false);
294 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
296 if (theID == ROTATION_LIST_ID()) {
297 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
298 if (aRotationObjectRefs->size() == 0) {
299 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
303 // Clear list of objects
304 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
305 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
306 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
307 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
308 while (aTargetIter != aTargetList.end()) {
310 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
311 aRefListOfRotated->remove(*aTargetIter);
312 // remove the corresponding feature from the sketch
313 ResultConstructionPtr aRC =
314 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
315 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
316 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
318 aDoc->removeFeature(aFeature);
321 aRefListOfRotated->clear();
322 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
323 data()->attribute(SketchPlugin_Constraint::ENTITY_A()))->clear();
324 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
325 data()->attribute(SketchPlugin_Constraint::ENTITY_B()))->clear();
328 else if (theID == ANGLE_ID() && !myBlockAngle) {
329 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
332 SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_ID()), attribute(ANGLE_FULL_ID()),
334 myBlockAngle = false;
335 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
338 else if (theID == ANGLE_FULL_ID() && !myBlockAngle) {
339 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
342 SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_FULL_ID()), attribute(ANGLE_ID()),
344 myBlockAngle = false;
345 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
348 else if (theID == NUMBER_OF_OBJECTS_ID()) {
349 if (attribute(NUMBER_OF_OBJECTS_ID())->isInitialized() &&
350 attribute(ANGLE_ID())->isInitialized() &&
351 attribute(ANGLE_TYPE())->isInitialized()) {
352 AttributeStringPtr aMethodTypeAttr = string(ANGLE_TYPE());
353 std::string aMethodType = aMethodTypeAttr->value();
354 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
357 if (aMethodType == "SingleAngle")
358 SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_ID()), attribute(ANGLE_FULL_ID()),
361 SketchPlugin_Tools::updateMultiAttribute(attribute(ANGLE_FULL_ID()), attribute(ANGLE_ID()),
364 myBlockAngle = false;
365 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));