5 Replies Latest reply on Feb 11, 2015 10:16 AM by David Odle

    Need to select the last item in a design tree

    David Odle

      this is what i have so far...

       

      '    Dim swapp As SldWorks.SldWorks

      '    Dim part As IModelDoc2

      '    Dim addedPart As SldWorks.ModelDoc2

      '    Dim assemDoc As SldWorks.AssemblyDoc

      '    Dim lerrors As Long

      '    Dim lwarnings As Long

      '    Dim compcount As Integer

      '    Dim boolstatus As Boolean

      '    Dim i As Integer

      '    Set swapp = Application.SldWorks

      '    i = 0

      '    Set assemDoc = swapp.ActiveDoc

      '    For i = 0 To UBound(assemDoc.GetComponents(False))

      '        i = i + 1

      '    Next i

      '    Set compcount = assemDoc.GetComponents("") 'UBound(assemDoc.GetComponents(False))

      '    Set addedPart = assemDoc.GetComponents(False)(i - 1)

      '    Set part = swapp.OpenDoc6(addedPart.GetPathName, swDocPART, 1, "", lerrors, lwarnings)

      '    boolstatus = part.SaveAs("C:\Users\dodle\Documents\derp\herpenson.sldprt")

      '    swapp.QuitDoc (part)

       

      when i run program it gives me a "compile error: object required"

      what should i do

       

      if there is a better way to select this object feel free to suggest

        • Re: Need to select the last item in a design tree
          Ivana Kolin
          Option Explicit
          Sub main()
              Dim swApp                       As SldWorks.SldWorks
              Dim swModel                     As SldWorks.ModelDoc2
              Dim swFeat                      As SldWorks.Feature
              Dim i                           As Long
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              For i = 0 To swModel.GetFeatureCount - 1
                  Set swFeat = swModel.FeatureByPositionReverse(i)
                  If Not swFeat Is Nothing Then
                      Debug.Print swFeat.Name & " [" & swFeat.GetTypeName & "]"
                      If swFeat.GetTypeName = "Reference" Then
                          swFeat.Select2 False, -1
                          Exit For
                      End If
                  End If
              Next i
          End Sub
          
            • Re: Need to select the last item in a design tree
              David Odle

              that seemed to work thanks!

              • Re: Need to select the last item in a design tree
                David Odle

                I was wondering if there was a way to do this and then based on that selected part the program would proceed to open the part save it and then close it

                  • Re: Need to select the last item in a design tree
                    Ivana Kolin
                    Option Explicit
                    Sub main()
                        Dim swApp                       As SldWorks.SldWorks
                        Dim swModel                     As SldWorks.ModelDoc2
                        Dim swFeat                      As SldWorks.Feature
                        Dim i                           As Long
                        Dim Errors             As Long
                        Dim Warnings           As Long
                        Dim swExportPDFData     As SldWorks.ExportPdfData
                    
                        Set swApp = Application.SldWorks
                        Set swModel = swApp.ActiveDoc
                        Dim swComp As SldWorks.Component2
                        Dim swCompModel As SldWorks.ModelDoc2
                        For i = 0 To swModel.GetFeatureCount - 1
                            Set swFeat = swModel.FeatureByPositionReverse(i)
                            If Not swFeat Is Nothing Then
                                Debug.Print swFeat.Name & " [" & swFeat.GetTypeName & "]"
                                If swFeat.GetTypeName = "Reference" Then
                                   ' swFeat.Select2 False, -1
                                    Set swComp = swFeat.GetSpecificFeature2
                                    Exit For
                                End If
                            End If
                        Next i
                        If Not swComp Is Nothing Then
                            Set swCompModel = swApp.ActivateDoc3(swComp.GetPathName, False, swRebuildOnActivation_e.swUserDecision, Errors)
                            If Not swCompModel Is Nothing Then
                                swCompModel.Extension.SaveAs "d:\test.sldprt", 0, 0, swExportPDFData, Errors, Warnings
                                swApp.CloseDoc swCompModel.GetPathName
                            End If
                        End If
                    End Sub