API: IF, THEN question
API: IF, THEN question
(OP)
My apologies for the length of the mail, but it’s here for clarity. I’ve written a macro and boxed myself into a corner. When I originally figured this out, I was just learning so sorry if its not clean.
On with the question: The first go at the macro would return the values asked for as long as the assembly components were manually set to resolved. Therefore it would work for a part or assembly. I wanted to get fancy and have it automatically resolve the components. From help files, I found the nRetval line to resolve all components. All was well until it stopped working for individual parts.
I would like to build some sort of logic statement the says if assembly than resolve all components and then continue the rest of the macro, if part then do rest of macro.
The italicized text is what was added to the second version when it stopped working on individual parts
'This macro is designed to display the X, Y, & Z values of a part
'or anassembly model at the origin of the model coordinate system.
'It will display the following values in relation to the X, Y & Z axes:
'X, Y & Z coordinates of the center of gravity
'Resultant vector length
'Resultant vector angle
'Imbalance in gram millimeters
'Preconditions: Assembly document is open.
'Postconditions: All lightweight components are resolved and values are displayed.
'-------------------------------------
Option Explicit
Public Enum swComponentResolveStatus_e
swResolveOk = 0
swResolveAbortedByUser = 1
swResolveNotPerformed = 2
swResolveError = 3
End Enum
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim nRetval As Long
'Define Mass property variables
Dim mp, X, Y, Z, Mass 'mass properties call value, x, y and z values, mass value
'Define Resultant vector variables
Dim vxy, vyz, vxz 'xy, yz, and xz vectors
'Define vector angle variables
Dim axy, ayz, axz 'xy, yz and xz angles
'Define Imbalance value variables
Dim ixy, iyz, ixz 'xy, yz and xz imbalance
'Define display value variables
Dim dx, dy, dz, dmass 'x, y, z and mass
Dim dvxy, dvyz, dvxz 'xy,yz and xz vectors
Dim daxy, dayz, daxz 'xy, yz and xz vector angles
Dim dixy, diyz, dixz 'xy,yz and xz imbalance
Sub main()
Put the IF,THEN in this area somewhere before 'Calculate the imbalance variables
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set swAssy = Part
'Set lightweight parts to resolved
nRetval = swAssy.ResolveAllLightWeightComponents(False)
'Calculate the imbalance variables
'Get mass properties
mp = Part.GetMassProperties
'Convert mass properties to mm or grams
X = Str(mp(0)) * 1000: Y = Str(mp(1)) * 1000: Z = Str(mp(2)) * 1000: Mass = Str(mp(5)) * 1000
'Resultant vectors lengths
vxy = Sqr(X ^ 2 + Y ^ 2): vyz = Sqr(Y ^ 2 + Z ^ 2): vxz = Sqr(X ^ 2 + Z ^ 2)
'Resultant vector angles
'XY angle
axy = Cos(X / vxy)
If Y < 0 Then
axy = 360 - axy
Else
axy = axy
End If
'YZ angle
ayz = Cos(Z / vyz)
If Y < 0 Then
ayz = 360 - ayz
Else
ayz = ayz
End If
'YZ angle
axz = Cos(X / vxz)
If Z < 0 Then
axz = 360 - axz
Else
axz = axz
End If
'Imbalance values
ixy = vxy * Mass: iyz = vyz * Mass: ixz = vxz * Mass
'Define display values
dx = FormatNumber(X, 4): dy = FormatNumber(Y, 4): dz = FormatNumber(Z, 4)
dmass = FormatNumber(Mass, 4)
dvxy = FormatNumber(vxy, 4): dvyz = FormatNumber(vyz, 4): dvxz = FormatNumber(vxz, 4)
daxy = FormatNumber(axy, 4): dayz = FormatNumber(ayz, 4): daxz = FormatNumber(axz, 4)
dixy = FormatNumber(ixy, 4): diyz = FormatNumber(iyz, 4): dixz = FormatNumber(ixz, 4)
'Display mass property values and imbalance values in form
Form.dx = dx & " mm": Form.dy = dy & " mm": Form.dz = dz & " mm"
Form.dmass = dmass & " grams"
Form.dvxy = dvxy & " mm": Form.dvyz = dvyz & " mm": Form.dvxz = dvxz & " mm"
Form.daxy = daxy: Form.dayz = dayz: Form.daxz = daxz
Form.dixy = dixy & " gmm": Form.diyz = diyz & " gmm": Form.dixz = dixz & " gmm"
Form.Show
End Sub
Thanks in advance for any suggestions.
On with the question: The first go at the macro would return the values asked for as long as the assembly components were manually set to resolved. Therefore it would work for a part or assembly. I wanted to get fancy and have it automatically resolve the components. From help files, I found the nRetval line to resolve all components. All was well until it stopped working for individual parts.
I would like to build some sort of logic statement the says if assembly than resolve all components and then continue the rest of the macro, if part then do rest of macro.
The italicized text is what was added to the second version when it stopped working on individual parts
'This macro is designed to display the X, Y, & Z values of a part
'or anassembly model at the origin of the model coordinate system.
'It will display the following values in relation to the X, Y & Z axes:
'X, Y & Z coordinates of the center of gravity
'Resultant vector length
'Resultant vector angle
'Imbalance in gram millimeters
'Preconditions: Assembly document is open.
'Postconditions: All lightweight components are resolved and values are displayed.
'-------------------------------------
Option Explicit
Public Enum swComponentResolveStatus_e
swResolveOk = 0
swResolveAbortedByUser = 1
swResolveNotPerformed = 2
swResolveError = 3
End Enum
Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim nRetval As Long
'Define Mass property variables
Dim mp, X, Y, Z, Mass 'mass properties call value, x, y and z values, mass value
'Define Resultant vector variables
Dim vxy, vyz, vxz 'xy, yz, and xz vectors
'Define vector angle variables
Dim axy, ayz, axz 'xy, yz and xz angles
'Define Imbalance value variables
Dim ixy, iyz, ixz 'xy, yz and xz imbalance
'Define display value variables
Dim dx, dy, dz, dmass 'x, y, z and mass
Dim dvxy, dvyz, dvxz 'xy,yz and xz vectors
Dim daxy, dayz, daxz 'xy, yz and xz vector angles
Dim dixy, diyz, dixz 'xy,yz and xz imbalance
Sub main()
Put the IF,THEN in this area somewhere before 'Calculate the imbalance variables
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set swAssy = Part
'Set lightweight parts to resolved
nRetval = swAssy.ResolveAllLightWeightComponents(False)
'Calculate the imbalance variables
'Get mass properties
mp = Part.GetMassProperties
'Convert mass properties to mm or grams
X = Str(mp(0)) * 1000: Y = Str(mp(1)) * 1000: Z = Str(mp(2)) * 1000: Mass = Str(mp(5)) * 1000
'Resultant vectors lengths
vxy = Sqr(X ^ 2 + Y ^ 2): vyz = Sqr(Y ^ 2 + Z ^ 2): vxz = Sqr(X ^ 2 + Z ^ 2)
'Resultant vector angles
'XY angle
axy = Cos(X / vxy)
If Y < 0 Then
axy = 360 - axy
Else
axy = axy
End If
'YZ angle
ayz = Cos(Z / vyz)
If Y < 0 Then
ayz = 360 - ayz
Else
ayz = ayz
End If
'YZ angle
axz = Cos(X / vxz)
If Z < 0 Then
axz = 360 - axz
Else
axz = axz
End If
'Imbalance values
ixy = vxy * Mass: iyz = vyz * Mass: ixz = vxz * Mass
'Define display values
dx = FormatNumber(X, 4): dy = FormatNumber(Y, 4): dz = FormatNumber(Z, 4)
dmass = FormatNumber(Mass, 4)
dvxy = FormatNumber(vxy, 4): dvyz = FormatNumber(vyz, 4): dvxz = FormatNumber(vxz, 4)
daxy = FormatNumber(axy, 4): dayz = FormatNumber(ayz, 4): daxz = FormatNumber(axz, 4)
dixy = FormatNumber(ixy, 4): diyz = FormatNumber(iyz, 4): dixz = FormatNumber(ixz, 4)
'Display mass property values and imbalance values in form
Form.dx = dx & " mm": Form.dy = dy & " mm": Form.dz = dz & " mm"
Form.dmass = dmass & " grams"
Form.dvxy = dvxy & " mm": Form.dvyz = dvyz & " mm": Form.dvxz = dvxz & " mm"
Form.daxy = daxy: Form.dayz = dayz: Form.daxz = daxz
Form.dixy = dixy & " gmm": Form.diyz = diyz & " gmm": Form.dixz = dixz & " gmm"
Form.Show
End Sub
Thanks in advance for any suggestions.
Christopher Zona - Senior CAD Designer
Concord, Ontario






