Salome HOME
Update copyrights
[modules/shaper.git] / src / SketchAPI / SketchAPI_Rotation.cpp
1 // Copyright (C) 2014-2019  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(theObjects, rotationList());
47     fillAttribute(theCenter, center());
48     fillAttribute(theFullValue ? "FullAngle" : "SingleAngle", valueType());
49     fillAttribute(theAngle, angle());
50     fillAttribute(theReversed, reversed());
51     fillAttribute(theNumberOfObjects, numberOfObjects());
52
53     execute(true);
54   }
55 }
56
57 SketchAPI_Rotation::~SketchAPI_Rotation()
58 {
59
60 }
61
62 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Rotation::rotated() const
63 {
64   std::list<ObjectPtr> aList = rotatedObjects()->list();
65   // remove all initial features
66   std::list<FeaturePtr> anIntermediate;
67   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
68   for (; anIt != aList.end(); ++anIt) {
69     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
70     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
71     if (isCopy.get() && isCopy->value())
72       anIntermediate.push_back(aFeature);
73   }
74   return SketchAPI_SketchEntity::wrap(anIntermediate);
75 }
76
77 //--------------------------------------------------------------------------------------
78
79 void SketchAPI_Rotation::dump(ModelHighAPI_Dumper& theDumper) const
80 {
81   FeaturePtr aBase = feature();
82   const std::string& aSketchName = theDumper.parentName(aBase);
83
84   AttributeRefListPtr aRotObjects = rotationList();
85   AttributeRefAttrPtr aCenter = center();
86   AttributeDoublePtr anAngle = angle();
87   AttributeIntegerPtr aNbCopies = numberOfObjects();
88   bool isFullValue = valueType()->value() != "SingleAngle";
89   bool isReversed = reversed()->value();
90
91   // Check all attributes are already dumped. If not, store the constraint as postponed.
92   if (!theDumper.isDumped(aCenter) || !theDumper.isDumped(aRotObjects)) {
93     theDumper.postpone(aBase);
94     return;
95   }
96
97   theDumper << aBase << " = " << aSketchName << ".addRotation("
98             << aRotObjects << ", " << aCenter << ", " << anAngle << ", " << aNbCopies;
99   if (isFullValue || isReversed)
100   {
101     theDumper << ", " << isFullValue;
102     if (isReversed)
103       theDumper << ", " << isReversed;
104   }
105   theDumper << ")" << std::endl;
106
107   // Dump variables for a list of rotated features
108   theDumper << "[";
109   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = rotated();
110   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
111   for (; anIt != aList.end(); ++anIt) {
112     if (anIt != aList.begin())
113       theDumper << ", ";
114     theDumper << (*anIt)->feature();
115   }
116   theDumper << "] = " << theDumper.name(aBase) << ".rotated()" << std::endl;
117
118   // Set necessary "auxiliary" flag for rotated features
119   // (flag is set if it differs to base entity)
120   std::list<ObjectPtr> aRotList = aRotObjects->list();
121   std::list<ObjectPtr>::const_iterator aRIt = aRotList.begin();
122   anIt = aList.begin();
123   for (; aRIt != aRotList.end(); ++aRIt) {
124     FeaturePtr aFeature = ModelAPI_Feature::feature(*aRIt);
125     if (!aFeature)
126       continue;
127     bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
128
129     for (int i = 1; i < aNbCopies->value(); ++i, ++anIt) {
130       aFeature = (*anIt)->feature();
131       bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
132       if (aFeatAux != aBaseAux)
133         theDumper << theDumper.name((*anIt)->feature(), false)
134                   << ".setAuxiliary(" << aFeatAux << ")" <<std::endl;
135     }
136   }
137 }