1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2016-2021 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 from salome.shaper import model
31 class importParameters(model.Feature):
32 """Import of Construction points
35 # Feature initializations
38 """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
39 model.Feature.__init__(self)
43 """Return Id of the Feature."""
44 return "importParameters"
48 """Returns ID of the file."""
52 """Override Feature.getKind()"""
53 return importParameters.ID()
56 # Initialization of the dialog panel
58 def initAttributes(self):
59 """Override Feature.initAttributes()"""
60 # Creating the input argument of the feature
61 self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString_typeId())
64 # Get existing parameters names
66 def existingParameters(self):
67 """ Returns list of already existing parameters names"""
68 aDoc = model.activeDocument()
69 aNbFeatures = aDoc.numInternalFeatures()
71 for i in range(aNbFeatures):
72 aParamFeature = aDoc.internalFeature(i)
73 if aParamFeature is not None:
74 if aParamFeature.getKind() == ParametersAPI.ParametersAPI_Parameter.ID():
75 aNames.append(aParamFeature.name())
79 # Execution of the Import
82 """F.execute() -- execute the Feature"""
83 # Retrieving the user input
84 apath = self.string(self.FILE_ID())
85 filepath = apath.value()
86 #print("filepath : '{}'".format(filepath))
88 if os.path.isfile(filepath):
89 # Creating the parameters in the current document
90 part = model.activeDocument()
91 aNames = self.existingParameters()
92 with open(filepath) as fic:
94 defParameters = line.replace("\n","").split(' ')
95 if len(defParameters) == 2 :
96 if defParameters[0] not in aNames:
97 model.addParameter(part, defParameters[0], defParameters[1])
98 aNames.append(defParameters[0])
101 self.setError("The file '{}' does not exist".format(filepath))
106 """Override Feature.initAttributes().
109 importParameters feature is macro: removes itself on the creation transaction