mihalj
Mechanical
- Apr 15, 2009
- 40
Hi,
Can anyone tell me what is wrong here (and I used example from SW help as well as book)?
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Partial Class SolidWorksMacro
Public Sub main()
Dim swModel As IModelDoc2
Dim swConfMgr As ConfigurationManager
Dim swConf As Configuration
' Dim swAssembly As IAssemblyDoc
' Dim subAssembly As IAssemblyDoc
' Dim swPart As IPartDoc
' Dim compInAssyNo As Integer
Dim topLevelOnly As Boolean = True
Dim swRootComp As IComponent2
swModel = swApp.IActiveDoc2
swConfMgr = swModel.ConfigurationManager
swConf = swConfMgr.ActiveConfiguration
swRootComp = swConf.GetRootComponent
If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
TraverseComponent(swRootComp, 1)
Else
MsgBox("Please open main assembly!")
End If
End Sub
Sub TraverseComponent(ByVal swComp As IComponent2, ByVal nLevel As Long)
Dim iChildComp As Object
Dim swChildComp As IComponent2
Dim i As Long
Dim swModelDoc As IModelDoc2
iChildComp = swComp.IGetChildren
' MsgBox("iChildComp has " & UBound(iChildComp) & " components!")
For i = 0 To UBound(iChildComp) '<-- my first message that is unable to handle type cast - and this is example from SW help
MsgBox("Top Assembly-component #" & i & "!")
swChildComp = iChildComp(i)
'Here I want to check swChildComp is it Assembly or Part and based on that decide which function will be used
'TraverseComponent or TraversePart
swModelDoc = swChildComp.GetModelDoc2 'what is wrong here?
If swModelDoc.GetType = swDocumentTypes_e.swDocPART Then
MsgBox("It is part!")
TraversePart(swChildComp, 1)
Else
TraverseComponent(swChildComp, nLevel + 1)
End If
Next i
End Sub
Sub TraversePart(ByVal swComp As IComponent2, ByVal nLevel As Long)
MsgBox("This is part!-TraversePart f-tion")
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class
Can anyone tell me what is wrong here (and I used example from SW help as well as book)?
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Partial Class SolidWorksMacro
Public Sub main()
Dim swModel As IModelDoc2
Dim swConfMgr As ConfigurationManager
Dim swConf As Configuration
' Dim swAssembly As IAssemblyDoc
' Dim subAssembly As IAssemblyDoc
' Dim swPart As IPartDoc
' Dim compInAssyNo As Integer
Dim topLevelOnly As Boolean = True
Dim swRootComp As IComponent2
swModel = swApp.IActiveDoc2
swConfMgr = swModel.ConfigurationManager
swConf = swConfMgr.ActiveConfiguration
swRootComp = swConf.GetRootComponent
If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
TraverseComponent(swRootComp, 1)
Else
MsgBox("Please open main assembly!")
End If
End Sub
Sub TraverseComponent(ByVal swComp As IComponent2, ByVal nLevel As Long)
Dim iChildComp As Object
Dim swChildComp As IComponent2
Dim i As Long
Dim swModelDoc As IModelDoc2
iChildComp = swComp.IGetChildren
' MsgBox("iChildComp has " & UBound(iChildComp) & " components!")
For i = 0 To UBound(iChildComp) '<-- my first message that is unable to handle type cast - and this is example from SW help
MsgBox("Top Assembly-component #" & i & "!")
swChildComp = iChildComp(i)
'Here I want to check swChildComp is it Assembly or Part and based on that decide which function will be used
'TraverseComponent or TraversePart
swModelDoc = swChildComp.GetModelDoc2 'what is wrong here?
If swModelDoc.GetType = swDocumentTypes_e.swDocPART Then
MsgBox("It is part!")
TraversePart(swChildComp, 1)
Else
TraverseComponent(swChildComp, nLevel + 1)
End If
Next i
End Sub
Sub TraversePart(ByVal swComp As IComponent2, ByVal nLevel As Long)
MsgBox("This is part!-TraversePart f-tion")
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class