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