5 Replies Latest reply on Apr 8, 2016 11:24 PM by Josh Brady

    API importing multiple txt file as xyz curves

    Shar Fa

      Hi guys!


      The Background

      I have multiple text files with increment titles (Solid_Curve_1....Solid_Curve_2 ... Solid_Curve_21 ...)

      Each set of coordinates (e.g Solid_Curve_1) contains a set of xyz points in space:

      -0.006477 0.003790 0.016829

      -0.006301 0.004081 0.016760

      -0.006102 0.004171 0.016738

      -0.005820 0.004210 0.016728

      -0.005429 0.004265 0.016714

      -0.004933 0.004336 0.016696

      -0.004332 0.004418 0.016675

      ...................and goes on.........


      The Goal

      I want each set to do a curve through XYZ points to create the profiles, by opening a dialog box giving the option to choose which txt files i want to import + create3dsketch + convert entities to a new part.



      Reference (tried working the macros + editing = no success so far...)

      macro for creating curves reading coordinates from textpad

      Macro for curve through xyz to import several curves in one

      Importing multiple data sets from text file

      Is there any way to import multiple XYZ curves from a single file?



      Thanks !

        • Re: API importing multiple txt file as xyz curves
          Elmar Klammer

          Try this from Stefan Berlitz



          Sub main()
              Dim swApp As Object
              Dim Part As Object
              Dim boolstatus As Boolean
              ' an SolidWorks anhängen
              Set swApp = Application.SldWorks
              ' aktives Modell holen
              Set Part = swApp.ActiveDoc
              ' und nacheinander die Kurvendateien einlesen
              boolstatus = Part.InsertCurveFile("c:\temp\kurve-params-01.txt")
              boolstatus = Part.InsertCurveFile("c:\temp\kurve-params-02.txt")
              boolstatus = Part.InsertCurveFile("c:\temp\kurve-params-03.txt")
              ' usw.

          End Sub


          GOTO Browse for folder in SW Macro  or copy all text files into a single temp folder and traverse all files within. There is plenty samples in the forum to do that.

          With a little effort you should be able to put that together.



          • Re: API importing multiple txt file as xyz curves
            Shar Fa

            I am trying to make it work for multiselect:=1 text files in a way that "Path" variable would contain the paths of all selected text file and

            boolstatus = Part.InsertCurveFile(Path) would create the curves for each selected text.

            It now works for one selection each time :-S


            Sub Main()


              Dim swApp As SldWorks.SldWorks

              Dim Path As String

              Dim fileOptions As Long

              Dim fileConfigName As String

              Dim fileDisplayName As String


              Set swApp = Application.SldWorks

              Path = swApp.GetOpenFileName("Select CS", "", "Text files (*.txt;)|", fileOptions, fileConfigName, fileDisplayName)

              %how to add multiselect and define Path as array?%


                Dim Part As Object

                Dim boolstatus As Boolean

                Set Part = swApp.ActiveDoc


                boolstatus = Part.InsertCurveFile(Path)

            %how to create the curves from previously selected txt files one after the other auto.%


            End Sub

              • Re: API importing multiple txt file as xyz curves
                Michael Spens

                Hello Shar Fa,

                Unfortunately, GetOpenFileName doesn't have the ability to multi-select.  VBA macros are not the easiest place to create file browser dialogs.  If possible, I might suggest moving to VB.NET or C# macros to make file dialog operations easier to manage.  For example, this would be the main procedure in a VB.NET macro.


                    Public Sub Main()



                        Dim Path As String



                        Dim Part As PartDoc

                        Dim boolstatus As Boolean

                        Part = swApp.ActiveDoc



                        'open files

                        Dim ofd As New Windows.Forms.OpenFileDialog

                        ofd.Multiselect = True

                        ofd.Filter = "Text files (*.txt)|*.txt"

                        ofd.Title = "Select CS"

                        Dim dialogRes As Windows.Forms.DialogResult = ofd.ShowDialog

                        If dialogRes = Windows.Forms.DialogResult.OK Then

                            Dim paths() As String = ofd.FileNames

                            For Each Path In paths

                                boolstatus = Part.InsertCurveFile(Path)


                        End If



                    End Sub