1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_MultiRotation.h"
22 #include "SketchPlugin_Tools.h"
24 #include <GeomDataAPI_Point2D.h>
25 #include <ModelAPI_AttributeRefAttr.h>
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_AttributeInteger.h>
29 #include <ModelAPI_Data.h>
30 #include <ModelAPI_ResultConstruction.h>
31 #include <ModelAPI_AttributeRefList.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Validator.h>
35 #include <ModelAPI_Tools.h>
37 #include <SketchPlugin_SketchEntity.h>
39 #include <GeomAPI_Pnt2d.h>
40 #include <GeomAPI_XY.h>
42 #include <SketcherPrs_Factory.h>
46 #define PI 3.1415926535897932
48 SketchPlugin_MultiRotation::SketchPlugin_MultiRotation()
52 void SketchPlugin_MultiRotation::initAttributes()
54 data()->addAttribute(CENTER_ID(), ModelAPI_AttributeRefAttr::typeId());
56 data()->addAttribute(ANGLE_TYPE(), ModelAPI_AttributeString::typeId());
57 data()->addAttribute(ANGLE_ID(), ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(NUMBER_OF_OBJECTS_ID(), ModelAPI_AttributeInteger::typeId());
59 data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefList::typeId());
60 data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefList::typeId());
61 data()->addAttribute(ROTATION_LIST_ID(), ModelAPI_AttributeRefList::typeId());
62 ModelAPI_Session::get()->validators()->
63 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_A());
64 ModelAPI_Session::get()->validators()->
65 registerNotObligatory(getKind(), SketchPlugin_Constraint::ENTITY_B());
68 void SketchPlugin_MultiRotation::execute()
71 // it is possible, that this method is called before this feature has back reference to sketch
72 // in this case, the execute is performed after this is done
76 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
77 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value() - 1;
81 // Obtain center and angle of rotation
82 AttributeRefAttrPtr aCenter = data()->refattr(CENTER_ID());
83 if (!aCenter || !aCenter->isInitialized())
86 //double anAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
87 // attribute(ANGLE_ID()))->value();
89 // Convert angle to radians
90 //anAngle *= PI / 180.0;
92 // Wait all objects being created, then send update events
93 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
94 bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
96 Events_Loop::loop()->setFlushed(anUpdateEvent, false);
98 std::shared_ptr<ModelAPI_Data> aData = data();
99 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
100 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
101 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
102 aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
103 int aCurrentNbCopies = aRefListOfShapes->size() ?
104 aRefListOfRotated->size() / aRefListOfShapes->size() - 1 : 0;
105 std::list<ObjectPtr> anInitialList = aRefListOfShapes->list();
106 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
107 std::list<ObjectPtr> anAddition;
108 std::vector<bool> isUsed(anInitialList.size(), false);
109 // collect new items and check the items to remove
110 for(int anInd = 0; anInd < aRotationObjectRefs->size(); anInd++) {
111 ObjectPtr anObject = aRotationObjectRefs->object(anInd);
112 std::list<ObjectPtr>::const_iterator anIt = anInitialList.begin();
113 std::vector<bool>::iterator aUsedIt = isUsed.begin();
114 for (; anIt != anInitialList.end(); anIt++, aUsedIt++)
115 if (*anIt == anObject) {
119 if (anIt == anInitialList.end())
120 anAddition.push_back(anObject);
122 // remove unused items
123 std::list<ObjectPtr>::iterator anInitIter = anInitialList.begin();
124 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
125 std::vector<bool>::iterator aUsedIter = isUsed.begin();
126 std::set<FeaturePtr> aFeaturesToBeRemoved;
127 for (; aUsedIter != isUsed.end(); aUsedIter++) {
129 aRefListOfShapes->remove(*anInitIter);
130 aRefListOfRotated->remove(*aTargetIter++);
131 for (int i = 0; i < aCurrentNbCopies && aTargetIter != aTargetList.end();
132 i++, aTargetIter++) {
133 aRefListOfRotated->remove(*aTargetIter);
134 // remove the corresponding feature from the sketch
135 ResultConstructionPtr aRC =
136 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aTargetIter);
137 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
138 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
140 aFeaturesToBeRemoved.insert(aFeature);
143 for (int i = 0; i <= aNbCopies && aTargetIter != aTargetList.end(); i++)
146 if (anInitIter != anInitialList.end())
149 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
150 // change number of copies
151 if (aCurrentNbCopies != 0 && aNbCopies != aCurrentNbCopies) {
152 bool isAdd = aNbCopies > aCurrentNbCopies;
153 int aMinCopies = isAdd ? aCurrentNbCopies : aNbCopies;
154 int aMaxCopies = isAdd ? aNbCopies : aCurrentNbCopies;
157 aTargetList = aRefListOfRotated->list();
158 aTargetIter = aTargetList.begin();
159 ObjectPtr anObjToCopy = *aTargetIter;
160 std::set<FeaturePtr> aFeaturesToBeRemoved;
161 while (aTargetIter != aTargetList.end()) {
162 aRefListOfRotated->remove(*aTargetIter);
165 if (ind > aMinCopies && ind <=aMaxCopies) {
166 while (ind <= aMaxCopies) {
168 // Add new shifted item
169 ObjectPtr anObject = copyFeature(anObjToCopy);
170 aTargetList.insert(aTargetIter, anObject);
173 std::list<ObjectPtr>::iterator aRemoveIt = aTargetIter++;
174 ObjectPtr anObject = *aRemoveIt;
175 aTargetList.erase(aRemoveIt);
176 aRefListOfRotated->remove(anObject);
177 // remove the corresponding feature from the sketch
178 ResultConstructionPtr aRC =
179 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(anObject);
180 DocumentPtr aDoc = aRC ? aRC->document() : DocumentPtr();
181 FeaturePtr aFeature = aDoc ? aDoc->feature(aRC) : FeaturePtr();
183 aFeaturesToBeRemoved.insert(aFeature);
188 if (aTargetIter != aTargetList.end())
189 anObjToCopy = *aTargetIter;
192 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
194 for (aTargetIter = aTargetList.begin(); aTargetIter != aTargetList.end(); aTargetIter++)
195 aRefListOfRotated->append(*aTargetIter);
198 std::list<ObjectPtr>::iterator anAddIter = anAddition.begin();
199 for (; anAddIter != anAddition.end(); anAddIter++) {
200 aRefListOfShapes->append(*anAddIter);
201 aRefListOfRotated->append(*anAddIter);
202 for (int i = 0; i < aNbCopies; i++) {
203 ObjectPtr anObject = copyFeature(*anAddIter);
204 aRefListOfRotated->append(anObject);
208 //// if (fabs(anAngle) > 1.e-12) {
209 //// // Recalculate positions of features
210 //// aTargetList = aRefListOfRotated->list();
211 //// aTargetIter = aTargetList.begin();
212 //// while (aTargetIter != aTargetList.end()) {
213 //// ObjectPtr anInitialObject = *aTargetIter++;
214 //// for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++)
215 //// rotateFeature(anInitialObject, *aTargetIter, aCenter->x(), aCenter->y(), anAngle * (i + 1));
219 // send events to update the sub-features by the solver
221 Events_Loop::loop()->setFlushed(anUpdateEvent, true);
224 AISObjectPtr SketchPlugin_MultiRotation::getAISObject(AISObjectPtr thePrevious)
229 AISObjectPtr anAIS = SketcherPrs_Factory::rotateConstraint(this, sketch()->coordinatePlane(),
234 void SketchPlugin_MultiRotation::erase()
236 static Events_Loop* aLoop = Events_Loop::loop();
237 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
239 // Set copy attribute to false on all copied features.
240 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
241 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
242 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
243 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
245 if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
246 for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
247 ObjectPtr anObject = aRefListOfRotated->object(anIndex);
248 if(aRefListOfShapes->isInList(anObject)) {
249 // Don't modify attribute of original features, just skip.
253 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
255 FeaturePtr aFeature = aRes->document()->feature(aRes);
257 AttributeBooleanPtr aBooleanAttr =
258 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
259 if(aBooleanAttr.get()) {
260 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
261 aBooleanAttr->setValue(false);
262 // Redisplay object as it is not copy anymore.
263 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
271 SketchPlugin_ConstraintBase::erase();
274 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
276 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
277 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
278 if (!aFeature || !aResult)
281 FeaturePtr aNewFeature =
282 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
283 aNewFeature->execute();
285 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
286 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
288 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
289 const std::list<ResultPtr>& aResults = aNewFeature->results();
290 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
291 for (; anIt != aResults.end(); anIt++) {
292 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
294 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
295 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
296 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
297 (aShapeIn->isFace() && aShapeOut->isFace()))
303 /*void SketchPlugin_MultiRotation::rotateFeature(
304 ObjectPtr theInitial, ObjectPtr theTarget,
305 double theCenterX, double theCenterY, double theAngle)
307 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
308 double cosA = std::cos(theAngle);
309 double sinA = std::sin(theAngle);
311 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
312 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
314 // block feature update
315 aTargetFeature->data()->blockSendAttributeUpdated(true);
317 std::list<AttributePtr> anInitAttrList =
318 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
319 std::list<AttributePtr> aTargetAttrList =
320 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
321 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
322 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
323 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
324 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
325 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
326 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
327 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
328 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
329 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
330 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
331 double dx = cosA * aDir->x() - sinA * aDir->y();
332 double dy = sinA * aDir->x() + cosA * aDir->y();
333 aPnt->setX(aCenter->x() + dx);
334 aPnt->setY(aCenter->y() + dy);
336 aPointTo->setValue(aPnt->x(), aPnt->y());
339 // unblock feature update
340 aTargetFeature->data()->blockSendAttributeUpdated(false);
344 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
346 if (theID == ROTATION_LIST_ID()) {
347 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
348 if (aRotationObjectRefs->size() == 0) {
349 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
353 // Clear list of objects
354 AttributeRefListPtr aRefListOfRotated = reflist(SketchPlugin_Constraint::ENTITY_B());
355 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
356 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
357 std::set<FeaturePtr> aFeaturesToBeRemoved;
358 while (aTargetIter != aTargetList.end()) {
360 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
361 aRefListOfRotated->remove(*aTargetIter);
362 // remove the corresponding feature from the sketch
363 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
365 aFeaturesToBeRemoved.insert(aFeature);
368 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
370 aRefListOfRotated->clear();
371 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();