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(),
230 sketch()->coordinatePlane(),
235 void SketchPlugin_MultiRotation::erase()
237 static Events_Loop* aLoop = Events_Loop::loop();
238 static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
240 // Set copy attribute to false on all copied features.
241 AttributeRefListPtr aRefListOfShapes = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
242 data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
243 AttributeRefListPtr aRefListOfRotated = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
244 data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
246 if(aRefListOfShapes.get() && aRefListOfRotated.get()) {
247 for(int anIndex = 0; anIndex < aRefListOfRotated->size(); anIndex++) {
248 ObjectPtr anObject = aRefListOfRotated->object(anIndex);
249 if(aRefListOfShapes->isInList(anObject)) {
250 // Don't modify attribute of original features, just skip.
254 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
256 FeaturePtr aFeature = aRes->document()->feature(aRes);
258 AttributeBooleanPtr aBooleanAttr =
259 aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
260 if(aBooleanAttr.get()) {
261 if (ModelAPI_Session::get()->isOperation()) // if this is not undo or redo
262 aBooleanAttr->setValue(false);
263 // Redisplay object as it is not copy anymore.
264 ModelAPI_EventCreator::get()->sendUpdated(aRes, aRedispEvent);
272 SketchPlugin_ConstraintBase::erase();
275 ObjectPtr SketchPlugin_MultiRotation::copyFeature(ObjectPtr theObject)
277 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
278 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
279 if (!aFeature || !aResult)
282 FeaturePtr aNewFeature =
283 SketchPlugin_Sketch::addUniqueNamedCopiedFeature(aFeature, sketch(), true);
284 aNewFeature->execute();
286 static Events_ID aRedisplayEvent = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
287 ModelAPI_EventCreator::get()->sendUpdated(aNewFeature, aRedisplayEvent);
289 std::shared_ptr<GeomAPI_Shape> aShapeIn = aResult->shape();
290 const std::list<ResultPtr>& aResults = aNewFeature->results();
291 std::list<ResultPtr>::const_iterator anIt = aResults.begin();
292 for (; anIt != aResults.end(); anIt++) {
293 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*anIt);
295 std::shared_ptr<GeomAPI_Shape> aShapeOut = aRC->shape();
296 if ((aShapeIn->isVertex() && aShapeOut->isVertex()) ||
297 (aShapeIn->isEdge() && aShapeOut->isEdge()) ||
298 (aShapeIn->isFace() && aShapeOut->isFace()))
304 /*void SketchPlugin_MultiRotation::rotateFeature(
305 ObjectPtr theInitial, ObjectPtr theTarget,
306 double theCenterX, double theCenterY, double theAngle)
308 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(theCenterX, theCenterY));
309 double cosA = std::cos(theAngle);
310 double sinA = std::sin(theAngle);
312 FeaturePtr anInitialFeature = ModelAPI_Feature::feature(theInitial);
313 FeaturePtr aTargetFeature = ModelAPI_Feature::feature(theTarget);
315 // block feature update
316 aTargetFeature->data()->blockSendAttributeUpdated(true);
318 std::list<AttributePtr> anInitAttrList =
319 anInitialFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
320 std::list<AttributePtr> aTargetAttrList =
321 aTargetFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
322 std::list<AttributePtr>::iterator anInitIt = anInitAttrList.begin();
323 std::list<AttributePtr>::iterator aTargetIt = aTargetAttrList.begin();
324 for (; anInitIt != anInitAttrList.end(); anInitIt++, aTargetIt++) {
325 std::shared_ptr<GeomDataAPI_Point2D> aPointFrom =
326 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anInitIt);
327 std::shared_ptr<GeomDataAPI_Point2D> aPointTo =
328 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aTargetIt);
329 std::shared_ptr<GeomAPI_XY> aPnt = aPointFrom->pnt()->xy();
330 if (aPnt->distance(aCenter->xy()) > 1.e-7) {
331 std::shared_ptr<GeomAPI_XY> aDir = aPnt->decreased(aCenter->xy());
332 double dx = cosA * aDir->x() - sinA * aDir->y();
333 double dy = sinA * aDir->x() + cosA * aDir->y();
334 aPnt->setX(aCenter->x() + dx);
335 aPnt->setY(aCenter->y() + dy);
337 aPointTo->setValue(aPnt->x(), aPnt->y());
340 // unblock feature update
341 aTargetFeature->data()->blockSendAttributeUpdated(false);
345 void SketchPlugin_MultiRotation::attributeChanged(const std::string& theID)
347 if (theID == ROTATION_LIST_ID()) {
348 AttributeRefListPtr aRotationObjectRefs = reflist(ROTATION_LIST_ID());
349 if (aRotationObjectRefs->size() == 0) {
350 int aNbCopies = integer(NUMBER_OF_OBJECTS_ID())->value()-1;
354 // Clear list of objects
355 AttributeRefListPtr aRefListOfRotated = reflist(SketchPlugin_Constraint::ENTITY_B());
356 std::list<ObjectPtr> aTargetList = aRefListOfRotated->list();
357 std::list<ObjectPtr>::iterator aTargetIter = aTargetList.begin();
358 std::set<FeaturePtr> aFeaturesToBeRemoved;
359 while (aTargetIter != aTargetList.end()) {
361 for (int i = 0; i < aNbCopies && aTargetIter != aTargetList.end(); i++, aTargetIter++) {
362 aRefListOfRotated->remove(*aTargetIter);
363 // remove the corresponding feature from the sketch
364 FeaturePtr aFeature = ModelAPI_Feature::feature(*aTargetIter);
366 aFeaturesToBeRemoved.insert(aFeature);
369 ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToBeRemoved);
371 aRefListOfRotated->clear();
372 reflist(SketchPlugin_Constraint::ENTITY_A())->clear();