Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

API Assembly traversal 3

Status
Not open for further replies.

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
 
Replies continue below

Recommended for you

Well this is done in Visual Basic Net and I don't think there is variant as type.
And also in their help they specified iChildComp As Object (whick is kind of Variant as in VBA).
Thanks anyway for your response.
 
Finally I realized IGetChildren is not function I need, but GetChildren is right one (line "iChildComp = swComp.IGetChildren).
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
 
Is swModelDoc even getting initialized, or is it still "= Nothing"?

Ken
 
Yes it is Nothing (that's what I got when debugging)!
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?
 
But wasn't it initialized with line
swModelDoc = swChildComp.GetModelDoc2
"If" everything else in the code before this is working correct, then yes this is a true statement.

If "swModelDoc = Nothing" after that line has occurred...then something is wrong. I would next verify that "Not(swChildComp= Nothing)"

Ken
 
mihalj,
Not to get off topic, but you said:
(and I used example from SW help as well as book)?
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
 
Standing, you will want to check out Luke Malpass' book over on his
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
 
Thank you Anna. That is what I have been looking for.
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
 
For Standing:
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
 
mihalj,
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor