6 Replies Latest reply on Jun 4, 2014 4:03 AM by Ar K

    Problem with "CreateSaveBodyFeature"

    Ar K

      hi :-)

      I created a vb.net version of macro as in

      http://help.solidworks.com/2014/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IFeatureManager~CreateSaveBodyFeature.html

      My vb.net macro is not working but vba macro is working.

       

      '-------------------------------------------------------------------------------------------

      ' VB.NET

      ' Preconditions: Part document is open and contains a solid body that has been split.                 

      ' Postconditions: C:\temp\p1.sldprt, C:\temp\p2.sldprt,C:\temp\p3.sldprt,

      ' and C:\temp\asm.sldasm are created.                     

      '-------------------------------------------------------------------------------------------

       

      Imports SolidWorks.Interop.sldworks

       

      Partial Class SolidWorksMacro

       

          Public swModel As ModelDoc2

          Public swFeat As Feature

          Public swFeatMgr As FeatureManager

          Public swBodyFolder As BodyFolder

          Public bodyListVar As Object

          Public fileNames(2) As String

          Public fileNameVar As Object

       

          Public Sub main()

       

              swModel = swApp.ActiveDoc

              swFeat = swModel.FirstFeature

              swFeatMgr = swModel.FeatureManager

       

              Dim contLoop As Boolean = True

              Dim Name As String

       

              While Not swFeat Is Nothing And contLoop = True

       

                  Name = swFeat.GetTypeName2

       

                  If (Name = "SolidBodyFolder") Then

                      swBodyFolder = swFeat.GetSpecificFeature2

                      bodyListVar = swBodyFolder.GetBodies

                      contLoop = False

                  ElseIf (contLoop = True) Then

                      swFeat = swFeat.GetNextFeature

                  End If

       

              End While

       

              fileNames(0) = "C:\temp\p1.sldprt"

              fileNames(1) = "C:\temp\p2.sldprt"

              fileNames(2) = "C:\temp\p3.sldprt"

              fileNameVar = fileNames

       

              swFeatMgr.CreateSaveBodyFeature(bodyListVar, fileNameVar, "C:\temp\ass.sldasm", True, True)

       

          End Sub

       

          Public swApp As SldWorks

       

      End Class

       

      Message was edited by: Ar K

        • Re: Problem with "CreateSaveBodyFeature"
          Artem Taturevych

          Try to use early binding, i.e. declare

          Dim bodyListVar(2) As Body2

          You may need to reassign the elements of the array because swBodyFolder.GetBodies may not cast directly to array of Body2(). Also make sure that you have 3 bodies in the folder, i.e. the size of the array is 3.

          ______________________________________________

          Regards, Artem Taturevych | Snr. Developer | IC3D ANZ

           

          IC3DSteel – New Steel Solution for SolidWorks

          translationXpert – SolidWorks files language translator

          LinkedIn - SolidWorks API Group

            • Re: Problem with "CreateSaveBodyFeature"
              Ar K

              Thank You Artem Taturevych :-)

               

              but i am sure that i have 3 bodies in the folder , and i should not cast object to body2 because:

               

              1-Function GetBodies() As System.Object

               

              2-Function CreateSaveBodyFeature( _

                 ByVal Bodies As System.Object, _

                 ByVal FilePaths As System.Object, _

                 ByVal AssemName As System.String, _

                 ByVal ConsumeBody As System.Boolean, _

                 ByVal CopyCustomProperty As System.Boolean _

              ) As System.Object

              • Re: Problem with "CreateSaveBodyFeature"
                Ar K

                I have correct vba code , and this code working excellent !!

                Please see this :


                Option Explicit

                 

                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swFeat As SldWorks.Feature

                Dim swFeatMgr As SldWorks.FeatureManager

                Dim swBodyFolder As SldWorks.BodyFolder

                Dim bodyListVar As Variant

                Dim fileNames(2) As String

                Dim fileNameVar As Variant

                 

                Sub main()

                 

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                Set swFeat = swModel.FirstFeature

                Set swFeatMgr = swModel.FeatureManager

                 

                Dim contLoop As Boolean= True

                Dim Name As String

                 

                While Not swFeat Is Nothing And contLoop = True

                 

                 

                    Name = swFeat.GetTypeName2

                 

                    If (Name = "SolidBodyFolder") Then

                        Set swBodyFolder = swFeat.GetSpecificFeature2

                        bodyListVar = swBodyFolder.GetBodies

                        contLoop = False

                    ElseIf (contLoop = True) Then

                        Set swFeat = swFeat.GetNextFeature

                    End If

                 

                Wend

                 

                fileNames(0) = "C:\temp\p1.sldprt"

                fileNames(1) = "C:\temp\p2.sldprt"

                fileNames(2) = "C:\temp\p3.sldprt"

                fileNameVar = fileNames

                 

                swFeatMgr.CreateSaveBodyFeature bodyListVar, fileNameVar, "C:\temp\asm.sldasm", True, True

                 

                End Sub

                 

                 

                 

                But i have Problem with My VB.NET Code , There is no Error but don't work True !

                Please see this :

                 

                Imports SolidWorks.Interop.sldworks

                 

                Partial Class SolidWorksMacro

                 

                    Public swModel As ModelDoc2

                    Public swFeat As Feature

                    Public swFeatMgr As FeatureManager

                    Public swBodyFolder As BodyFolder

                    Public bodyListVar As Object

                    Public fileNames(2) As String

                    Public fileNameVar As Object

                 

                    Public Sub main()

                 

                        swModel = swApp.ActiveDoc

                        swFeat = swModel.FirstFeature

                        swFeatMgr = swModel.FeatureManager

                 

                        Dim contLoop As Boolean = True

                        Dim Name As String

                 

                        While Not swFeat Is Nothing And contLoop = True

                 

                            Name = swFeat.GetTypeName2

                 

                            If (Name = "SolidBodyFolder") Then

                                swBodyFolder = swFeat.GetSpecificFeature2

                                bodyListVar = swBodyFolder.GetBodies

                                contLoop = False

                            ElseIf (contLoop = True) Then

                                swFeat = swFeat.GetNextFeature

                            End If

                 

                        End While

                 

                        fileNames(0) = "C:\temp\p1.sldprt"

                        fileNames(1) = "C:\temp\p2.sldprt"

                        fileNames(2) = "C:\temp\p3.sldprt"

                        fileNameVar = fileNames

                 

                        swFeatMgr.CreateSaveBodyFeature(bodyListVar, fileNameVar, "C:\temp\ass.sldasm", True, True)

                 

                    End Sub

                 

                    Public swApp As SldWorks

                 

                End Class

                 


              • Re: Problem with "CreateSaveBodyFeature"
                Ar K

                Please help me :-)