Salome HOME
Update pour separator
[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                 #part.removeFeature(self.folder.feature())
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                     x = float(coord[0]); y = float(coord[1]); z = float(coord[2]);
81                     point = model.addPoint(part, x,y,z); point.execute(True); self.lfeatures.append(point)
82                     vertex = model.addVertex(part, [point.result()]); vertex.execute(True); self.lfeatures.append(vertex)
83                     lVertices.append(vertex.result())
84                 file.close()
85                 compound = model.addCompound(part, lVertices)
86                 compound.execute(True); self.lfeatures.append(compound)
87                 compound.result().setName(nameRes)
88                 self.folder = model.addFolder(part, self.lfeatures[0], compound)
89                 self.folder.setName(nameRes)
90                 return
91         
92             setError("The file does not exist")
93
94     def isMacro(self):
95         """Override Feature.initAttributes().
96         F.isMacro() -> True
97
98         compoundVertices feature is macro: removes itself on the creation transaction
99         finish.
100         """
101         return True