Dheeraj Peddireddy

Rotating component in iterations about center using macro

Discussion created by Dheeraj Peddireddy on Jul 18, 2019
Latest reply on Jul 19, 2019 by Artem Taturevych

I am trying to write a for loop that rotates a component in steps of 20 degrees about an axis with it's center as the center of rotation.  I've managed to write the following macro:


Dim swApp As Object

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

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim CWAddinCallBackObj As Object
Set CWAddinCallBackObj = swApp.GetAddInObject("CosmosWorks.CosmosWorks")
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Dim myFeature As Object
Dim OPFolder As String
Dim i As Integer

For i = 1 To 18
   boolstatus = Part.Extension.SelectByID2("cap.STEP", "SOLIDBODY", 0, 0, 0, False, 1, Nothing, 0)
   Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 2.20016092441915E-02, 0.0185, 0, 0, 0.349065850398867, False, 1)

   OPFolder = "\\nas01.itap.purdue.edu\puhome\desktop\testrun\images\cap_" & i
   longstatus = Part.SaveAs3(OPFolder & ".JPG", 0, 0)
Next i

Set CWAddinCallBackObj = Nothing
Set COSMOSWORKSObj = Nothing

End Sub




But it throws an error saying "Object variable or With block variable not set" at the first line inside the for loop

I am unable to figure out the error or how to debug it. Also, I was wondering if there's a way to rotate the component by center without actually specifying the coordinates of the center. Since I'd be using the same code for several files, I'd want it to be generic.