×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

API Assembly traversal
3

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
 

RE: API Assembly traversal

(OP)
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.

RE: API Assembly traversal

(OP)
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
 

RE: API Assembly traversal

Is swModelDoc even getting initialized, or is it still "= Nothing"?

Ken

RE: API Assembly traversal

(OP)
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?
 

RE: API Assembly traversal

Quote:

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

RE: API Assembly traversal

mihalj,
Not to get off topic, but you said:

Quote:

(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

RE: API Assembly traversal

Standing, you will want to check out Luke Malpass' book over on his www.angelsix.forum

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

Standing,

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

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

RE: API Assembly traversal

(OP)
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

RE: API Assembly traversal

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources