18 Replies Latest reply on Jun 15, 2015 5:33 AM by Willie Roelofs

    Cutlist Save Bodies Macro

    Willie Roelofs

      Hi,

       

      I want to write a macro that saves the weldment part as an assembly and each individual structural member as a part. I want to give all of them a custom name. But I don't know how to acces the Save Bodies feature via VBA.

       

      Could someone help me with that?

       

       

      With kind regards,

      Willie

        • Re: Cutlist Save Bodies Macro
          Keith Rice

          If you're talking about right clicking on a body --> Insert Into New Part, this is done in the API by selecting the body using IModelDocExtension::SelectByID2 and then using IPartDoc::SaveToFile3. Once all of these weldment members are saved as their own parts, you could insert them into an assembly and mate them via the API. Accomplishing all of this via API would be pretty complex, however.

           

          If this isn't what you're trying to do then please provide more details, such as describing how you already perform this task via the GUI.

           

          Keith

          SolidWorks API Training

          • Re: Cutlist Save Bodies Macro
            Alex Henry

            Hello - Couldn't you call the functionality that already exists

             

            Note: I am not an API person so this may be too simplistic for your task; ignore if so

             

              • Re: Cutlist Save Bodies Macro
                Willie Roelofs

                You are right, I can fill in manually the names of each part and assembly. The problem however is that I have a lot of weldments and each weldment contains 40 or more different structural members.

                 

                If I fill in manually the name of each structural member it takes me 15 minutes because of message boxes that tell me which character I can't use (even though I just double clicked to fill in the name, and haven't typed in a single character).

                 

                What I want is to save the weldment as:

                xxx-xxx-xxx-00.SLDPRT

                 

                The first part gets the number:

                xxx-xxx-xxx-01.SLDPRT

                Each following part gets i+1

                 

                The assembly gets the same name as the weldment, except for it's file extension.

                 

                This is an easy macro in my opinion. The problem is that I'm not that familier with vba-solidworks language.

                  • Re: Cutlist Save Bodies Macro
                    Willie Roelofs

                    I found the following code on soldiworks API. It wasn't working, it couldn't find bodies in the bodyfolder, so I changed it to CutListFolder and works. There are 2 different bodies in the cutlist folder and v1 does select them. Everything should work, except for the last line. No faults are found but no bodies are saved also.

                     

                     

                    What am I doing wrong?

                     

                     

                    Dim swApp As SldWorks.SldWorks

                    Dim swModel As SldWorks.ModelDoc2

                    Dim swSelMgr As SldWorks.SelectionMgr

                    Dim swFeat As SldWorks.Feature

                    Dim swFeatMgr As SldWorks.FeatureManager

                    Dim swBodyFolder As SldWorks.BodyFolder

                    Dim v1, fileName() As Variant

                    Dim i As Long

                    Dim nummer As String

                    Dim count As Integer

                    Dim bret As Boolean

                    Dim iets As IFeature

                     

                    Sub GetVariantOfBody(swFeature As SldWorks.Feature, bodyList As Variant)

                        Dim tt As Variant

                      

                        Set swBodyFolder = swFeature.GetSpecificFeature2

                      

                        count = swBodyFolder.GetBodyCount

                      

                        If (count < 1) Then

                            MsgBox ("There are no bodies. Please create a body.")

                        Else

                            bodyList = swBodyFolder.GetBodies

                        End If

                    End Sub

                     

                    Sub main()

                     

                     

                        Set swApp = Application.SldWorks

                        Set swModel = swApp.ActiveDoc

                        Set swSelMgr = swModel.SelectionManager

                        Set swFeat = swModel.FirstFeature

                        Set swFeatMgr = swModel.FeatureManager

                     

                        a = 0

                     

                        Dim contLoop As Boolean

                        contLoop = True

                        While Not swFeat Is Nothing And contLoop = True

                            Dim Name As String

                            Name = swFeat.GetTypeName2

                            If (Name = "CutListFolder") Then

                                GetVariantOfBody swFeat, v1

                                contLoop = False

                            End If

                            If (contLoop = True) Then

                                Set swFeat = swFeat.GetNextFeature

                            End If

                        Wend

                      

                        ReDim fileName(0 To count - 1)

                      

                        For i = 1 To count

                            If i < 10 Then nummer = "0" & i

                          

                            naam = Mid(swModel.GetTitle, 1, Len(swModel.GetTitle) - 2) & nummer

                            fileName(i - 1) = "C:\temp\" & Mid(swModel.GetTitle, 1, Len(swModel.GetTitle) - 2) & nummer & ".SLDPRT"

                        Next i

                      

                        swFeatMgr.CreateSaveBodyFeature v1, fileName, "C:\temp\" & swModel.GetTitle & ".SLDASM", False, False

                    End Sub

                      • Re: Cutlist Save Bodies Macro
                        Simon Turner

                        Add this line to the start of the macro (to make it case insensitive for the Replace command):

                        Option Compare Text

                         

                        Change the declaration of v1 and fileName to this:

                        Dim v1 As Variant, fileName() As String

                         

                        Starting from this line:

                         

                        ReDim fileName(0 To count - 1)

                         

                        Change the entire block of code to this:

                            ReDim fileName(0 To count - 1)

                            Dim vFileName As Variant

                            Dim myTitle As String

                           

                            myTitle = Replace(swModel.GetTitle, ".sldprt", "")

                         

                            For i = 1 To count

                                naam = Left(myTitle, Len(myTitle) - 2) & Format(i, "00")

                                fileName(i - 1) = "C:\temp\" & naam & ".SLDPRT"

                            Next

                         

                            vFileName = fileName

                            swFeatMgr.CreateSaveBodyFeature v1, vFileName, "C:\temp\" & myTitle & ".SLDASM", False, False

                          • Re: Cutlist Save Bodies Macro
                            Willie Roelofs

                            Thank you for your comment Simon. The issue isn't within the filename.

                             

                            I did the following:

                             

                            I made a weldment with 8 structural members and ran the following code:

                             

                                 While Not swFeat Is Nothing And contLoop = True

                                      Dim Name As String

                                      Name = swFeat.GetTypeName2

                                      Debug.Print Name

                                      Set swFeat = swFeat.GetNextFeature

                                      End If

                                 Wend

                             

                             

                            The debug.print list contains the following:

                            CommentsFolder

                            FavoriteFolder

                            HistoryFolder

                            SelectionSetFolder

                            SensorFolder

                            DocsFolder

                            DetailCabinet

                            SurfaceBodyFolder

                            SolidBodyFolder

                            EnvFolder

                            EqnFolder

                            MaterialFolder

                            RefPlane

                            RefPlane

                            RefPlane

                            OriginProfileFeature

                            WeldmentFeature

                            ProfileFeature

                            WeldMemberFeat

                            CutListFolder

                            CutListFolder

                            CutListFolder

                            CutListFolder

                            CutListFolder

                            CutListFolder

                            CutListFolder

                            CutListFolder

                             

                            Each CutListFolder contains 1 or more objects, all objects in one folder are exactly the same.

                             

                            So I need to select each first body in each CutListFolder to get the right list of bodies. However I haven't got a clue how to do that.

                              • Re: Cutlist Save Bodies Macro
                                Willie Roelofs
                                Folder: CommentsFolderBodies: 0
                                Folder: FavoriteFolderBodies: 0
                                Folder: HistoryFolderBodies: 0
                                Folder: SelectionSetFolderBodies: 0
                                Folder: SensorFolderBodies: 0
                                Folder: DocsFolderBodies: 0
                                Folder: DetailCabinetBodies: 0
                                Folder: SurfaceBodyFolderBodies: 0
                                Folder: SolidBodyFolderBodies: 0
                                Folder: EnvFolderBodies: 0
                                Folder: EqnFolderBodies: 0
                                Folder: MaterialFolderBodies: 0
                                Folder: RefPlaneBodies: 0
                                Folder: RefPlaneBodies: 0
                                Folder: RefPlaneBodies: 0
                                Folder: OriginProfileFeatureBodies: 0
                                Folder: WeldmentFeatureBodies: 0
                                Folder: ProfileFeatureBodies: 0
                                Folder: WeldMemberFeatBodies: 0
                                Folder: CutListFolderBodies: 2
                                Folder: CutListFolderBodies: 2
                                Folder: CutListFolderBodies: 1
                                Folder: CutListFolderBodies: 1
                                Folder: CutListFolderBodies: 1
                                Folder: CutListFolderBodies: 1
                                Folder: CutListFolderBodies: 1
                                Folder: CutListFolderBodies: 1
                                • Re: Cutlist Save Bodies Macro
                                  Simon Turner

                                  Your code GetVariantOfBody should do that for you. It will get an array containing all the bodies in each cutlstfolder that you run it on. So just use the first one for each (element 0 in the array).

                                  I have edited your code to do that.