]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAddons/macros/importParameters/feature.py
Salome HOME
neutral fiber macro
[modules/shaper.git] / src / PythonAddons / macros / importParameters / feature.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2016-2021  CEA/DEN, EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 """importParameters
22 Author: Nathalie Gore
23 """
24 import os
25
26 from salome.shaper import model
27
28 import ModelAPI
29 import ParametersAPI
30
31 class importParameters(model.Feature):
32     """Import of Construction points
33     """
34
35 # Feature initializations
36
37     def __init__(self):
38         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
39         model.Feature.__init__(self)
40
41     @staticmethod
42     def ID():
43         """Return Id of the Feature."""
44         return "importParameters"
45
46     @staticmethod
47     def FILE_ID():
48         """Returns ID of the file."""
49         return "file_path"
50
51     def getKind(self):
52         """Override Feature.getKind()"""
53         return importParameters.ID()
54
55
56 # Initialization of the dialog panel
57
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())
62
63
64 # Get existing parameters names
65
66     def existingParameters(self):
67         """ Returns list of already existing parameters names"""
68         aDoc = model.activeDocument()
69         aNbFeatures = aDoc.numInternalFeatures()
70         aNames = list()
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())
76         return aNames
77
78
79 # Execution of the Import
80
81     def execute(self):
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))
87         if 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:
93                     for line in 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])
99                     fic.close()
100             else:
101                 self.setError("The file '{}' does not exist".format(filepath))
102
103         return
104
105     def isMacro(self):
106         """Override Feature.initAttributes().
107         F.isMacro() -> True
108
109         importParameters feature is macro: removes itself on the creation transaction
110         finish.
111         """
112         return True