Salome HOME
Issue #2481: Application error when create fillet
[modules/shaper.git] / src / SketchAPI / SketchAPI_Translation.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchAPI_Translation.h"
22 #include <SketchAPI_SketchEntity.h>
23 //--------------------------------------------------------------------------------------
24 #include <ModelHighAPI_Dumper.h>
25 #include <ModelHighAPI_Tools.h>
26
27 #include <SketchPlugin_SketchEntity.h>
28 //--------------------------------------------------------------------------------------
29 SketchAPI_Translation::SketchAPI_Translation(
30     const std::shared_ptr<ModelAPI_Feature> & theFeature)
31 : ModelHighAPI_Interface(theFeature)
32 {
33   initialize();
34 }
35
36 SketchAPI_Translation::SketchAPI_Translation(
37     const std::shared_ptr<ModelAPI_Feature> & theFeature,
38     const std::list<std::shared_ptr<ModelAPI_Object> > & theObjects,
39     const ModelHighAPI_RefAttr & thePoint1,
40     const ModelHighAPI_RefAttr & thePoint2,
41     const ModelHighAPI_Integer & theNumberOfObjects,
42     bool theFullValue)
43 : ModelHighAPI_Interface(theFeature)
44 {
45   if (initialize()) {
46     fillAttribute(theObjects, translationList());
47     fillAttribute(thePoint1, startPoint());
48     fillAttribute(thePoint2, endPoint());
49     fillAttribute(theNumberOfObjects, numberOfObjects());
50     fillAttribute(theFullValue ? "FullValue" : "SingleValue", valueType());
51
52     execute(true);
53   }
54 }
55
56 SketchAPI_Translation::~SketchAPI_Translation()
57 {
58
59 }
60
61 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Translation::translated() const
62 {
63   std::list<ObjectPtr> aList = translatedObjects()->list();
64   // remove all initial features
65   std::list<FeaturePtr> anIntermediate;
66   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
67   for (; anIt != aList.end(); ++anIt) {
68     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
69     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
70     if (isCopy.get() && isCopy->value())
71       anIntermediate.push_back(aFeature);
72   }
73   return SketchAPI_SketchEntity::wrap(anIntermediate);
74 }
75
76 //--------------------------------------------------------------------------------------
77
78 void SketchAPI_Translation::dump(ModelHighAPI_Dumper& theDumper) const
79 {
80   FeaturePtr aBase = feature();
81   const std::string& aSketchName = theDumper.parentName(aBase);
82
83   AttributeRefListPtr aTransObjects = translationList();
84   AttributeRefAttrPtr aStart = startPoint();
85   AttributeRefAttrPtr aEnd   = endPoint();
86   AttributeIntegerPtr aNbCopies = numberOfObjects();
87   bool isFullValue = valueType()->value() != "SingleValue";
88
89   // Check all attributes are already dumped. If not, store the constraint as postponed.
90   if (!theDumper.isDumped(aStart) || !theDumper.isDumped(aEnd) ||
91       !theDumper.isDumped(aTransObjects)) {
92     theDumper.postpone(aBase);
93     return;
94   }
95
96   theDumper << aBase << " = " << aSketchName << ".addTranslation("
97             << aTransObjects << ", " << aStart << ", " << aEnd << ", " << aNbCopies;
98   if (isFullValue)
99     theDumper << ", " << isFullValue;
100   theDumper << ")" << std::endl;
101
102   // Dump variables for a list of translated features
103   theDumper << "[";
104   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = translated();
105   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
106   for (; anIt != aList.end(); ++anIt) {
107     if (anIt != aList.begin())
108       theDumper << ", ";
109     theDumper << (*anIt)->feature();
110   }
111   theDumper << "] = " << theDumper.name(aBase) << ".translated()" << std::endl;
112
113   // Set necessary "auxiliary" flag for translated features
114   // (flag is set if it differs to base entity)
115   std::list<ObjectPtr> aTransList = aTransObjects->list();
116   std::list<ObjectPtr>::const_iterator aTrIt = aTransList.begin();
117   anIt = aList.begin();
118   for (; aTrIt != aTransList.end(); ++aTrIt) {
119     FeaturePtr aFeature = ModelAPI_Feature::feature(*aTrIt);
120     if (!aFeature)
121       continue;
122     bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
123
124     for (int i = 1; i < aNbCopies->value(); ++i, ++anIt) {
125       aFeature = (*anIt)->feature();
126       bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
127       if (aFeatAux != aBaseAux)
128         theDumper << theDumper.name((*anIt)->feature(), false)
129                   << ".setAuxiliary(" << aFeatAux << ")" <<std::endl;
130     }
131   }
132 }