Salome HOME
6f8088f7040991869b984659a1cf926c0531a86f
[modules/shaper.git] / src / SketchAPI / SketchAPI_Offset.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_Offset.h"
21 #include <SketchAPI_SketchEntity.h>
22 //--------------------------------------------------------------------------------------
23 #include <ModelHighAPI_Dumper.h>
24 #include <ModelHighAPI_Selection.h>
25 #include <ModelHighAPI_Tools.h>
26
27 //--------------------------------------------------------------------------------------
28 SketchAPI_Offset::SketchAPI_Offset (const std::shared_ptr<ModelAPI_Feature> & theFeature)
29   : ModelHighAPI_Interface(theFeature)
30 {
31   initialize();
32 }
33
34 SketchAPI_Offset::SketchAPI_Offset (const std::shared_ptr<ModelAPI_Feature> & theFeature,
35                                     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
36                                     const ModelHighAPI_Double & theOffsetValue,
37                                     const bool theIsReversed,
38                                     const std::string & theJointType,
39                                     const bool theIsApprox)
40   : ModelHighAPI_Interface(theFeature)
41 {
42   if (initialize()) {
43     fillAttribute(theObjects, edgesList());
44     fillAttribute(theOffsetValue, value());
45     fillAttribute(theIsReversed, reversed());
46     fillAttribute(theJointType, joint());
47     fillAttribute(theIsApprox, approx());
48
49     execute();
50   }
51 }
52
53 SketchAPI_Offset::~SketchAPI_Offset()
54 {
55 }
56
57 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Offset::offset() const
58 {
59   std::list<ObjectPtr> aList = feature()->reflist(SketchPlugin_Constraint::ENTITY_B())->list();
60   std::list<FeaturePtr> anIntermediate;
61   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
62   for (; anIt != aList.end(); ++anIt) {
63     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
64     anIntermediate.push_back(aFeature);
65   }
66   return SketchAPI_SketchEntity::wrap(anIntermediate);
67 }
68
69 //--------------------------------------------------------------------------------------
70
71 void SketchAPI_Offset::dump (ModelHighAPI_Dumper& theDumper) const
72 {
73   FeaturePtr aBase = feature();
74   const std::string& aSketchName = theDumper.parentName(aBase);
75
76   AttributeRefListPtr aOffsetObjects = edgesList();
77   AttributeDoublePtr aValue = value();
78   AttributeBooleanPtr aReversed = reversed();
79   AttributeStringPtr aJoint = joint();
80   AttributeBooleanPtr anApprox = approx();
81
82   // Check all attributes are already dumped. If not, store the feature as postponed.
83   if (!theDumper.isDumped(aOffsetObjects)) {
84     theDumper.postpone(aBase);
85     return;
86   }
87
88   theDumper << aBase << " = " << aSketchName << ".addOffset(" << aOffsetObjects << ", "
89             << aValue << ", " << aReversed  << ", " << aJoint << ", " << anApprox << ")" << std::endl;
90
91   // Dump variables for a list of created features
92   theDumper << "[";
93   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = offset();
94   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
95   for (; anIt != aList.end(); ++anIt) {
96     if (anIt != aList.begin())
97       theDumper << ", ";
98     theDumper << (*anIt)->feature();
99   }
100   theDumper << "] = " << theDumper.name(aBase) << ".offset()" << std::endl;
101
102   // Set necessary "auxiliary" flag for created features
103   for (anIt = aList.begin(); anIt != aList.end(); ++anIt) {
104     FeaturePtr aFeature = (*anIt)->feature();
105     bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
106     if (aFeatAux)
107       theDumper << theDumper.name((*anIt)->feature(), false)
108                 << ".setAuxiliary(" << aFeatAux << ")" <<std::endl;
109   }
110 }