]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAddons/macros/importParameters/feature.py
Salome HOME
Issue #3194: Do not add parameters with already existing names
[modules/shaper.git] / src / PythonAddons / macros / importParameters / feature.py
1 """importParameters
2 Author: Nathalie Gore
3 """
4
5 from salome.shaper import model
6 from salome.shaper import geom
7 import ModelAPI
8 import ParametersAPI
9
10 class importParameters(model.Feature):
11     """Import of Construction points
12     """
13
14 # Feature initializations
15
16     def __init__(self):
17         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
18         model.Feature.__init__(self)
19
20     @staticmethod
21     def ID():
22         """Return Id of the Feature."""
23         return "importParameters"
24
25     @staticmethod
26     def FILE_ID():
27         """Returns ID of the file select parameter."""
28         return "file_path"
29
30     def getKind(self):
31         """Override Feature.getKind()"""
32         return importParameters.ID()
33
34
35 # Initialization of the dialog panel
36
37     def initAttributes(self):
38         """Override Feature.initAttributes()"""
39         # Creating the input argument of the feature
40         self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString_typeId())
41
42
43 # Get existing parameters names
44
45     def existingParameters(self):
46         """ Returns list of already existing parameters names"""
47         aDoc = model.activeDocument()
48         aNbFeatures = aDoc.numInternalFeatures();
49         aNames = []
50         for i in range(aNbFeatures):
51             aParamFeature = aDoc.internalFeature(i);
52             if aParamFeature is not None:
53                 if aParamFeature.getKind() == ParametersAPI.ParametersAPI_Parameter.ID():
54                     aNames.append(aParamFeature.name())
55         return aNames
56
57
58 # Execution of the Import
59
60     def execute(self):
61         """F.execute() -- execute the Feature"""
62         # Retrieving the user input
63         apath    = self.string(self.FILE_ID())
64         filepath = apath.value()
65         #print("filepath : ", filepath)
66         if filepath != "" :
67
68             # Creating the parameters in the current document
69             part = model.activeDocument()
70             aNames = self.existingParameters()
71
72             with open(filepath) as file:
73                 for line in file:
74                     defParameters = line.replace("\n","").split(' ')
75                     if len(defParameters) == 2 :
76                         if defParameters[0] not in aNames:
77                             model.addParameter(part, defParameters[0], defParameters[1])
78                             aNames.append(defParameters[0])
79                 file.close()
80                 return
81
82             setError("The file does not exist")
83
84     def isMacro(self):
85         """Override Feature.initAttributes().
86         F.isMacro() -> True
87
88         importParameters feature is macro: removes itself on the creation transaction
89         finish.
90         """
91         return True