Salome HOME
Add field in macro
[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     def getKind(self):
30         """Override Feature.getKind()"""
31         return compoundVertices.ID()
32
33
34 # Initialization of the dialog panel
35
36     def initAttributes(self):
37         """Override Feature.initAttributes()"""
38         # Creating the input argument of the feature
39         self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString_typeId())
40
41 # Execution of the Import
42
43     def execute(self):
44         """F.execute() -- execute the Feature"""
45         # Retrieving the user input
46         apath    = self.string(self.FILE_ID())
47         filepath = apath.value()
48         print("filepath : ", filepath)
49         if filepath != "" :
50             
51             from os.path import basename
52             filename = basename(filepath)
53             print("filename : ", filename)
54
55             # Creating the construction points in the current document
56             part = model.activeDocument()
57             lVertices = []
58
59             startPoint = None
60
61             with open(filepath) as file:
62                 for line in file:
63                     coord = line.split(' ')
64                     x = float(coord[0]); y = float(coord[1]); z = float(coord[2]);
65                     point = model.addPoint(part, x,y,z); point.execute(True)
66                     if startPoint == None : startPoint = point
67                     vertex = model.addVertex(part, [point.result()]); vertex.execute(True)
68                     lVertices.append(vertex.result())
69                 file.close()
70                 compound = model.addCompound(part, lVertices)
71                 compound.execute(True)
72                 folder = model.addFolder(part, startPoint, compound)
73                 folder.setName(filename)
74                 return
75         
76             setError("The file does not exist")
77