API Assembly traversal
API Assembly traversal
(OP)
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






RE: API Assembly traversal
"Dim iChildComp As Object"
s/b
"Dim iChildComp As Variant"
Matt Lorono
CAD Engineer/ECN Analyst
Silicon Valley, CA
Lorono's SolidWorks Resources
Co-moderator of Solidworks Yahoo! Group
and Mechnical.Engineering Yahoo! Group
RE: API Assembly traversal
And also in their help they specified iChildComp As Object (whick is kind of Variant as in VBA).
Thanks anyway for your response.
RE: API Assembly traversal
At least I do not get issues with array type conversion.
Now I have trouble to find out how to approach GetModelDoc2 function (member of IComponent2 interface).
I call
swModelDoc = swChildComp.GetModelDoc2 'what is wrong here?
If swModelDoc.GetType = swDocumentTypes_e.swDocPART Then ...
and as per SolidWorks help GetModelDoc2 returns pointer on IModelDoc2 object. If that is true then I do not understand why I am getting error on "swModelDoc.GetType" because I should be able to get type of document with GetType function?
Anyway, is there anyone who knows how I can check IComponent2 document (in this case swChildComp) is it part or assembly.
Thanks!
I am also providing complete code so it is easier to understand:
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Array
Imports System.Diagnostics
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 vChildComp As Object
Dim swChildComp As IComponent2
Dim sPadStr As String = " "
Dim i As Long
Dim swModelDoc As IModelDoc2
vChildComp = swComp.GetChildren
For i = 0 To UBound(vChildComp)
MsgBox("Top Assembly-component #" & i & "!")
swChildComp = vChildComp(i)
MsgBox(sPadStr & "+" & swChildComp.Name2 & " <" & swChildComp.ReferencedConfiguration & ">")
'Here I want to check swChildComp is it Assembly or Part and based on that decide which function will be used
' swModelDoc = swApp.IActivateDoc3(swChildComp.Name2, True, 0) ---> I tried this one but didn't work
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
RE: API Assembly traversal
Ken
RE: API Assembly traversal
But wasn't it initialized with line
swModelDoc = swChildComp.GetModelDoc2
It is basically same as
Dim i As Integer
i = j+1 (j is earlier initialized and given value,so i just accept value) or I am missing something?
RE: API Assembly traversal
If "swModelDoc = Nothing" after that line has occurred...then something is wrong. I would next verify that "Not(swChildComp= Nothing)"
Ken
RE: API Assembly traversal
Not to get off topic, but you said:
I have been looking for a book that teaches VB.Net and SolidWorks together. Does the one you are using do that?
Standing
SolidWorks Pro 2009 x64, SP3.0, PDMWorks Workgroup, SolidWorks BOM,
HP xw8600, 64-bit Windows Vista Business, Service Pack 1
Intel Xeon CPU, 3.00 GHz, 16 GB RAM, Virtual memory 166682 MB, nVidia Quadro FX 4600
RE: API Assembly traversal
He has a book on the SolidWorks API and programming in C# and VB.Net
It is a good book that I purchased myself last year.
Cheers,
Anna Wood
Anna Built Workstation, Core i7 EE965, FirePro V8700, 12 gigs of RAM, OCZ Vertex 120 Gig SSD
SW2009 SP3.0, Windows 7 RC1
http://www.solidmuse.com
http://www.phxswug.com
RE: API Assembly traversal
Luke's website is http://angelsix.com
Had two thoughts going through my brain when I typed the link in the previous post.
CHeers,
Anna Wood
Anna Built Workstation, Core i7 EE965, FirePro V8700, 12 gigs of RAM, OCZ Vertex 120 Gig SSD
SW2009 SP3.0, Windows 7 RC1
http://www.solidmuse.com
http://www.phxswug.com
RE: API Assembly traversal
A star for you.
Standing
SolidWorks Pro 2009 x64, SP3.0, PDMWorks Workgroup, SolidWorks BOM,
HP xw8600, 64-bit Windows Vista Business, Service Pack 1
Intel Xeon CPU, 3.00 GHz, 16 GB RAM, Virtual memory 166682 MB, nVidia Quadro FX 4600
RE: API Assembly traversal
Book is "Automating SolidWorks 2009 Using Macros using Visual Basic.Net" - Mike Spens
Regarding this problem - it is solved and thanks to brengine for that one.
Here is the code and for brengine one star.
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Array
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public swModel As IModelDoc2
Public Sub main()
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 vChildComp() As Object
Dim swChildComp As Component2
Dim i As Long
Dim swModelDoc As ModelDoc2 = Nothing
vChildComp = swComp.GetChildren
For i = 0 To (UBound(vChildComp) - 1)
MsgBox("Top Assembly-component #" & i & "!")
swChildComp = vChildComp(i)
If swChildComp Is Nothing Then
MsgBox("swChildComp is Nothing for " & i & " element!")
Else
swModelDoc = swChildComp.GetModelDoc2
If swModelDoc IsNot Nothing Then
If swModelDoc.GetType = swDocumentTypes_e.swDocASSEMBLY Then
MsgBox("It is assembly")
TraverseComponent(swChildComp, nLevel + 1)
Else
MsgBox("It is part")
TraversePart(swComp, nLevel)
End If
Else
MsgBox("swModelDoc is nothing!")
End If
End If
Next i
MsgBox("It is end of this module!")
End Sub
Sub TraversePart(ByVal swComp As IComponent2, ByVal nLevel As Long)
MsgBox("Passing to TraversePart works")
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class
RE: API Assembly traversal
Thanks for the book recommendation. Also thanks for posting the working code. A star for that one.
Standing
SolidWorks Pro 2009 x64, SP3.0, PDMWorks Workgroup, SolidWorks BOM,
HP xw8600, 64-bit Windows Vista Business, Service Pack 1
Intel Xeon CPU, 3.00 GHz, 16 GB RAM, Virtual memory 166682 MB, nVidia Quadro FX 4600