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