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