5 Replies Latest reply on Nov 12, 2015 7:10 AM by Jean Stassen

    How to save with overwrite within a macro. Must overwrite the model in result that the thumbnail is correct

    Jean Stassen

      Please comment on macro. Ive used a macro from internet and I am a BEGINNER in API !

      I've headlighted the part of the macro where the question refers to.

      I want to have SW overwrite the savename in result that the thumbnail in explorer is right.

      I can't find the right API to do so.

       

      Thank yout in advance for your help

       

       

       

      Option Explicit

      Dim swApp        As SldWorks.SldWorks 
      Dim swModel      As SldWorks.ModelDoc2
      Dim swDrawing    As SldWorks.DrawingDoc

      Dim strFileName  As String
      Dim saveFilename As String
      Dim strPath      As String
      Dim boolStatus   As Boolean
      Dim longStatus   As Long
      Dim longErrors   As Long
      Dim longWarnings As Long
       
      Sub main()
         
          Set swApp = Application.SldWorks
             
          strPath = "F:\Mijn documenten\40 Try-outs SW\2015\Macro's\Concept\Test Directory voor macro\2011_206_06 capafgifte"
          strPath = strPath + "\"

      Debug.Print strPath
         
          strFileName = Dir(strPath & "*.sldprt") ' Change .sldprt to any other extension you need. Think about OpneDoc or LoadFile
         
          Do Until strFileName = ""

      Debug.Print strFileName

          'deze regel is voor NON Solidworks files als model in te lezen in solidworks, voor SW files opendoc6 gebruiken ' --> boolstatus = swApp.LoadFile2(strPath + strFileName, "")
        
          Set swModel = swApp.OpenDoc6(strPath + strFileName, swDocPART, swOpenDocOptions_Silent, "", longErrors, longWarnings)
                  
              swModel.ShowNamedView2 "*Isometric", 7 'isometrisch plaatsen in file
              swModel.ViewZoomtofit2
                  
          'longStatus = swModel.SaveAs3(strPath + swModel.GetTitle, 0, 2)
          saveFilename = Left(swApp.ActiveDoc.GetPathName, Len(swApp.ActiveDoc.GetPathName) - 6)    'De bewaarnaam verkrijgen om te kunnen overwriten
          'swModel.SaveAs2 saveFilename, 0, True, False
         
         
         swApp.CloseDoc swModel.GetTitle
        
        
      Set swModel = Nothing

       

      strFileName = Dir
        
      Debug.Print strFileName
        
      Loop

      End Sub

        • Re: How to save with overwrite within a macro. Must overwrite the model in result that the thumbnail is correct
          Deepak Gupta

          Try these codes:

           

          Option Explicit

          Dim swApp               As SldWorks.SldWorks

          Dim swModel             As SldWorks.ModelDoc2

          Dim Path                As String

          Dim swFilename          As String

          Dim nErrors             As Long

          Dim nWarnings           As Long

          Dim Response            As String

           

          Sub main()   

           

          Set swApp = Application.SldWorks 

             Path = "C:\Users\Desktop\Test Folder\" ' Change path here   

              Response = Dir(Path)

             

          Do Until Response = ""

                  swFilename = Path & Response

                     Debug.Print swFilename

                      Set swModel = swApp.OpenDoc6(swFilename, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                      swModel.ShowNamedView2 "*Isometric", -1

                      swModel.ViewZoomtofit2

                      swModel.Extension.SaveAs swFilename, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings

                      swApp.CloseDoc swModel.GetTitle

                  Response = Dir

              Loop

             

              MsgBox "Files Updated"   

          End Sub

            • Re: How to save with overwrite within a macro. Must overwrite the model in result that the thumbnail is correct
              Jean Stassen

              Dear Deepak,

              I've tried your lines but it still does not update the model. The thing I want to establish is that the model Is isomteric placed, zoomed and saved.

              The reason for this is, when opening with the Windows explorer in the example window wil be a nice zoom preview of the model. In my case where I've imported SAT files with seperate parts the parts are not nicely Iso Zoomed.

               

              I've cleande my macro and try to attached it, but I don't know how to do so.

              Therefore underneath the lines.

              The macro works but the model seems not to bee Isom,teric and zoomed.

               

              I hope you (or anybody) can help me with this.

              Thank you in advance

               

               

              '
              ' Prerequisits: Solidworks UNDER CONSTRUCTION
              '
              ' Goal !!:       Macro must find all *.SLDPRT files in the specified Path/location,
              '                placing the model in Isometric view, zoom to fit, rebuild and save
              '                them as SolidWorks part files. The result should be a correct thumbnail,
              '                and when scrolling in Windows Explorer to the file you should see a
              '                clear zoomed Iso model in the examplewindow.

               

               

               

              Option Explicit

              Dim swApp        As SldWorks.SldWorks   'calling Interface Soliworks
              Dim swModel      As SldWorks.ModelDoc2  'calling Interface Soliworks
              Dim swDrawing    As SldWorks.DrawingDoc 'calling Interface Soliworks

              Dim strFileName  As String
              Dim saveFilename As String
              Dim strPath      As String
              Dim boolStatus   As Boolean
              Dim longStatus   As Long
              Dim longErrors   As Long
              Dim longWarnings As Long
               
              Sub main()
                 
                  Set swApp = Application.SldWorks
                 
              ' ------------------------------------------------------------------------------
              ' Put in the pathname here
              ' ------------------------------------------------------------------------------
                      strPath = "F:\Mijn documenten\40 Try-outs SW\2015\Macro's\Concept\Test Directory voor macro\2011_206_06 capafgifte\"

                      Debug.Print strPath
                     
              ' ------------------------------------------------------------------------------
              ' Now only the *.SLDPRT files i want to open in SW
              ' ------------------------------------------------------------------------------
                    
                      strFileName = Dir(strPath & "*.sldprt")
                 
                  Do Until strFileName = ""

                  Debug.Print strFileName

              ' ------------------------------------------------------------------------------
              ' Now opening the file in SW
              ' ------------------------------------------------------------------------------
                
                  Set swModel = swApp.OpenDoc6(strPath + strFileName, swDocPART, swOpenDocOptions_Silent, "", longErrors, longWarnings)
                          
              ' ------------------------------------------------------------------------------
              ' Now placing the model in ISOmetric view and zoom to fit
              ' ------------------------------------------------------------------------------
                          
                      swModel.ShowNamedView2 "*Isometric", 7
                      swModel.ViewZoomtofit2
                          
              ' ------------------------------------------------------------------------------
              ' Now saving the model,
              ' ------------------------------------------------------------------------------
                 
                      swModel.Extension.SaveAs strFileName, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, longErrors, longWarnings
                 
                      swApp.CloseDoc swModel.GetTitle
                
                  Set swModel = Nothing

                  strFileName = Dir
                
              Debug.Print strFileName
                
              Loop

              End Sub