0 Replies Latest reply on Dec 17, 2013 4:30 AM by Bob Giesberts

    Save drawing as EPS (using SW and Illustrator)

    Bob Giesberts

      Hi all,


      I'm new to this forum and relatively new to scripting in SolidWorks. For making manuals of some products I have to make a lot of vector drawings which have to be delivered in .eps format. Now I'm trying to script some sort of 'Screenshot' button to save the current view of my model first as a .dxf file in SolidWorks (I already got that working), and now open that file again in Illustrator to run an action there (to set line thickness to 0.35, all colors to real black, etc.) and save it as .eps. My script always crashes when trying to access Illustrator although the program does start and sometimes even opens the dxf file. It says "Run-time error '429': ActiveX component can't create object


      I hope someone can help me improve my script! Every help is welcome!


      My script:


      ' ******************************************************************************
      ' Pre-conditions:
      '  + SolidWorks model in correct orientation
      '  + New Drawing opened with the active model ("Make Drawing from Assembly / Part")
      '  + Illustrator is open with an empty document
      ' End-conditions:
      '  + A 'screenshot' is made and saved in /DXF/ as ....dxf
      '  - In Illustrator (CS6) the .dxf file is opened, the action "dxf > eps" is performed and the file is saved as ....eps. Illustrator is closed
      '  + In Solidworks we're back at the active model (drawing is closed)
      ' ******************************************************************************
      Option Explicit
      Dim swApp              As SldWorks.SldWorks
      Dim swModel            As SldWorks.ModelDoc2
      Dim swDrawing          As SldWorks.DrawingDoc
      Dim swSheet            As SldWorks.Sheet
      Dim swView             As SldWorks.View
      Dim iApp               As Illustrator.Application
      Dim iDoc               As Illustrator.Document
      Dim vViewNames         As Variant
      Dim nPaperSize         As swDwgPaperSizes_e
      Dim dWidth             As Double
      Dim dHeight            As Double
      Dim SFileName          As String
      Dim SPathName          As String
      Dim bSave              As Boolean
      Dim SFile              As String
      Sub main()
           Set swApp = Application.SldWorks
           Set swModel = swApp.ActiveDoc
           Set swDrawing = swModel
           If Not swDrawing Is Nothing Then
                Debug.Print "___SolidWorks___"
                Set swSheet = swDrawing.GetCurrentSheet
                nPaperSize = swSheet.GetSize(dWidth, dHeight)
                If Not swSheet Is Nothing Then
                     vViewNames = swDrawing.GetDrawingPaletteViewNames
                     If (Not (IsEmpty(vViewNames))) Then
                          Set swView = swDrawing.DropDrawingViewFromPalette2("*Current", dWidth / 2#, dHeight / 2#, 0#)
                          swView.ShowExploded (True)
                          swView.ScaleDecimal = 1
                          If Not swView Is Nothing Then
                               SFileName = InputBox("How do you want to call this file? (" & swModel.GetPathName & ")", "DXF File Name", swModel.GetTitle)
                               SPathName = "D:\Documents\My Paperwork\WERK\Irene Pieper\DXF\" & SFileName & ".DXF"
                               bSave = swModel.SaveAs3(SPathName, 0, 0)
                               If Not bSave Then
                                    Debug.Print " * File was saved (" & SPathName & ")"
                                    Debug.Print " * File was not saved (" & SPathName & ", " & SFileName & ")"
                               End If
                               Debug.Print " * Closing: " & swModel.GetTitle
                               swApp.QuitDoc swModel.GetTitle
                               ' Continue with Illustrator
                               Call DXFtoEPS(SPathName)
                               Debug.Print " * No view was dropped"
                          End If
                          Debug.Print " * No available views in " & swSheet.GetName & ". So drawing was not linked to model?"
                     End If
                     Debug.Print " * No sheet in the drawing."
                End If
                Debug.Print "SolidWorks: No active model."
           End If
      End Sub
      Sub DXFtoEPS(SFile As String)
           ' Continue in Illustrator
           Debug.Print "___Illustrator___"
           ' Start Illustrator. This is where it crashes:
           ' Set iApp = New Illustrator.Application
           Set iApp = CreateObject("Illustrator.Application")
           Debug.Print " * Illustrator is started!"
           ' Bestand openen in Illustrator
           Set iDoc = iApp.Open(SFile)
           Debug.Print " * File opened: " & SFile
           ' Execute action DXF > EPS 
           iApp.DoScript "DXF > EPS", "Default Actions"
           While (iApp.ActionIsRunning)
                Debug.Print "zzz..."
           Debug.Print "DXF > EPS is executed..."
      End Sub