Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchAPI / SketchAPI_Translation.cpp
1 // Copyright (C) 2014-2022  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_Translation.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_Translation::SketchAPI_Translation(
29     const std::shared_ptr<ModelAPI_Feature> & theFeature)
30 : ModelHighAPI_Interface(theFeature)
31 {
32   initialize();
33 }
34
35 SketchAPI_Translation::SketchAPI_Translation(
36     const std::shared_ptr<ModelAPI_Feature> & theFeature,
37     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
38     const ModelHighAPI_RefAttr & thePoint1,
39     const ModelHighAPI_RefAttr & thePoint2,
40     const ModelHighAPI_Integer & theNumberOfObjects,
41     bool theFullValue)
42 : ModelHighAPI_Interface(theFeature)
43 {
44   if (initialize()) {
45     fillAttribute(thePoint1, startPoint());
46     fillAttribute(thePoint2, endPoint());
47     fillAttribute(theNumberOfObjects, numberOfObjects());
48     fillAttribute(theFullValue ? "FullValue" : "SingleValue", valueType());
49
50     setTranslationList(theObjects);
51   }
52 }
53
54 SketchAPI_Translation::~SketchAPI_Translation()
55 {
56 }
57
58 void SketchAPI_Translation::setTranslationList(
59     const std::list<std::shared_ptr<ModelAPI_Object> >& theObjects)
60 {
61   fillAttribute(theObjects, translationList());
62   execute(true);
63 }
64
65 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Translation::translated() const
66 {
67   std::list<ObjectPtr> aList = translatedObjects()->list();
68   // remove all initial features
69   std::list<FeaturePtr> anIntermediate;
70   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
71   for (; anIt != aList.end(); ++anIt) {
72     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
73     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
74     if (isCopy.get() && isCopy->value())
75       anIntermediate.push_back(aFeature);
76   }
77   return SketchAPI_SketchEntity::wrap(anIntermediate);
78 }
79
80 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Translation::translatedList() const
81 {
82   std::list<ObjectPtr> aList = translationList()->list();
83   std::set<ObjectPtr> anOriginalObjects;
84   anOriginalObjects.insert(aList.begin(), aList.end());
85   // remove all initial features
86   std::list<FeaturePtr> anIntermediate;
87   aList = translatedObjects()->list();
88   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
89   for (; anIt != aList.end(); ++anIt) {
90     if (anOriginalObjects.find(*anIt) != anOriginalObjects.end())
91       continue; // skip initial object
92     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
93     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
94     if (isCopy.get() && isCopy->value())
95       anIntermediate.push_back(aFeature);
96   }
97   return SketchAPI_SketchEntity::wrap(anIntermediate);
98 }
99
100 //--------------------------------------------------------------------------------------
101
102 void SketchAPI_Translation::dump(ModelHighAPI_Dumper& theDumper) const
103 {
104   FeaturePtr aBase = feature();
105   const std::string& aSketchName = theDumper.parentName(aBase);
106
107   AttributeRefListPtr aTransObjects = translationList();
108   AttributeRefAttrPtr aStart = startPoint();
109   AttributeRefAttrPtr aEnd   = endPoint();
110   AttributeIntegerPtr aNbCopies = numberOfObjects();
111   bool isFullValue = valueType()->value() != "SingleValue";
112
113   // Check all attributes are already dumped. If not, store the constraint as postponed.
114   size_t aFirstNotDumped = theDumper.indexOfFirstNotDumped(aTransObjects);
115   if (!theDumper.isDumped(aStart) || !theDumper.isDumped(aEnd) || aFirstNotDumped == 0) {
116     theDumper.postpone(aBase);
117     return;
118   }
119
120   // the number of dumped aTransObjects is not changed, no need to dump anything
121   static std::map<FeaturePtr, size_t> aNbDumpedArguments;
122   std::map<FeaturePtr, size_t>::iterator aFound = aNbDumpedArguments.find(aBase);
123   if (aFound != aNbDumpedArguments.end() && aFound->second == aFirstNotDumped) {
124     theDumper.postpone(aBase);
125     return;
126   }
127   else
128     aNbDumpedArguments[aBase] = aFirstNotDumped;
129
130   if (theDumper.isDumped(aBase)) {
131     // the feature is already dumped, but it was postponed, because of some arguments
132     // were not dumped yet, thus, it is necessary to update the list of rotated objects
133     theDumper << "\n### Update " << aBase->getKind() << std::endl;
134     theDumper << aBase << ".setTranslationList(" << aTransObjects << ")" << std::endl;
135   }
136   else {
137     // the feature is not dumped yet, make the full dump
138     theDumper << aBase << " = " << aSketchName << ".addTranslation("
139               << aTransObjects << ", " << aStart << ", " << aEnd << ", " << aNbCopies;
140     if (isFullValue)
141       theDumper << ", " << isFullValue;
142     theDumper << ")" << std::endl;
143   }
144
145   // Dump variables for a list of translated features
146   theDumper << "[";
147   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = translatedList();
148   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
149   for (size_t anIndex = 0; anIndex < aFirstNotDumped; ++anIndex)
150     for (int i = 1; i < aNbCopies->value() && anIt != aList.end(); ++i, ++anIt) {
151       if (anIt != aList.begin())
152         theDumper << ", ";
153       theDumper << (*anIt)->feature();
154     }
155   theDumper << "] = " << theDumper.name(aBase) << ".translatedList()" << std::endl;
156
157   if (theDumper.isDumped(aTransObjects)) {
158     aNbDumpedArguments.erase(aBase);
159     // Set necessary "auxiliary" flag for translated features
160     // (flag is set if it differs to base entity)
161     std::list<ObjectPtr> aTransList = aTransObjects->list();
162     std::list<ObjectPtr>::const_iterator aTrIt = aTransList.begin();
163     anIt = aList.begin();
164     for (; aTrIt != aTransList.end(); ++aTrIt) {
165       FeaturePtr aFeature = ModelAPI_Feature::feature(*aTrIt);
166       if (!aFeature)
167         continue;
168       bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
169
170       for (int i = 1; i < aNbCopies->value(); ++i, ++anIt) {
171         aFeature = (*anIt)->feature();
172         bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
173         if (aFeatAux != aBaseAux)
174           theDumper << theDumper.name((*anIt)->feature(), false)
175                     << ".setAuxiliary(" << aFeatAux << ")" << std::endl;
176       }
177     }
178   }
179   else {
180     // If all refereced objects are not dumped yet, mark the feature as postponed.
181     theDumper.postpone(aBase);
182   }
183 }