ShaggyPE
Mechanical
- Sep 8, 2003
- 1,127
I am trying to do a seemingly simple task that is to be a portion of a much larger macro. I have created several test components and assemblies. The children of the top assy have all been assigned a custom property called "Level". The value associated with "Level" is 0 or 1. The function of this macro is to traverse through the children of the already open top assy, looking at the Level attribute. It will then create and assign a Level attribute to the top assy. The value of this attribute will be one higher than the highest of the children components.
The area I am having trouble with is grabbing the value of the attribute from the children. I downloaded a sample macro called TraverseAssembly to help me creating this one. The sample worked well to output the name of the children, I want to dive just a bit deeper. Below is my code. In bold is the line of code where I receive an error. Any help on this would be appreciated. Mind you I don't have much of a background in code so specific info is appreciated.
-Shaggy
Const swDocASSEMBLY = 2
Dim swApp As SldWorks.SldWorks
Dim ModelDoc As SldWorks.ModelDoc2
Dim Level As Integer
Dim strText As Variant
Sub Main()
Dim swRootComp As SldWorks.Component2
Dim swConf As SldWorks.configuration
Dim FileName As String
Set swApp = Application.SldWorks
Set ModelDoc = swApp.ActiveDoc
If Not ModelDoc Is Nothing Then
If ModelDoc.GetType = swDocASSEMBLY Then
'Get the active configuration
Set swConf = ModelDoc.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
'Traverse the assembly
TraverseComponent swRootComp
End If
End If
ModelDoc.AddCustomInfo3 "", Level, swCustomInfoText, ""
ModelLevel = childlevel + 1
ModelDoc.CustomInfo2("", Level) = ModelLevel
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2)
'this recursively traverses all of the components in an assembly
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim i As Long
Dim childlevel As Long
Dim childlevel_temp As Long
Dim ModDoc As Object ' ModelDoc2 of child component
Dim ModName As String
vChildComp = swComp.GetChildren
childlevel_temp = 0
childlevel = 0
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
ModName = swChildComp.Name
Set ModDoc = swChildComp.GetModelDoc()
childlevel_temp = swChildComp.GetCustomInfoValue("", Level)
If childlevel_temp > childlevel Then
childlevel = childlevel_temp
End If
Next i
End Sub
The area I am having trouble with is grabbing the value of the attribute from the children. I downloaded a sample macro called TraverseAssembly to help me creating this one. The sample worked well to output the name of the children, I want to dive just a bit deeper. Below is my code. In bold is the line of code where I receive an error. Any help on this would be appreciated. Mind you I don't have much of a background in code so specific info is appreciated.
-Shaggy
Const swDocASSEMBLY = 2
Dim swApp As SldWorks.SldWorks
Dim ModelDoc As SldWorks.ModelDoc2
Dim Level As Integer
Dim strText As Variant
Sub Main()
Dim swRootComp As SldWorks.Component2
Dim swConf As SldWorks.configuration
Dim FileName As String
Set swApp = Application.SldWorks
Set ModelDoc = swApp.ActiveDoc
If Not ModelDoc Is Nothing Then
If ModelDoc.GetType = swDocASSEMBLY Then
'Get the active configuration
Set swConf = ModelDoc.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
'Traverse the assembly
TraverseComponent swRootComp
End If
End If
ModelDoc.AddCustomInfo3 "", Level, swCustomInfoText, ""
ModelLevel = childlevel + 1
ModelDoc.CustomInfo2("", Level) = ModelLevel
End Sub
Sub TraverseComponent(swComp As SldWorks.Component2)
'this recursively traverses all of the components in an assembly
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim i As Long
Dim childlevel As Long
Dim childlevel_temp As Long
Dim ModDoc As Object ' ModelDoc2 of child component
Dim ModName As String
vChildComp = swComp.GetChildren
childlevel_temp = 0
childlevel = 0
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
ModName = swChildComp.Name
Set ModDoc = swChildComp.GetModelDoc()
childlevel_temp = swChildComp.GetCustomInfoValue("", Level)
If childlevel_temp > childlevel Then
childlevel = childlevel_temp
End If
Next i
End Sub