Salome HOME
Fix incorrect number of results returned by MultiRotation/MultiTranslation feature...
[modules/shaper.git] / src / SketchAPI / SketchAPI_Rotation.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchAPI_Rotation.h"
21 #include <SketchAPI_SketchEntity.h>
22 //--------------------------------------------------------------------------------------
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Tools.h>
25
26 #include <SketchPlugin_SketchEntity.h>
27 //--------------------------------------------------------------------------------------
28 SketchAPI_Rotation::SketchAPI_Rotation(
29     const std::shared_ptr<ModelAPI_Feature> & theFeature)
30 : ModelHighAPI_Interface(theFeature)
31 {
32   initialize();
33 }
34
35 SketchAPI_Rotation::SketchAPI_Rotation(
36     const std::shared_ptr<ModelAPI_Feature> & theFeature,
37     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
38     const ModelHighAPI_RefAttr & theCenter,
39     const ModelHighAPI_Double & theAngle,
40     const ModelHighAPI_Integer & theNumberOfObjects,
41     bool theFullValue,
42     bool theReversed)
43 : ModelHighAPI_Interface(theFeature)
44 {
45   if (initialize()) {
46     fillAttribute(theCenter, center());
47     fillAttribute(theFullValue ? "FullAngle" : "SingleAngle", valueType());
48     fillAttribute(theAngle, angle());
49     fillAttribute(theReversed, reversed());
50     fillAttribute(theNumberOfObjects, numberOfObjects());
51
52     setRotationList(theObjects);
53   }
54 }
55
56 SketchAPI_Rotation::~SketchAPI_Rotation()
57 {
58
59 }
60
61 void SketchAPI_Rotation::setRotationList(
62     const std::list<std::shared_ptr<ModelAPI_Object> >& theObjects)
63 {
64   fillAttribute(theObjects, rotationList());
65   execute(true);
66 }
67
68 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Rotation::rotated() const
69 {
70   std::list<ObjectPtr> aList = rotationList()->list();
71   std::set<ObjectPtr> anOriginalObjects;
72   anOriginalObjects.insert(aList.begin(), aList.end());
73   // remove all initial features
74   std::list<FeaturePtr> anIntermediate;
75   aList = rotatedObjects()->list();
76   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
77   for (; anIt != aList.end(); ++anIt) {
78     if (anOriginalObjects.find(*anIt) != anOriginalObjects.end())
79       continue; // skip initial object
80     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
81     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
82     if (isCopy.get() && isCopy->value())
83       anIntermediate.push_back(aFeature);
84   }
85   return SketchAPI_SketchEntity::wrap(anIntermediate);
86 }
87
88 //--------------------------------------------------------------------------------------
89
90 void SketchAPI_Rotation::dump(ModelHighAPI_Dumper& theDumper) const
91 {
92   FeaturePtr aBase = feature();
93   const std::string& aSketchName = theDumper.parentName(aBase);
94
95   AttributeRefListPtr aRotObjects = rotationList();
96   AttributeRefAttrPtr aCenter = center();
97   AttributeDoublePtr anAngle = angle();
98   AttributeIntegerPtr aNbCopies = numberOfObjects();
99   bool isFullValue = valueType()->value() != "SingleAngle";
100   bool isReversed = reversed()->value();
101
102   // Check all attributes are already dumped. If not, store the constraint as postponed.
103   size_t aFirstNotDumped = theDumper.indexOfFirstNotDumped(aRotObjects);
104   if (!theDumper.isDumped(aCenter) || aFirstNotDumped == 0) {
105     theDumper.postpone(aBase);
106     return;
107   }
108
109   // the number of dumped aRotObjects is not changed, no need to dump anything
110   static std::map<FeaturePtr, size_t> aNbDumpedArguments;
111   std::map<FeaturePtr, size_t>::iterator aFound = aNbDumpedArguments.find(aBase);
112   if (aFound != aNbDumpedArguments.end() && aFound->second == aFirstNotDumped) {
113     theDumper.postpone(aBase);
114     return;
115   }
116   else
117     aNbDumpedArguments[aBase] = aFirstNotDumped;
118
119   if (theDumper.isDumped(aBase)) {
120     // the feature is already dumped, but it was postponed, because of some arguments
121     // were not dumped yet, thus, it is necessary to update the list of rotated objects
122     theDumper << "\n### Update " << aBase->getKind() << std::endl;
123     theDumper << aBase << ".setRotationList(" << aRotObjects << ")" << std::endl;
124   }
125   else {
126     // the feature is not dumped yet, make the full dump
127     theDumper << aBase << " = " << aSketchName << ".addRotation("
128               << aRotObjects << ", " << aCenter << ", " << anAngle << ", " << aNbCopies;
129     if (isFullValue || isReversed)
130     {
131       theDumper << ", " << isFullValue;
132       if (isReversed)
133         theDumper << ", " << isReversed;
134     }
135     theDumper << ")" << std::endl;
136   }
137
138   // Dump variables for a list of rotated features
139   theDumper << "[";
140   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = rotated();
141   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
142   for (size_t anIndex = 0; anIndex < aFirstNotDumped; ++anIndex)
143     for (int i = 1; i < aNbCopies->value() && anIt != aList.end(); ++i, ++anIt) {
144       if (anIt != aList.begin())
145         theDumper << ", ";
146       theDumper << (*anIt)->feature();
147     }
148   theDumper << "] = " << theDumper.name(aBase) << ".rotated()" << std::endl;
149
150   if (theDumper.isDumped(aRotObjects)) {
151     aNbDumpedArguments.erase(aBase);
152     // Set necessary "auxiliary" flag for rotated features
153     // (flag is set if it differs to base entity)
154     std::list<ObjectPtr> aRotList = aRotObjects->list();
155     std::list<ObjectPtr>::const_iterator aRIt = aRotList.begin();
156     anIt = aList.begin();
157     for (; aRIt != aRotList.end(); ++aRIt) {
158       FeaturePtr aFeature = ModelAPI_Feature::feature(*aRIt);
159       if (!aFeature)
160         continue;
161       bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
162
163       for (int i = 1; i < aNbCopies->value(); ++i, ++anIt) {
164         aFeature = (*anIt)->feature();
165         bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
166         if (aFeatAux != aBaseAux)
167           theDumper << theDumper.name((*anIt)->feature(), false)
168                     << ".setAuxiliary(" << aFeatAux << ")" << std::endl;
169       }
170     }
171   }
172   else {
173     // If all refereced objects are not dumped yet, mark the feature as postponed.
174     theDumper.postpone(aBase);
175   }
176 }