]> SALOME platform Git repositories - modules/shaper.git/blob - src/PythonAddons/macros/compoundVertices/feature.py
Salome HOME
Issue #3194: Do not add parameters with already existing names
[modules/shaper.git] / src / PythonAddons / macros / compoundVertices / feature.py
1 """compound of vertices Feature
2 Author: Nathalie Gore
3 """
4
5 from salome.shaper import model
6 from salome.shaper import geom
7 import ModelAPI
8
9 class compoundVertices(model.Feature):
10     """Import of Construction points
11     """
12
13 # Feature initializations
14
15     def __init__(self):
16         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
17         model.Feature.__init__(self)
18
19     @staticmethod
20     def ID():
21         """Return Id of the Feature."""
22         return "compoundVertices"
23
24     @staticmethod
25     def FILE_ID():
26         """Returns ID of the file select parameter."""
27         return "file_path"
28
29     @staticmethod
30     def SEPARATOR_ID():
31         """Returns ID of the separator parameter."""
32         return "separator"
33
34     def getKind(self):
35         """Override Feature.getKind()"""
36         return compoundVertices.ID()
37
38
39 # Initialization of the dialog panel
40
41     def initAttributes(self):
42         """Override Feature.initAttributes()"""
43         # Creating the input argument of the feature
44         self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString_typeId())
45         self.data().addAttribute(self.SEPARATOR_ID(), ModelAPI.ModelAPI_AttributeString_typeId())
46
47         self.lfeatures = []
48         self.folder = None
49         self.separator = " "
50
51 # Execution of the Import
52
53     def execute(self):
54         """F.execute() -- execute the Feature"""
55         # Retrieving the user input
56         apath    = self.string(self.FILE_ID())
57         aseparator = self.string(self.SEPARATOR_ID()).value()
58         if aseparator:
59             self.separator = aseparator
60
61         filepath = apath.value()
62         if filepath != "" :
63             part = model.activeDocument()
64             if self.lfeatures :
65                 for feature in self.lfeatures:
66                    part.removeFeature(feature.feature())
67                 self.lfeatures = []
68                 model.removeFolder(self.folder)
69
70             from os.path import basename
71             filename = basename(filepath)
72             nameRes = "Points_" + filename
73
74             # Creating the construction points in the current document
75             lVertices = []
76
77             with open(filepath) as file:
78                 for line in file:
79                     coord = line.split(self.separator)
80                     if len(coord) != 3:
81                         return
82                     x = float(coord[0]); y = float(coord[1]); z = float(coord[2]);
83                     point = model.addPoint(part, x,y,z); point.execute(True); self.lfeatures.append(point)
84                     #vertex = model.addVertex(part, [point.result()]); vertex.execute(True); self.lfeatures.append(vertex)
85                     lVertices.append(point.result())
86                 file.close()
87                 compound = model.addCompound(part, lVertices)
88                 compound.execute(True); self.lfeatures.append(compound)
89                 compound.result().setName(nameRes)
90                 self.folder = model.addFolder(part, self.lfeatures[0], compound)
91                 self.folder.setName(nameRes)
92                 return
93
94             setError("The file does not exist")
95
96     def isMacro(self):
97         """Override Feature.initAttributes().
98         F.isMacro() -> True
99
100         compoundVertices feature is macro: removes itself on the creation transaction
101         finish.
102         """
103         return False