Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchAPI / SketchAPI_Mirror.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_Mirror.h"
21 #include <SketchAPI_SketchEntity.h>
22 //--------------------------------------------------------------------------------------
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Selection.h>
25 #include <ModelHighAPI_Tools.h>
26 //--------------------------------------------------------------------------------------
27 SketchAPI_Mirror::SketchAPI_Mirror(
28     const std::shared_ptr<ModelAPI_Feature> & theFeature)
29 : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
34 SketchAPI_Mirror::SketchAPI_Mirror(
35     const std::shared_ptr<ModelAPI_Feature> & theFeature,
36     const ModelHighAPI_RefAttr & theMirrorLine,
37     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects)
38 : ModelHighAPI_Interface(theFeature)
39 {
40   if (initialize()) {
41     fillAttribute(theMirrorLine, mirrorLine());
42     setMirrorList(theObjects);
43   }
44 }
45
46 SketchAPI_Mirror::~SketchAPI_Mirror()
47 {
48 }
49
50 void SketchAPI_Mirror::setMirrorList(const std::list<std::shared_ptr<ModelAPI_Object> >& theObjects)
51 {
52   fillAttribute(theObjects, mirrorList());
53   execute();
54 }
55
56 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Mirror::mirrored() const
57 {
58   std::list<ObjectPtr> aList = mirroredObjects()->list();
59   std::list<FeaturePtr> anIntermediate;
60   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
61   for (; anIt != aList.end(); ++anIt) {
62     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
63     anIntermediate.push_back(aFeature);
64   }
65   return SketchAPI_SketchEntity::wrap(anIntermediate);
66 }
67
68 //--------------------------------------------------------------------------------------
69
70 void SketchAPI_Mirror::dump(ModelHighAPI_Dumper& theDumper) const
71 {
72   FeaturePtr aBase = feature();
73   const std::string& aSketchName = theDumper.parentName(aBase);
74
75   AttributeRefAttrPtr aMirrorLine = mirrorLine();
76   AttributeRefListPtr aMirrorObjects = mirrorList();
77
78   // Check all attributes are already dumped. If not, store the constraint as postponed.
79   size_t aFirstNotDumped = theDumper.indexOfFirstNotDumped(aMirrorObjects);
80   if (!theDumper.isDumped(aMirrorLine) || aFirstNotDumped == 0) {
81     theDumper.postpone(aBase);
82     return;
83   }
84
85
86   // the number of dumped aMirrorObjects is not changed, no need to dump anything
87   static std::map<FeaturePtr, size_t> aNbDumpedArguments;
88   std::map<FeaturePtr, size_t>::iterator aFound = aNbDumpedArguments.find(aBase);
89   if (aFound != aNbDumpedArguments.end() && aFound->second == aFirstNotDumped) {
90     theDumper.postpone(aBase);
91     return;
92   }
93   else
94     aNbDumpedArguments[aBase] = aFirstNotDumped;
95
96   if (theDumper.isDumped(aBase)) {
97     // the feature is already dumped, but it was postponed, because of some arguments
98     // were not dumped yet, thus, it is necessary to update the list of rotated objects
99     theDumper << "\n### Update " << aBase->getKind() << std::endl;
100     theDumper << aBase << ".setMirrorList(" << aMirrorObjects << ")" << std::endl;
101   }
102   else {
103     // the feature is not dumped yet, make the full dump
104     theDumper << aBase << " = " << aSketchName << ".addMirror(" << aMirrorLine << ", "
105               << aMirrorObjects << ")" << std::endl;
106   }
107
108   // Dump variables for a list of mirrored features
109   theDumper << "[";
110   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = mirrored();
111   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
112   for (size_t anIndex = 0; anIndex < aFirstNotDumped; ++anIndex, ++anIt) {
113     if (anIt != aList.begin())
114       theDumper << ", ";
115     theDumper << (*anIt)->feature();
116   }
117   theDumper << "] = " << theDumper.name(aBase) << ".mirrored()" << std::endl;
118
119   if (theDumper.isDumped(aMirrorObjects)) {
120     aNbDumpedArguments.erase(aBase);
121     // Set necessary "auxiliary" flag for mirrored features
122     // (flag is set if it differs to base entity)
123     std::list<ObjectPtr> aMirList = aMirrorObjects->list();
124     std::list<ObjectPtr>::const_iterator aMIt = aMirList.begin();
125     for (anIt = aList.begin(); aMIt != aMirList.end(); ++aMIt, ++anIt) {
126       FeaturePtr aFeature = ModelAPI_Feature::feature(*aMIt);
127       if (!aFeature)
128         continue;
129       bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
130
131       aFeature = (*anIt)->feature();
132       bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
133       if (aFeatAux != aBaseAux)
134         theDumper << theDumper.name((*anIt)->feature(), false)
135                   << ".setAuxiliary(" << aFeatAux << ")" <<std::endl;
136     }
137   }
138   else {
139     // If all refereced objects are not dumped yet, mark the feature as postponed.
140     theDumper.postpone(aBase);
141   }
142 }