Salome HOME
Updated copyright comment
[modules/shaper.git] / src / PythonAddons / macros / importParameters / feature.py
1 # Copyright (C) 2016-2024  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 """importParameters
21 Author: Nathalie Gore
22 """
23
24 from salome.shaper import model
25 from salome.shaper import geom
26 import ModelAPI
27 import ParametersAPI
28
29 class importParameters(model.Feature):
30     """Import of Construction points
31     """
32
33 # Feature initializations
34
35     def __init__(self):
36         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
37         model.Feature.__init__(self)
38
39     @staticmethod
40     def ID():
41         """Return Id of the Feature."""
42         return "importParameters"
43
44     @staticmethod
45     def FILE_ID():
46         """Returns ID of the file select parameter."""
47         return "file_path"
48
49     def getKind(self):
50         """Override Feature.getKind()"""
51         return importParameters.ID()
52
53
54 # Initialization of the dialog panel
55
56     def initAttributes(self):
57         """Override Feature.initAttributes()"""
58         # Creating the input argument of the feature
59         self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString.typeId())
60
61
62 # Get existing parameters names
63
64     def existingParameters(self):
65         """ Returns list of already existing parameters names"""
66         aDoc = model.activeDocument()
67         aNbFeatures = aDoc.numInternalFeatures();
68         aNames = []
69         for i in range(aNbFeatures):
70             aParamFeature = aDoc.internalFeature(i);
71             if aParamFeature is not None:
72                 if aParamFeature.getKind() == ParametersAPI.ParametersAPI_Parameter.ID():
73                     aNames.append(aParamFeature.name())
74         return aNames
75
76
77 # Execution of the Import
78
79     def execute(self):
80         """F.execute() -- execute the Feature"""
81         # Retrieving the user input
82         apath    = self.string(self.FILE_ID())
83         filepath = apath.value()
84         #print("filepath : ", filepath)
85         if filepath != "" :
86
87             # Creating the parameters in the current document
88             part = model.activeDocument()
89             aNames = self.existingParameters()
90
91             with open(filepath) as file:
92                 for line in file:
93                     defParameters = line.replace("\n","").split(' ')
94                     if len(defParameters) == 2 :
95                         if defParameters[0] not in aNames:
96                             model.addParameter(part, defParameters[0], defParameters[1])
97                             aNames.append(defParameters[0])
98                 file.close()
99                 return
100
101             setError("The file does not exist")
102
103     def isMacro(self):
104         """Override Feature.initAttributes().
105         F.isMacro() -> True
106
107         importParameters feature is macro: removes itself on the creation transaction
108         finish.
109         """
110         return True