11 Replies Latest reply on Oct 15, 2014 12:38 PM by Newell Voss

    referencing active document when saving out configurations as IGES

    Newell Voss

      I used the SW macro recorder to create a program that will save out 3 different configurations in an assembly to IGES format for programming. From what I can tell, the macro does what it should, except for referencing the open or current document. I tried to run the macro on another assembly with the same configurations and it worked, but it saved to the original file location that I used to record the macro. I assume this is a relatively easy fix, I just don't know the call out for the macro to reference the active or open file location. any help is greatly appreciated. Thanks!

        • Re: referencing active document when saving out configurations as IGES
          Deepak Gupta

          Post your macro here and someone will check it out. Use advanced editor to upload the file.

          • Re: referencing active document when saving out configurations as IGES
            Jesse Seger

            Try this

             

            Dim swApp As SldWorks.SldWorks

             

             

            Dim Part As ModelDoc2

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

            Dim folder As String

            Dim path As String

            Dim name As String

            Dim fileName As String

            Dim configs As Variant

             

             

            Sub main()

             

             

            Set swApp = Application.SldWorks

            Set Part = swApp.ActiveDoc

             

             

            Dim n1, n2 As Integer

            n1 = InStrRev(Part.GetPathName(), "\", -1, vbTextCompare)

            n2 = InStrRev(Part.GetPathName(), ".", -1, vbTextCompare)

            folder = Left(Part.GetPathName(), n1)

            name = Mid(Part.GetPathName(), n1 + 1, n2 - n1 - 1)

            Debug.Print folder

            Debug.Print name

             

             

            configs = Part.GetConfigurationNames()

             

             

            For Each config In configs

             

             

                boolstatus = Part.ShowConfiguration2(config)

               

                fileName = folder + name + config + ".IGS"

                Debug.Print fileName

                longstatus = Part.SaveAs3(fileName, 0, 0)

            Next

             

             

            End Sub

              • Re: referencing active document when saving out configurations as IGES
                Newell Voss

                Thanks Jesse!

                The code seems to work as intended. There is only one other question I have. Is there any way to reference a folder titled "OUT" for the save location? This is a sub-folder we create within the original job folder which is always labeled exactly the same as: (OUT). This separates our design files from programmer files. I'm sure I could just create a copy and open up the assembly from within the out folder before executing the macro, however it seems like a relatively easy step to add in. Let me know when you get a chance.

                 

                 

                Best regards,

                 

                 

                Newell

                  • Re: referencing active document when saving out configurations as IGES
                    Jesse Seger

                    Updated code.

                     

                    Dim swApp As SldWorks.SldWorks

                     

                     

                    Dim Part As ModelDoc2

                    Dim boolstatus As Boolean

                    Dim longstatus As Long, longwarnings As Long

                    Dim folder As String

                    Dim path As String

                    Dim name As String

                    Dim fileName As String

                    Dim configs As Variant

                     

                     

                    Sub main()

                     

                     

                    Set swApp = Application.SldWorks

                    Set Part = swApp.ActiveDoc

                     

                     

                    Dim n1, n2 As Integer

                    n1 = InStrRev(Part.GetPathName(), "\", -1, vbTextCompare)

                    n2 = InStrRev(Part.GetPathName(), ".", -1, vbTextCompare)

                    folder = Left(Part.GetPathName(), n1)

                    name = Mid(Part.GetPathName(), n1 + 1, n2 - n1 - 1)

                    Debug.Print folder

                    Debug.Print name

                     

                     

                    configs = Part.GetConfigurationNames()

                     

                     

                    On Error Resume Next

                    MkDir folder + "OUT\"

                    On Error GoTo 0

                     

                     

                    For Each config In configs

                     

                     

                        boolstatus = Part.ShowConfiguration2(config)

                       

                        fileName = folder + "OUT\" + name + config + ".IGS"

                        Debug.Print fileName

                        longstatus = Part.SaveAs3(fileName, 0, 0)

                    Next

                     

                     

                    End Sub

                      • Re: referencing active document when saving out configurations as IGES
                        Newell Voss

                        Awesome. Everything works perfectly. I know I said that was my last question but I noticed an issue that I hadn't thought of previously. In order to properly import these files from native SW to IGES we have to tag them with a coordinate system. Since we re-use the same sub-assembly by performing a "pack&go" the coordinate system remains in the same place with the same display name each time. So my question is: is there a way to include the coordinate system in this code? Typically I have to go file>save as>(select IGES from format type)>then there is an "options" tab that appears. In the options tab at the bottom next to "output coordinate system:" there is a drop-down menu that is set automatically to "default" or none. The coordinate system I create appears in the drop-down as " Coordinate System1". Is this a possibility?

                         

                        Thank you again for your patience and cooperation. It is very much appreciated!

                          • Re: referencing active document when saving out configurations as IGES
                            Jesse Seger

                            Try this code.

                             

                            Dim swApp As SldWorks.SldWorks

                             

                             

                            Dim Part As ModelDoc2

                            Dim boolstatus As Boolean

                            Dim longstatus As Long, longwarnings As Long

                            Dim folder As String

                            Dim path As String

                            Dim name As String

                            Dim fileName As String

                            Dim configs As Variant

                            Dim modExt As ModelDocExtension

                             

                             

                            Sub main()

                             

                             

                            Set swApp = Application.SldWorks

                            Set Part = swApp.ActiveDoc

                            Set modExt = Part.Extension

                            modExt.SetUserPreferenceString swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, "Coordinate System1"

                            Dim n1, n2 As Integer

                            n1 = InStrRev(Part.GetPathName(), "\", -1, vbTextCompare)

                            n2 = InStrRev(Part.GetPathName(), ".", -1, vbTextCompare)

                            folder = Left(Part.GetPathName(), n1)

                            name = Mid(Part.GetPathName(), n1 + 1, n2 - n1 - 1)

                            Debug.Print folder

                            Debug.Print name

                             

                             

                            configs = Part.GetConfigurationNames()

                             

                             

                            On Error Resume Next

                            MkDir folder + "OUT\"

                            On Error GoTo 0

                             

                             

                            For Each config In configs

                             

                             

                                boolstatus = Part.ShowConfiguration2(config)

                              

                                fileName = folder + "OUT\" + name + config + ".IGS"

                              

                                Debug.Print fileName

                                longstatus = Part.SaveAs3(fileName, 0, 0)

                            Next

                             

                             

                            End Sub

                              • Re: referencing active document when saving out configurations as IGES
                                Newell Voss

                                The files convert and save to the proper location. However, the orientation upon import is incorrect. I checked the part assembly and on some of the configurations the coordinate system was suppressed. I unsuppressed it on all the configs, saved the updated assembly, and then ran the macro again but still had the same issue. Not sure if its a code error or the coordinate system isn't being included for other reasons. Any thoughts?

                                  • Re: referencing active document when saving out configurations as IGES
                                    Jesse Seger

                                    Check the SaveAs options after you run this macro to see if the correct coordinate system is selected.

                                      • Re: referencing active document when saving out configurations as IGES
                                        Newell Voss

                                        The first configuration I checked did actually display "coordinate system1" in the drop down window. Unfortunately, all the other configurations and an entire second assembly I ran it on all displayed --default-- in the drop down area.

                                          • Re: referencing active document when saving out configurations as IGES
                                            Jesse Seger

                                            Ahhhhh... This should fix it.  And make sure your coordinate system is name "Coordinate System1".  Otherwise it will not select it.

                                             

                                            Dim swApp As SldWorks.SldWorks

                                             

                                             

                                            Dim Part As ModelDoc2

                                            Dim boolstatus As Boolean

                                            Dim longstatus As Long, longwarnings As Long

                                            Dim folder As String

                                            Dim path As String

                                            Dim name As String

                                            Dim fileName As String

                                            Dim configs As Variant

                                            Dim modExt As ModelDocExtension

                                             

                                             

                                            Sub main()

                                             

                                             

                                            Set swApp = Application.SldWorks

                                            Set Part = swApp.ActiveDoc

                                            Set modExt = Part.Extension

                                             

                                            Dim n1, n2 As Integer

                                            n1 = InStrRev(Part.GetPathName(), "\", -1, vbTextCompare)

                                            n2 = InStrRev(Part.GetPathName(), ".", -1, vbTextCompare)

                                            folder = Left(Part.GetPathName(), n1)

                                            name = Mid(Part.GetPathName(), n1 + 1, n2 - n1 - 1)

                                            Debug.Print folder

                                            Debug.Print name

                                             

                                             

                                            configs = Part.GetConfigurationNames()

                                             

                                             

                                            On Error Resume Next

                                            MkDir folder + "OUT\"

                                            On Error GoTo 0

                                             

                                             

                                            For Each config In configs

                                             

                                            modExt.SetUserPreferenceString swUserPreferenceStringValue_e.swFileSaveAsCoordinateSystem, swUserPreferenceOption_e.swDetailingNoOptionSpecified, "Coordinate System1"

                                                boolstatus = Part.ShowConfiguration2(config)

                                             

                                                fileName = folder + "OUT\" + name + config + ".IGS"

                                             

                                                Debug.Print fileName

                                                longstatus = Part.SaveAs3(fileName, 0, 0)

                                            Next

                                             

                                             

                                            End Sub

                                              • Re: referencing active document when saving out configurations as IGES
                                                Newell Voss

                                                It still seems to be having the same issue upon import. this isn't entirely a major issue as I could have our programmer tweak his code to flip the view to the proper orientation if it imports the same every time. Although, after going through each configuration and checking the save settings for each I found that of the 4 configs, the first 3 all show coordinate system1 in the drop down menu as they should. However, the last one still displays --default--. Any ideas on why that is?