7 Replies Latest reply on Dec 1, 2017 1:44 PM by Fatih Mehmet Ozcan

    Create an Assembly with API with user define folder

    Gaurav Bhonsule

      Hello,

      I want to create an assembly with API.

      Task 1: User can define/select folder.( (Preferably with dialog box or can be ok do it with edit code for every run)

      Task 2: call all parts from folder in assembly & set it at origin.( default set)

      Task 3: Save in same folder with user define name for assembly. (Preferably with dialog box or can be ok do it with edit code for every run)

      Task 4:Close Solidworks.

       

      Regards,

      Gaurav

        • Re: Create an Assembly with API with user define folder
          Alex Burnett

          This sounds like an interesting problem to solve. You will get more involvement from the users on here if you begin to tackle this yourself and come here with specific questions when you hit a roadblock. You'll be hard pressed to find someone to just write this all for you as I estimate it could take quite a bit longer than just a few minutes.

          • Re: Create an Assembly with API with user define folder
            Deepak Gupta

            Have you tried recording a macro? If you look around on this forum and/or SOLIDWORKS API helps files, you can find the required puzzle pieces to solve this

             

            Task 1: User can define/select folder. Browse for folder in SW Macro

            Task 2: call all parts from folder in assembly & set it at origin Add and Mate Component Example (VBA)

            Task 3: Save in same folder with user define name for assembly. Browse for folder in SW Macro

            Task 4:Close Solidworks. ExitApp Method (ISldWorks)

            • Re: Create an Assembly with API with user define folder
              Fatih Mehmet Ozcan

              I started writing this and tomorrow it will be finished if you wait for it

                • Re: Create an Assembly with API with user define folder
                  Gaurav Bhonsule

                  Hello Faith,

                   

                  Really appreciate your help.

                  As I am working on this & surely not able to finish in days time.

                  I am looking forward for your code.

                   

                  Regards,

                  Gaurav

                    • Re: Create an Assembly with API with user define folder
                      Fatih Mehmet Ozcan

                      Hello, this is my code, I got to the point where all parts are inserted in new assembly. They are not positioned as their own origin, this needs to be worked on.

                       

                      My code is not professional. I used GetOpenFileName to get folder name. Solidworks doesn't support open folder dialog box as I know and I didn't want to use any reference not in Solidworks so It got a bit dirty. I explained in the code

                       

                       

                       

                      Dim swApp As SldWorks.SldWorks

                      Sub main()

                       

                       

                      Set swApp = Application.SldWorks

                       

                       

                       

                       

                       

                       

                      'this is where I used file open dialog box to get file name and folder name

                      'I search all "\" characters which For Each and InStr thourgh all characters to determine where file name begins, to do that I used InStr and made an array

                      'to record positions of "\" characters

                      'after that I used Right and Left method to seperate path and file name to variables

                      'the reason of this "UBound(fol) - 2)" is that when InStr couldn't find after last "\" character it returns a zero

                      'I made this changed to "jump1"

                      'and continues from that to beginning of file string. finds first "\" character again

                      'I made an if statement to check if any of the array members are same within each other, so when it finds same

                      'thing it changes that one to "jump2" which is first "\" character

                       

                       

                      Dim filepath

                      Dim fol() As Variant

                      Dim folsay

                      Dim foltemp

                      Dim filename

                      Dim folderpath

                       

                       

                          Dim Filter As String

                          Dim fileConfig As String

                          Dim fileDispName As String

                          Dim fileOptions As Long

                      Filter = "SOLIDWORKS Files (*.sldprt)|*.sldprt;"

                      filepath = swApp.GetOpenFileName("File for Folder", "", Filter, fileOptions, fileConfig, fileDispName)

                       

                       

                      If filepath <> Empty Then

                      'MsgBox filepath

                      For i = 1 To Len(filepath)

                      folsay = folsay + 1

                      If i >= 2 Then

                      ReDim Preserve fol(1 To i)

                      fol(folsay) = InStr(fol(folsay - 1) + 1, filepath, "\")

                          foltemp = fol(folsay)

                          For k = LBound(fol()) To UBound(fol()) - 1

                              If fol(k) = foltemp Then

                              fol(UBound(fol)) = "jump2"

                              fol(UBound(fol) - 1) = "jump1"

                              GoTo Devam

                              End If

                          Next

                      Else

                      ReDim Preserve fol(1 To i)

                      fol(folsay) = InStr(filepath, "\")

                      End If

                      Next

                       

                       

                      Devam:

                       

                       

                      Else

                      Exit Sub

                      End If

                       

                       

                      filename = Right(filepath, Len(filepath) - fol(UBound(fol) - 2))

                      folderpath = Left(filepath, fol(UBound(fol) - 2))

                      'MsgBox filename & vbCrLf & folderpath

                       

                       

                       

                       

                       

                       

                       

                       

                      'searched through folder's files to check solidworks parts

                      'opened them one by one and recorded their title to use when adding parts in assembly

                       

                       

                      Dim fol1

                      Dim fol2

                      Set fol1 = CreateObject("scripting.filesystemobject")

                      Set fol2 = fol1.GetFolder(folderpath)

                      Dim swpart As SldWorks.ModelDoc2

                      Dim err As Long

                      Dim warr As Long

                      Dim opendoc()

                      Dim orderopen

                       

                       

                      For Each fi In fol2.Files

                      If InStr(fi.Name, "sldprt") Then

                          Set swpart = swApp.OpenDoc6(fi, swDocPART, swOpenDocOptions_Silent, "", err, warr)

                          orderopen = orderopen + 1

                          ReDim Preserve opendoc(1 To orderopen)

                          opendoc(orderopen) = swpart.GetTitle

                          'MsgBox swpart.GetTitle

                      End If

                      Next

                       

                       

                       

                       

                       

                       

                      'I made a new assembly here

                      'activated it

                      'and added components which are opened at the same time and closed them after that

                       

                       

                      Dim swmodel As SldWorks.ModelDoc2

                      Dim swasm As SldWorks.AssemblyDoc

                      Dim longstatus As Long

                      Dim status As Long

                      Dim status2 As Long

                      Dim assytemplate

                      assytemplate = swApp.GetUserPreferenceStringValue(swDefaultTemplateAssembly)

                      Dim activetitle

                      Dim swcomponent As SldWorks.Component2

                       

                       

                      Dim swSheetWidth As Double

                      swSheetWidth = 0

                      Dim swSheetHeight As Double

                      swSheetHeight = 0

                      Set swmodel = swApp.NewDocument(assytemplate, 0, swSheetWidth, swSheetHeight)

                       

                       

                      activetitle = swmodel.GetTitle

                      swApp.ActivateDoc2 activetitle, False, longstatus

                      Set swmodel = swApp.ActiveDoc

                      Set swasm = swmodel

                       

                       

                      For alfa = LBound(opendoc) To UBound(opendoc)

                      Set swcomponent = swasm.AddComponent5(opendoc(alfa), swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", -1, -1, -1)

                      swApp.CloseDoc (opendoc(alfa))

                      Next

                       

                       

                      swmodel.ViewZoomtofit2

                       

                       

                      End Sub