RE: API: IF, THEN question
CODE
'Set lightweight parts to resolved
nRetval = swAssy.ResolveAllLightWeightComponents(False)
with this:
CODE
Set swAssy = Part
'Set lightweight parts to resolved
nRetval = swAssy.ResolveAllLightWeightComponents(False)
End If
RE: API: IF, THEN question
Is there something that I haven't defined?
Christopher Zona - Senior CAD Designer
Concord, Ontario
RE: API: IF, THEN question
Set Part = swApp.ActiveDoc
?
RE: API: IF, THEN question
The error says, "Object variable or With block variable not set."
Does it make a difference that I am SW2005 SP5?
Regards,
Christopher Zona - Senior CAD Designer
Concord, Ontario
RE: API: IF, THEN question
Set Part = swApp.ActiveDoc
Then, if the specific type of document is an assembly you want to perform the assembly-only function of ResolveAllLightweightComponents. That is the "If" block I posted previously.
I know that "Part" is the default variable that SW uses for the active ModelDoc2 object when you record a macro. I usually try to avoid that, because if the active document is an assembly or a drawing "Part" is not really an accurate description. I usually use "swDoc" for the ModelDoc2 object and then swPart, swDrawing, and swAssy for the specific PartDoc, DrawingDoc, and AssemblyDoc objects, respectively.