Salome HOME
8bf2062182830d5d7c6e1826ce635d278d6f13a0
[modules/shaper.git] / src / SketchAPI / SketchAPI_Rotation.cpp
1 // Copyright (C) 2014-2023  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 = rotatedObjects()->list();
71   // remove all initial features
72   std::list<FeaturePtr> anIntermediate;
73   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
74   for (; anIt != aList.end(); ++anIt) {
75     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
76     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
77     if (isCopy.get() && isCopy->value())
78       anIntermediate.push_back(aFeature);
79   }
80   return SketchAPI_SketchEntity::wrap(anIntermediate);
81 }
82
83 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Rotation::rotatedList() const
84 {
85   std::list<ObjectPtr> aList = rotationList()->list();
86   std::set<ObjectPtr> anOriginalObjects;
87   anOriginalObjects.insert(aList.begin(), aList.end());
88   // remove all initial features
89   std::list<FeaturePtr> anIntermediate;
90   aList = rotatedObjects()->list();
91   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
92   for (; anIt != aList.end(); ++anIt) {
93     if (anOriginalObjects.find(*anIt) != anOriginalObjects.end())
94       continue; // skip initial object
95     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
96     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
97     if (isCopy.get() && isCopy->value())
98       anIntermediate.push_back(aFeature);
99   }
100   return SketchAPI_SketchEntity::wrap(anIntermediate);
101 }
102
103 //--------------------------------------------------------------------------------------
104
105 void SketchAPI_Rotation::dump(ModelHighAPI_Dumper& theDumper) const
106 {
107   FeaturePtr aBase = feature();
108   const std::string& aSketchName = theDumper.parentName(aBase);
109
110   AttributeRefListPtr aRotObjects = rotationList();
111   AttributeRefAttrPtr aCenter = center();
112   AttributeDoublePtr anAngle = angle();
113   AttributeIntegerPtr aNbCopies = numberOfObjects();
114   bool isFullValue = valueType()->value() != "SingleAngle";
115   bool isReversed = reversed()->value();
116
117   // Check all attributes are already dumped. If not, store the constraint as postponed.
118   size_t aFirstNotDumped = theDumper.indexOfFirstNotDumped(aRotObjects);
119   if (!theDumper.isDumped(aCenter) || aFirstNotDumped == 0) {
120     theDumper.postpone(aBase);
121     return;
122   }
123
124   // the number of dumped aRotObjects is not changed, no need to dump anything
125   static std::map<FeaturePtr, size_t> aNbDumpedArguments;
126   std::map<FeaturePtr, size_t>::iterator aFound = aNbDumpedArguments.find(aBase);
127   if (aFound != aNbDumpedArguments.end() && aFound->second == aFirstNotDumped) {
128     theDumper.postpone(aBase);
129     return;
130   }
131   else
132     aNbDumpedArguments[aBase] = aFirstNotDumped;
133
134   if (theDumper.isDumped(aBase)) {
135     // the feature is already dumped, but it was postponed, because of some arguments
136     // were not dumped yet, thus, it is necessary to update the list of rotated objects
137     theDumper << "\n### Update " << aBase->getKind() << std::endl;
138     theDumper << aBase << ".setRotationList(" << aRotObjects << ")" << std::endl;
139   }
140   else {
141     // the feature is not dumped yet, make the full dump
142     theDumper << aBase << " = " << aSketchName << ".addRotation("
143               << aRotObjects << ", " << aCenter << ", " << anAngle << ", " << aNbCopies;
144     if (isFullValue || isReversed)
145     {
146       theDumper << ", " << isFullValue;
147       if (isReversed)
148         theDumper << ", " << isReversed;
149     }
150     theDumper << ")" << std::endl;
151   }
152
153   // Dump variables for a list of rotated features
154   theDumper << "[";
155   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = rotatedList();
156   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
157   for (size_t anIndex = 0; anIndex < aFirstNotDumped; ++anIndex)
158     for (int i = 1; i < aNbCopies->value() && anIt != aList.end(); ++i, ++anIt) {
159       if (anIt != aList.begin())
160         theDumper << ", ";
161       theDumper << (*anIt)->feature();
162     }
163   theDumper << "] = " << theDumper.name(aBase) << ".rotatedList()" << std::endl;
164
165   if (theDumper.isDumped(aRotObjects)) {
166     aNbDumpedArguments.erase(aBase);
167     // Set necessary "auxiliary" flag for rotated features
168     // (flag is set if it differs to base entity)
169     std::list<ObjectPtr> aRotList = aRotObjects->list();
170     std::list<ObjectPtr>::const_iterator aRIt = aRotList.begin();
171     anIt = aList.begin();
172     for (; aRIt != aRotList.end(); ++aRIt) {
173       FeaturePtr aFeature = ModelAPI_Feature::feature(*aRIt);
174       if (!aFeature)
175         continue;
176       bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
177
178       for (int i = 1; i < aNbCopies->value(); ++i, ++anIt) {
179         aFeature = (*anIt)->feature();
180         bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
181         if (aFeatAux != aBaseAux)
182           theDumper << theDumper.name((*anIt)->feature(), false)
183                     << ".setAuxiliary(" << aFeatAux << ")" << std::endl;
184       }
185     }
186   }
187   else {
188     // If all refereced objects are not dumped yet, mark the feature as postponed.
189     theDumper.postpone(aBase);
190   }
191 }