Salome HOME
updated copyright message
[modules/shaper.git] / src / PythonAddons / macros / compoundVertices / feature.py
1 # Copyright (C) 2016-2023  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """compound of vertices Feature
21 Author: Nathalie Gore
22 """
23
24 from salome.shaper import model
25 from salome.shaper import geom
26 import ModelAPI
27
28 class compoundVertices(model.Feature):
29     """Import of Construction points
30     """
31
32 # Feature initializations
33
34     def __init__(self):
35         """x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
36         model.Feature.__init__(self)
37
38     @staticmethod
39     def ID():
40         """Return Id of the Feature."""
41         return "compoundVertices"
42
43     @staticmethod
44     def FILE_ID():
45         """Returns ID of the file select parameter."""
46         return "file_path"
47
48     @staticmethod
49     def SEPARATOR_ID():
50         """Returns ID of the separator parameter."""
51         return "separator"
52
53     def getKind(self):
54         """Override Feature.getKind()"""
55         return compoundVertices.ID()
56
57
58 # Initialization of the dialog panel
59
60     def initAttributes(self):
61         """Override Feature.initAttributes()"""
62         # Creating the input argument of the feature
63         self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString.typeId())
64         self.data().addAttribute(self.SEPARATOR_ID(), ModelAPI.ModelAPI_AttributeString.typeId())
65
66         self.lfeatures = []
67         self.folder = None
68         self.separator = " "
69
70 # Execution of the Import
71
72     def execute(self):
73         """F.execute() -- execute the Feature"""
74         # Retrieving the user input
75         apath    = self.string(self.FILE_ID())
76         aseparator = self.string(self.SEPARATOR_ID()).value()
77         if aseparator:
78             self.separator = aseparator
79
80         filepath = apath.value()
81         if filepath != "" :
82             part = model.activeDocument()
83             if self.lfeatures :
84                 for feature in self.lfeatures:
85                    part.removeFeature(feature.feature())
86                 self.lfeatures = []
87                 model.removeFolder(self.folder)
88
89             from os.path import basename
90             filename = basename(filepath)
91             nameRes = "Points_" + filename
92
93             # Creating the construction points in the current document
94             lVertices = []
95
96             with open(filepath) as file:
97                 for line in file:
98                     coord = line.split(self.separator)
99                     if len(coord) != 3:
100                         return
101                     x = float(coord[0]); y = float(coord[1]); z = float(coord[2]);
102                     point = model.addPoint(part, x,y,z); point.execute(True); self.lfeatures.append(point)
103                     #vertex = model.addVertex(part, [point.result()]); vertex.execute(True); self.lfeatures.append(vertex)
104                     lVertices.append(point.result())
105                 file.close()
106                 compound = model.addCompound(part, lVertices)
107                 compound.execute(True); self.lfeatures.append(compound)
108                 compound.result().setName(nameRes)
109                 self.folder = model.addFolder(part, self.lfeatures[0], compound)
110                 self.folder.setName(nameRes)
111                 return
112
113             setError("The file does not exist")
114
115     def isMacro(self):
116         """Override Feature.initAttributes().
117         F.isMacro() -> True
118
119         compoundVertices feature is macro: removes itself on the creation transaction
120         finish.
121         """
122         return False