AnsweredAssumed Answered

Running a macro on all files in a folder and saving output with respective names?

Question asked by Dheeraj Peddireddy on Jun 26, 2019
Latest reply on Jun 28, 2019 by Fifi Riri

Hello,

 

I've only started working with the macros today. I am trying to create orthographic drawings of .STEP files. I've managed to record a macro to do so but I'm confused as to how to run the macro on all the files and save the outputs with the names of the input files. I was told that #TASK will achieve this but unfortunately I cannot install it. Is there a way to run the macro on the entire batch without TASK ? I am copying the recorded macro below 

 

 

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Dim COSMOSWORKSObj As Object
Dim CWAddinCallBackObj As Object
Set CWAddinCallBackObj = swApp.GetAddInObject("CosmosWorks.CosmosWorks")
Set COSMOSWORKSObj = CWAddinCallBackObj.COSMOSWORKS
boolstatus = swApp.LoadFile2("\Desktop\CAD\STEP\2. Drill Shaft.STEP", "r")
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

' Save As
longstatus = Part.SaveAs3("\Desktop\CAD\SLDPRT\2. Drill Shaft.SLDPRT", 0, 2)

' New Document
Dim swSheetWidth As Double
swSheetWidth = 0.42
Dim swSheetHeight As Double
swSheetHeight = 0.297
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Drawing.drwdot", 12, swSheetWidth, swSheetHeight)
Dim swDrawing As DrawingDoc
Set swDrawing = Part
Set swDrawing = Part
Dim swSheet As sheet
Set swSheet = swDrawing.GetCurrentSheet()
swSheet.SetProperties2 12, 13, 1, 1, False, swSheetWidth, swSheetHeight, True
swSheet.SetTemplateName "C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Drawing.drwdot"
swSheet.ReloadTemplate True
boolstatus = Part.GenerateViewPaletteViews("\Desktop\CAD\SLDPRT\2. Drill Shaft.SLDPRT")
Dim myView As Object
Set myView = Part.DropDrawingViewFromPalette2("Drawing View1", 8.21380016245974E-02, 0.256188346842566, 0)
boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
Set myView = Part.CreateUnfoldedViewAt3(8.21380016245974E-02, 0.101469859624286, 0, False)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.ActivateView("Drawing View1")
Set myView = Part.CreateUnfoldedViewAt3(0.312179962883356, 0.256188346842566, 0, False)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
Set myView = Part.CreateUnfoldedViewAt3(0.272143161366345, 0.066183187100819, 0, False)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Drawing View1", "DRAWINGVIEW", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True

' Zoom To Fit
Part.ViewZoomtofit2

' Save As
longstatus = Part.SaveAs3("\Desktop\CAD\images\2. Drill Shaft.JPG", 0, 0)

' Close Document
Set swDrawing = Nothing
Set Part = Nothing
swApp.CloseDoc "2. Drill Shaft - Sheet1"
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 0
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "2. Drill Shaft.SLDPRT", False, longstatus
Set Part = swApp.ActiveDoc

' Redraw
Part.GraphicsRedraw2

' Close Document
swPart = Nothing
Set Part = Nothing
swApp.CloseDoc "2. Drill Shaft.SLDPRT"
StudyManagerObj = Nothing
ActiveDocObj = Nothing
Set CWAddinCallBackObj = Nothing
Set COSMOSWORKSObj = Nothing
End Sub

Outcomes