7 Replies Latest reply on Dec 6, 2018 4:11 AM by Dave Baxter

    Macro help

    Dave Baxter

      Hopefully I can explain this in a way that makes sense.


      I have been using a macro to export Sheet Metal Flat patterns from assemblies for a while now and I am trying to tweak the outputs (if possible).


      Basically I want to export as a PDF also but the view is normally (depending on the part size) larger than the output image. This is obviously the same with the less important but rather handy, 'windows preview' image. I believe what I need is to add a ZoomToFit command to the Macro but programming is not exactly my forte.


      The macro as it is now is below, can anyone even understand what I mean or even better help me add the command?


      I have researched this for quite a while now and just cannot find a simple answer.


      Thanks in advance





          Option Explicit


          Dim MyCount As Integer

          Dim PartName(0 To 200) As String



      Sub main()



         Dim swApp                       As SldWorks.SldWorks

         Dim swModel                     As SldWorks.ModelDoc2

         Dim swAssy                      As SldWorks.AssemblyDoc

         Dim swConf                      As SldWorks.Configuration

         Dim swRootComp                  As SldWorks.Component2

         Dim nStart                      As Single

         Dim bRet                        As Boolean



         Set swApp = Application.SldWorks

         Set swModel = swApp.ActiveDoc

         Set swConf = swModel.GetActiveConfiguration

         Set swRootComp = swConf.GetRootComponent3(True)







         TraverseComponent swRootComp, 1






      End Sub



      Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)


      On Error Resume Next




      Dim vChildComp                  As Variant


         Dim swApp                       As SldWorks.SldWorks

         Dim swpart                      As SldWorks.PartDoc


         Dim swChildComp                 As SldWorks.Component2

         Dim swConfig                    As SldWorks.Configuration

         Dim swConfMgr                   As SldWorks.ConfigurationManager

         Dim swChildModel                As SldWorks.ModelDoc2

         Dim swOpenModel                 As SldWorks.ModelDoc2


         Dim swChildCustPropMngr         As CustomPropertyManager

         Dim swChildModelDocExt          As ModelDocExtension

         Dim swsheetmetal                As SldWorks.SheetMetalFeatureData

         Dim swFeat                      As SldWorks.Feature

         Dim swBody                      As SldWorks.Body2



         Dim Sheet_metal                 As Boolean

         Dim Boolstatus                  As Boolean


         Dim Thickness                   As Double

         Dim conv                        As Double



         Dim i                           As Long

         Dim loptions                    As Long

         Dim lerrors                     As Long



         Dim sPadStr                     As String

         Dim FilePath                    As String

         Dim FileName                    As String

         Dim swThkDir                    As String

         Dim swMatDir                    As String

         Dim swCurrent                   As String

         Dim RefCfg                      As String

         Dim ChildConfigName             As String

         Dim sMatName                    As String

         Dim sMatDB                      As String

         Dim exFileName                  As String


         Dim Bodies                      As Variant



         Dim k As Long





         vChildComp = swComp.GetChildren



         For i = 0 To UBound(vChildComp)

             Set swChildComp = vChildComp(i)


             'Check to see if current component is suppressed

             If swChildComp.IsSuppressed = False Then GoTo Active Else GoTo Skip



      Set swApp = Application.SldWorks



      Set swChildModel = swChildComp.GetModelDoc2

         'Check to see if child component is an Assembly or part

         If (swChildModel.GetType <> swDocPART) Then GoTo Jump 'Skips Subassemby level



      Set swpart = swChildModel 'Applies part commands for current component



      FilePath = Left(swComp.GetPathName, InStrRev(swComp.GetPathName, "\") - 1)


      FileName = Left(swChildModel.GetTitle, InStrRev(swChildModel.GetTitle, ".") - 1) 'Get title of component

      swCurrent = swChildComp.ReferencedConfiguration 'Get current configuration of component






      'exclude same parts



      PartName(MyCount) = swChildModel.GetTitle & swChildComp.ReferencedConfiguration





      For k = 0 To MyCount



      If MyCount <> k Then



      If PartName(MyCount) = PartName(k) Then



      'part already exists



      GoTo Jump



      End If

      End If



      Next k



      If IsEmpty(swpart.GetBodies2(swBodyType_e.swAllBodies, True)) Then



      GoTo Jump



      End If




      Bodies = swpart.GetBodies2(swBodyType_e.swAllBodies, True)







      Set swBody = Bodies(0)



         If swBody.IsSheetMetal = 0 Then 'If Body is not sheet metal





                 GoTo Jump

         End If


         If swBody.IsSheetMetal = 1 Then 'If body is sheet metal





             GoTo Process

         End If






      'Get Part Material



      Set swpart = swChildModel

      sMatName = swpart.GetMaterialPropertyName2(swCurrent, sMatDB)



      If sMatName = "" Then sMatName = "DWG files"





      'Get part Thickness



      Set swFeat = swChildModel.FirstFeature

         While Not swFeat Is Nothing



         If swFeat.GetTypeName = "SheetMetal" Then

             Set swsheetmetal = swFeat.GetDefinition


             Thickness = swsheetmetal.Thickness



      Select Case swChildModel.LengthUnit


        Case "0" 'mm

            conv = 1000

           Thickness = Thickness * conv


          Case "2" 'm

            conv = 1

           Thickness = Thickness * conv


           Case "3" 'inches

            conv = 39.3700787401575

           Thickness = Thickness * conv



         End Select


         End If



      Set swFeat = swFeat.GetNextFeature




      swMatDir = FilePath & "\" & sMatName





      If Dir(swMatDir, vbDirectory) = "" Then MkDir swMatDir



      swThkDir = FilePath & "\" & sMatName & "\" & Thickness





      If Dir(swThkDir, vbDirectory) = "" Then MkDir swThkDir



      exFileName = FilePath & "\" & sMatName & "\" & Thickness & "\" & FileName & "-" & swCurrent







      Set swOpenModel = swApp.ActivateDoc3(swChildModel.GetPathName, True, loptions, lerrors)



      Boolstatus = swChildModel.ShowConfiguration2(swCurrent)



      swChildModel.ExportFlatPatternView exFileName & ".DWG", 1



      swChildModel.ExportFlatPatternView exFileName & ".PDF", 1



      swApp.CloseDoc (swChildModel.GetPathName)


      MyCount = MyCount + 1


      GoTo Jump










             TraverseComponent swChildComp, nLevel + 1

                Next i


      MyCount = 0



      End Sub

        • Re: Macro help
          Deepak Gupta

          Dave, not on the system but add following line



          Boolstatus = swChildModel.ShowConfiguration2(swCurrent)

          and check if that works.


          Also reading your codes I'm wondering if you're actually getting the PDF saved using this line?

          swChildModel.ExportFlatPatternView exFileName & ".PDF", 1
            • Re: Macro help
              Dave Baxter

              Thanks for the reply Deepak,


              I just added the line and it seemed to make no difference at all. Maybe I did something wrong?


              Regarding the PDF line, it seems that the problem I assumed was related might be a failure with an assumption I made.


              I am not sure if it makes a difference but, I basically changed .DXF to .DWG (in that line of code) and it worked. As in, I get a dwg file which seems fine in eviewer, draftsight and also with clients, but it does not have a preview in windows or, as I have found out just now (pointed out by yourself) the PDF does not export correctly either. I get the coordinate arrows in the PDF when there was nothing before though?


              I am double checking what I have said to make sure I am not giving you false info....



              I have tested with the 'test' assembly and for clarity have included the results. (I finally worked out that a zip file is allowed!! I think?)


              I did the simple parts different sizes to check the output size of the exported .DWG/.PDF. The .PDF obviously does not work which I apologise for as the actual job has very large cutouts, thus giving me a false flag.. and a blank preview.


              I am pretty sure it is a failure with me but ?


              I have edited some earlier text in this post to hopefully explain more specifically.

                • Re: Macro help
                  Deepak Gupta

                  There is no thumbnail for DXF file but yes there is for DWG (I've no clue why). So thumbnail can be added to the DXF file via macro which means DWG would be your option.


                  For PDF, the codes are not correct but those can be fixed. But are you looking for a PDF of folded part or flat pattern?

                    • Re: Macro help
                      Dave Baxter

                      That is the main reason for wanting to change the macro to .DWG export in the first place as it makes finding a file easier and quicker. Also I can see obvious problems with the flat pattern without opening anything. The reason for the PDF was mainly because I couldn't get the .DWG preview working. This was going to be my way of getting a preview.


                      I believe that the reason for no preview for a DXF is because they are not created with a thumbnail. This is the reason for DWG export. I have a macro for that but it doesnt work the way the macro I want to edit does and takes longer.

                      I was hoping that I could 'just' export to DWG and everything would be perfect but.......not that simple.

                        • Re: Macro help
                          Deepak Gupta

                          Dave Baxter wrote:


                          I believe that the reason for no preview for a DXF is because they are not created with a thumbnail. This is the reason for DWG export. I have a macro for that but it doesnt work the way the macro I want to edit does and takes longer.

                          I was hoping that I could 'just' export to DWG and everything would be perfect but.......not that simple.

                          For me the exported DWG files (even the ones you've posted above) are showing the thumbnails perfectly. Are you seeing the thumbnail in your PDF files (even if it is blank)? If not then it may be the thumbnail setting under your folder options.


                          Also have you tried using the preview pane to see the preview of selected file?

                            • Re: Macro help
                              Dave Baxter

                              They do show up but they are 4 different sizes and through further testing (I should've explained this better) if you increase the sizes of the part they become larger than the preview. Yes the preview pane works fine on any other DWG export with other macros but I have many parts in assemblies that require DWG files so the assembly export is better and saves time. I have attached a modified set of parts to show the size increase. This is why I want a fit all command.


                              The actual parts I need are frames with 1000 x 2000 cutouts in the middle so the preview just looked blank which is what got me started with this in the first place.

                          • Re: Macro help
                            Fifi Riri

                            Just for info:

                            dxf can't have thumbnail because it's a simple text file (you can open it with notepad)

                            while dwg is a container that can contains data and metadata