Salome HOME
Issue #3086: Avoid crash when FeatureInfo is null.
[modules/shaper.git] / src / SketchAPI / SketchAPI_Translation.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_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(theObjects, translationList());
46     fillAttribute(thePoint1, startPoint());
47     fillAttribute(thePoint2, endPoint());
48     fillAttribute(theNumberOfObjects, numberOfObjects());
49     fillAttribute(theFullValue ? "FullValue" : "SingleValue", valueType());
50
51     execute(true);
52   }
53 }
54
55 SketchAPI_Translation::~SketchAPI_Translation()
56 {
57
58 }
59
60 std::list<std::shared_ptr<SketchAPI_SketchEntity> > SketchAPI_Translation::translated() const
61 {
62   std::list<ObjectPtr> aList = translatedObjects()->list();
63   // remove all initial features
64   std::list<FeaturePtr> anIntermediate;
65   std::list<ObjectPtr>::const_iterator anIt = aList.begin();
66   for (; anIt != aList.end(); ++anIt) {
67     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
68     AttributeBooleanPtr isCopy = aFeature->boolean(SketchPlugin_SketchEntity::COPY_ID());
69     if (isCopy.get() && isCopy->value())
70       anIntermediate.push_back(aFeature);
71   }
72   return SketchAPI_SketchEntity::wrap(anIntermediate);
73 }
74
75 //--------------------------------------------------------------------------------------
76
77 void SketchAPI_Translation::dump(ModelHighAPI_Dumper& theDumper) const
78 {
79   FeaturePtr aBase = feature();
80   const std::string& aSketchName = theDumper.parentName(aBase);
81
82   AttributeRefListPtr aTransObjects = translationList();
83   AttributeRefAttrPtr aStart = startPoint();
84   AttributeRefAttrPtr aEnd   = endPoint();
85   AttributeIntegerPtr aNbCopies = numberOfObjects();
86   bool isFullValue = valueType()->value() != "SingleValue";
87
88   // Check all attributes are already dumped. If not, store the constraint as postponed.
89   if (!theDumper.isDumped(aStart) || !theDumper.isDumped(aEnd) ||
90       !theDumper.isDumped(aTransObjects)) {
91     theDumper.postpone(aBase);
92     return;
93   }
94
95   theDumper << aBase << " = " << aSketchName << ".addTranslation("
96             << aTransObjects << ", " << aStart << ", " << aEnd << ", " << aNbCopies;
97   if (isFullValue)
98     theDumper << ", " << isFullValue;
99   theDumper << ")" << std::endl;
100
101   // Dump variables for a list of translated features
102   theDumper << "[";
103   std::list<std::shared_ptr<SketchAPI_SketchEntity> > aList = translated();
104   std::list<std::shared_ptr<SketchAPI_SketchEntity> >::const_iterator anIt = aList.begin();
105   for (; anIt != aList.end(); ++anIt) {
106     if (anIt != aList.begin())
107       theDumper << ", ";
108     theDumper << (*anIt)->feature();
109   }
110   theDumper << "] = " << theDumper.name(aBase) << ".translated()" << std::endl;
111
112   // Set necessary "auxiliary" flag for translated features
113   // (flag is set if it differs to base entity)
114   std::list<ObjectPtr> aTransList = aTransObjects->list();
115   std::list<ObjectPtr>::const_iterator aTrIt = aTransList.begin();
116   anIt = aList.begin();
117   for (; aTrIt != aTransList.end(); ++aTrIt) {
118     FeaturePtr aFeature = ModelAPI_Feature::feature(*aTrIt);
119     if (!aFeature)
120       continue;
121     bool aBaseAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
122
123     for (int i = 1; i < aNbCopies->value(); ++i, ++anIt) {
124       aFeature = (*anIt)->feature();
125       bool aFeatAux = aFeature->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value();
126       if (aFeatAux != aBaseAux)
127         theDumper << theDumper.name((*anIt)->feature(), false)
128                   << ".setAuxiliary(" << aFeatAux << ")" <<std::endl;
129     }
130   }
131 }