42 Replies Latest reply on Jan 10, 2018 10:45 AM by Jim Steinmeyer

    Macro preforms different on different machines

    Jim Steinmeyer

      We have a macro that was set up to save the flat pattern sheet on our part drawings as a dxf file and one user is having problems. Our part drawing template  creates a second sheet that is blank and set to 1:1 scale. When making drawings we place the part flat pattern on this sheet and hide any bend lines or annotations. We then save that sheet as a dxf to a plasma file on the server. One of the former designers here created a macro that will save the dxf for us and place it in the correct folder. Several of us have used this macro for years with no problems. My supervisor had been having some of his dxfs not being correct so about a month ago I set the macro up on his machine and things were working fine. As a couple of days ago things went south. The macro now saves the first sheet with the format and dimensions on it as the dxf.

           We tested this and with the same drawing he would open it from the server and run the macro getting the first, fully annotated sheet saved every time. Then I would open the same drawing, run the same macro and get the correct flat pattern sheet saved, every time. I then went and removed the macro from his tool bar and reinstalled it to make sure the correct macro was being run, only to have the same problem. And yet it works correctly on my machine, every time. I have tried with different drawings too make sure it wasn't related to a certain drawing. Same results. The macro, the drawings and the dxf folder are all stored on the same server so I don't see that he would be having issues with different files or paths. In typing this I just thought, maybe I should have shut SW down after removing the macro or maybe shut the whole machine down to see if clearing memory would help. Does anyone have any other suggestions where the problem might be?

       

      The macro is attached.

        • Re: Macro preforms different on different machines
          Elmar Klammer

          Hi Jim,

           

          If you mean by format & dimension that it shows your sheetformat and dimension on the dxf then you might have the first sheet in edit sheet mode saved.

           

          Elmar

            • Re: Macro preforms different on different machines
              Jim Steinmeyer

              Elmar,

                   Yes, it is showing the sheet format, what it is doing is saving the first sheet on my supervisor's machine and saving the second sheet on my machine. Not just on one drawing but on every drawing he opens. And he can open a drawing run the macro and get the first sheet saved, then I will open the SAME drawing, run the SAME macro and get the second sheet saved. That is what is really confusing me, same drawing same macro getting different results on two machines.

                • Re: Macro preforms different on different machines
                  Elmar Klammer

                  Hi Jim,

                   

                  Make a snap shot of the dxf for every one to see.

                   

                  Below is your code

                   

                  Attribute VB_Name = "dxf_macro1"
                  '----------------------------------------------
                  '
                  ' Preconditions:
                  '       (1) Drawing doument is open.
                  '       (2) Drawing contains at least one sheet.
                  '
                  ' Postconditions:
                  '       (1) DXF file is generated for each sheet,
                  '           overwriting any existing file
                  '       (2) DXF file names are based on the file name.
                  '
                  '----------------------------------------------

                  Option Explicit
                  Public Enum swSaveAsVersion_e
                      swSaveAsCurrentVersion = 0  '  default
                      swSaveAsFormatProE = 2      '  save SolidWorks part as Pro/E format
                                                  '  .prt/.asm extension (not as SolidWorks.prt/.asm)
                  End Enum

                  Public Enum swSaveAsOptions_e
                      swSaveAsOptions_Silent = &H1            '  Save document silently or not
                      swSaveAsOptions_Copy = &H2              '  Save document as a copy or not
                      swSaveAsOptions_SaveReferenced = &H4    '  Save referenced documents or not (drawings and parts only)
                  End Enum

                  Public Enum swFileSaveError_e
                      swGenericSaveError = &H1
                      swReadOnlySaveError = &H2
                      swFileNameEmpty = &H4                       '  The filename cannot be empty
                      swFileNameContainsAtSign = &H8              '  The filename cannot contain an the at-sign character (@)
                      swFileLockError = &H10
                      swFileSaveFormatNotAvailable = &H20         '  The Save As file type is not valid
                      swFileSaveAsDoNotOverwrite = &H80           '  The user chose not to overwrite an existing file
                      swFileSaveAsInvalidFileExtension = &H100    '  The file extension differs from the SolidWorks document type
                  End Enum

                  Public Enum swFileSaveWarning_e
                      swFileSaveWarning_RebuildError = &H1    '  The file was saved with a rebuild error
                  End Enum

                  Public Enum swDxfFormat_e
                      swDxfFormat_R12 = 0
                      swDxfFormat_R13 = 1
                      swDxfFormat_R14 = 2
                      swDxfFormat_R2000 = 3
                  End Enum

                  Public Enum swArrowDirection_e
                      swINSIDE = 0
                      swOUTSIDE = 1
                      swSMART = 2
                  End Enum

                  Public Enum swUserPreferenceToggle_e
                      swDxfMapping = 8
                      swDXFDontShowMap = 21
                  End Enum

                     
                  Public Enum swUserPreferenceIntegerValue_e
                      swDxfVersion = 0
                      swDxfOutputFonts = 1
                      swDxfMappingFileIndex = 2
                      swDxfOutputLineStyles = 135
                      swDxfOutputNoScale = 136
                  End Enum

                  Public Enum swUserPreferenceDoubleValue_e
                      swDxfOutputScaleFactor = 79
                  End Enum

                  Public Enum swUserPreferenceStringListValue_e
                      swDxfMappingFiles = 0
                  End Enum

                   

                  Sub main()

                      Dim swApp                       As SldWorks.SldWorks
                      Dim swModel                     As SldWorks.ModelDoc2
                      Dim swDraw                      As SldWorks.DrawingDoc
                      Dim vSheetName                  As Variant
                      Dim nErrors                     As Long
                      Dim nWarnings                   As Long
                      Dim nRetval                     As Long
                      Dim bShowMap                    As Boolean
                      Dim nNumSheet                   As Long
                      Dim i                           As Long
                      Dim bRet                        As Boolean
                      Dim dxf_filepath                As String
                      Dim obs_filepath                As String
                      Dim currentdircount             As Integer
                      Dim strpath                     As String
                      Dim pathnamecount               As Integer
                      Dim dotpos                      As Integer
                      Dim slashpos                    As Integer
                      Dim dashpos                     As Integer
                      Dim strSheetName(4)             As String
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Dim Part                        As Object
                      Dim CurrentRevision             As String
                      Dim RevTable                    As Object
                      Dim Pronest                     As Variant
                      Dim Obsolete                    As Variant
                                        

                  Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      Set swDraw = swModel
                     
                     
                      ' Path to which to save DXF file of drawing
                      dxf_filepath = "W:\Pronest\DXF\"
                      obs_filepath = "W:\Obsolete Drawings\Obsolete DXFs\"
                         

                      ' Current settings
                      Debug.Print "DxfMapping             = " & swApp.GetUserPreferenceToggle(swDxfMapping)
                      Debug.Print "DXFDontShowMap         = " & swApp.GetUserPreferenceToggle(swDXFDontShowMap)
                      Debug.Print "DxfVersion             = " & swApp.GetUserPreferenceIntegerValue(swDxfVersion)
                      Debug.Print "DxfOutputFonts         = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputFonts)
                      Debug.Print "DxfMappingFileIndex    = " & swApp.GetUserPreferenceIntegerValue(swDxfMappingFileIndex)
                      Debug.Print "DxfOutputLineStyles    = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputLineStyles)
                      Debug.Print "DxfOutputNoScale       = " & swApp.GetUserPreferenceIntegerValue(swDxfOutputNoScale)
                      Debug.Print "DxfOutputScaleFactor   = " & swApp.GetUserPreferenceDoubleValue(swDxfOutputScaleFactor)
                      Debug.Print "DxfMappingFiles        = " & swApp.GetUserPreferenceStringListValue(swDxfMappingFiles)
                      Debug.Print ""
                    
                      ' Turn off showing of map
                      swApp.SetUserPreferenceToggle swDXFDontShowMap, True
                      vSheetName = swDraw.GetSheetNames
                     
                    ' Switch to first sheet
                      bRet = swDraw.ActivateSheet(vSheetName(0))
                     
                             
                      'Get Revision Table'
                    Set RevTable = swModel.GetCurrentSheet.RevisionTable
                      If RevTable Is Nothing Then
                      CurrentRevision = "ORIGINAL"
                      Else
                      ' Get Current Revision
                     
                      CurrentRevision = swModel.CustomInfo("Revision")
                     
                      End If
                     
                      ' Switch to second sheet
                      bRet = swDraw.ActivateSheet(vSheetName(1))
                     
                     
                      ' Strip off SolidWorks drawing file extension (.sldxxx)
                      vSheetName(1) = swModel.GetTitle()
                      ' vSheetName(1) = Left(vSheetName(1), Len(vSheetName(1)) - 9)
                          
                       dashpos = InStrRev(vSheetName(1), "-")  'find dash to remove *** name from title i.e. " - sheet1"
                       vSheetName(1) = Mid(vSheetName(1), 1, dashpos - 2)

                       dotpos = InStrRev(vSheetName(1), ".")
                       slashpos = InStrRev(vSheetName(1), "\")

                       If dotpos <> 0 Then ' if contains a dot (extension exists) then chop off extension
                       vSheetName(1) = Mid(vSheetName(1), slashpos + 1, dotpos - slashpos - 1)
                       Else 'does not contain a dot
                       vSheetName(1) = Right(vSheetName(1), Len(vSheetName(1)) - slashpos)
                    End If
                              
                      ' Strip off directory prefix of filename
                      ' strpath = swApp.GetCurrentWorkingDirectory
                      ' pathnamecount = Len(strpath)
                      ' vSheetName(1) = Right(vSheetName(1), Len(vSheetName(1)) - pathnamecount)

                          
                      ' adds file path to file name
                      Pronest = dxf_filepath + vSheetName(1)
                             
                      bRet = swDraw.ActivateSheet(vSheetName(1))
                      bRet = swModel.SaveAs4( _
                              Pronest & ".dxf", _
                              swSaveAsCurrentVersion, _
                              swSaveAsOptions_Silent, _
                              nErrors, _
                              nWarnings)
                      If bRet = False Then
                      MsgBox "Problem creating DXF!"
                      Else
                      End If
                   
                      Obsolete = obs_filepath + vSheetName(1) + "-" + CurrentRevision
                             
                      bRet = swDraw.ActivateSheet(vSheetName(1))
                      bRet = swModel.SaveAs4( _
                              Obsolete & ".dxf", _
                              swSaveAsCurrentVersion, _
                              swSaveAsOptions_Silent, _
                              nErrors, _
                              nWarnings)
                      If bRet = False Then
                      MsgBox "Problem creating DXF!"
                      Else
                      MsgBox "DXF Created Successfully!"
                     
                     
                      ' Switch back to first sheet
                      bRet = swDraw.ActivateSheet(vSheetName(0))

                      ' Restore old setting
                      swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap

                  End If
                  End Sub

                  • Re: Macro preforms different on different machines
                    Rob Edwards

                     

                    You are saving without checking the sheet was actually activated

                    add a msgbox or debug.print to check these return values and run on both machines

                    I would be looking at the value of vSheetName(1) as well.

                • Re: Macro preforms different on different machines
                  Peter Kennedy

                  I didn't look at the macro but did you try to debug the macro and run line by line, and see which line is causing the issue? If its a VBA macro should be F8 that will allow you to go line by line. 

                  • Re: Macro preforms different on different machines
                    Scott Stuart

                    I don't know if it's part of the problem, but the lines highlighted in red below do not seem like they belong inside the if statement. I think they should be after the end if.
                               
                        bRet = swDraw.ActivateSheet(vSheetName(1))
                        bRet = swModel.SaveAs4( _
                                Obsolete & ".dxf", _
                                swSaveAsCurrentVersion, _
                                swSaveAsOptions_Silent, _
                                nErrors, _
                                nWarnings)
                        If bRet = False Then
                        MsgBox "Problem creating DXF!"
                        Else
                        MsgBox "DXF Created Successfully!"
                       
                       
                        ' Switch back to first sheet
                        bRet = swDraw.ActivateSheet(vSheetName(0))

                        ' Restore old setting
                        swApp.SetUserPreferenceToggle swDXFDontShowMap, bShowMap

                        End If
                    End Sub

                    • Re: Macro preforms different on different machines
                      Jim Steinmeyer

                      Thank you everyone who assisted me with this. My supervisor left so I was able to open a drawing and check his DXF settings and Josh was correct. It was set to export all sheets as one. When I changed that setting to export active sheet the macro worked as expected.

                       

                      Again, thank you all.