Salome HOME
updated copyright message
[modules/shaper.git] / src / PythonAddons / macros / compoundVertices / feature.py
old mode 100644 (file)
new mode 100755 (executable)
index c8e5fed..7230c42
@@ -1,3 +1,22 @@
+# Copyright (C) 2016-2023  CEA, EDF
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
+
 """compound of vertices Feature
 Author: Nathalie Gore
 """
@@ -26,6 +45,11 @@ class compoundVertices(model.Feature):
         """Returns ID of the file select parameter."""
         return "file_path"
 
+    @staticmethod
+    def SEPARATOR_ID():
+        """Returns ID of the separator parameter."""
+        return "separator"
+
     def getKind(self):
         """Override Feature.getKind()"""
         return compoundVertices.ID()
@@ -36,7 +60,12 @@ class compoundVertices(model.Feature):
     def initAttributes(self):
         """Override Feature.initAttributes()"""
         # Creating the input argument of the feature
-        self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString_typeId())
+        self.data().addAttribute(self.FILE_ID(), ModelAPI.ModelAPI_AttributeString.typeId())
+        self.data().addAttribute(self.SEPARATOR_ID(), ModelAPI.ModelAPI_AttributeString.typeId())
+
+        self.lfeatures = []
+        self.folder = None
+        self.separator = " "
 
 # Execution of the Import
 
@@ -44,35 +73,43 @@ class compoundVertices(model.Feature):
         """F.execute() -- execute the Feature"""
         # Retrieving the user input
         apath    = self.string(self.FILE_ID())
+        aseparator = self.string(self.SEPARATOR_ID()).value()
+        if aseparator:
+            self.separator = aseparator
+
         filepath = apath.value()
-        print("filepath : ", filepath)
         if filepath != "" :
-            
+            part = model.activeDocument()
+            if self.lfeatures :
+                for feature in self.lfeatures:
+                   part.removeFeature(feature.feature())
+                self.lfeatures = []
+                model.removeFolder(self.folder)
+
             from os.path import basename
             filename = basename(filepath)
-            print("filename : ", filename)
+            nameRes = "Points_" + filename
 
             # Creating the construction points in the current document
-            part = model.activeDocument()
             lVertices = []
 
-            startPoint = None
-
             with open(filepath) as file:
                 for line in file:
-                    coord = line.split(' ')
+                    coord = line.split(self.separator)
+                    if len(coord) != 3:
+                        return
                     x = float(coord[0]); y = float(coord[1]); z = float(coord[2]);
-                    point = model.addPoint(part, x,y,z); point.execute(True)
-                    if startPoint == None : startPoint = point
-                    vertex = model.addVertex(part, [point.result()]); vertex.execute(True)
-                    lVertices.append(vertex.result())
+                    point = model.addPoint(part, x,y,z); point.execute(True); self.lfeatures.append(point)
+                    #vertex = model.addVertex(part, [point.result()]); vertex.execute(True); self.lfeatures.append(vertex)
+                    lVertices.append(point.result())
                 file.close()
                 compound = model.addCompound(part, lVertices)
-                compound.execute(True)
-                folder = model.addFolder(part, startPoint, compound)
-                folder.setName(filename)
+                compound.execute(True); self.lfeatures.append(compound)
+                compound.result().setName(nameRes)
+                self.folder = model.addFolder(part, self.lfeatures[0], compound)
+                self.folder.setName(nameRes)
                 return
-        
+
             setError("The file does not exist")
 
     def isMacro(self):
@@ -82,4 +119,4 @@ class compoundVertices(model.Feature):
         compoundVertices feature is macro: removes itself on the creation transaction
         finish.
         """
-        return True                    
+        return False