×
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 Opening each part file in an assembly.
2

API Opening each part file in an assembly.

API Opening each part file in an assembly.

(OP)
I am giving a shot at this API stuff and so far really like it but I am having a hard time with the following.

I want to open up each part file no matter how deep an a assembly file.  Run a special macro that I have, and then close the file.

I am using the traverse assembly macro out of the help section, and I put a line in there for I want to open the part file (I think it is in the right place).

What am I missing here?

'Make All Assembly Components Visible Example (VB)
'This example shows how to make all assembly components visible.

 

'---------------------------------------

'

' Preconditions: An assembly document is open.

'

' Postconditions: Any hidden assembly components are made visible.

'

'---------------------------------------

Option Explicit

Dim swApp As SldWorks.SldWorks

Dim swPart As SldWorks.ModelDoc2

Dim swFeat As SldWorks.Feature

Dim Indent As Long



 

Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

    Dim vChildCompArr               As Variant

    Dim vChildComp                  As Variant

    Dim swChildComp                 As SldWorks.Component2

    Dim swCompConfig                As SldWorks.Configuration

    Dim sPadStr                     As String

    Dim i                           As Long

    

    For i = 0 To nLevel - 1

        sPadStr = sPadStr + "  "

    Next i

    

    vChildCompArr = swComp.GetChildren

    For Each vChildComp In vChildCompArr

        Set swChildComp = vChildComp

        Debug.Print sPadStr & swChildComp.Name2 & " <" & swChildComp.ReferencedConfiguration & ">"
        
'       Right here is where I want to open the part file and run my other macro
        
        TraverseComponent swChildComp, nLevel + 1

    Next

End Sub

 

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swAssy                      As SldWorks.AssemblyDoc

    Dim swConf                      As SldWorks.Configuration

    Dim swRootComp                  As SldWorks.Component2

    Dim bRet                        As Boolean

    

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swConf = swModel.GetActiveConfiguration

    Set swRootComp = swConf.GetRootComponent

    

    Debug.Print "File = " & swModel.GetPathName

    

    TraverseComponent swRootComp, 1

End Sub

RE: API Opening each part file in an assembly.

Starting after this line:

vChildCompArr = swComp.GetChildren

Put this:

For Each vChildComp In vChildCompArr
    Set swChildComp = vChildComp

    swModDoc = swChildComp.GetModelDoc()
    
    FileTyp = swModelDoc.GetType
    strPrt = swModDoc.GetPathName

    If FileTyp = swDocPART Then
        Set Part = swApp.OpenDoc6(strPrt, swDocPART, swOpenDocOptions_Silent, "", longstatus, longwarnings)
    End If

    If FileTyp = swDocASSEMBLY Then
        Set Part = swApp.OpenDoc6(strPrt, swDocASSEMBLY, swOpenDocOptions_Silent, "", longstatus, longwarnings)
    End If


And erase everything until here:

'       Right here is where I want to open the part file and run my other macro
        
        TraverseComponent swChildComp, nLevel + 1

    Next

End Sub

RE: API Opening each part file in an assembly.

(OP)
Thanks alexit,

The only problem I got now is defining the new variables that you put in.

I am guessing the following:
dim swModDoc as sldworks.modeldoc2
dim filetyp as long
dim strPrt as long
dim part as object
dim longstatus as long
dim longwarnings as long
Dim swModelDoc As SldWorks.Component2

Thanks for the help

RE: API Opening each part file in an assembly.

(OP)
I am having a problem with this line, but I am pretty sure it is the way I am defining it

swmoddoc = swChildComp.GetModelDoc()

RE: API Opening each part file in an assembly.

How "open" do you need the files to be?  If you open the assembly file each part file is opened in memory unless it is either suppressed or lightweight.  You can then traverse through the collection of all open documents, making them visible if you need to.  If what you want is to open all parts as if for editing, you can strip out a few lines from the macro at FAQ559-1231 and be done.

RE: API Opening each part file in an assembly.

ditto to cadnutcase's issue here.

RE: API Opening each part file in an assembly.

(OP)
Okay here is the whole problem.

I am trying to open each part file from one master assembly file and export specific cut list properties from each body to an excel spread sheet would be nice, text delimited file would be just as good.

It seems that the other macros that I am using the file has to be open and current to export the properties.

If somebody has a macro to do this already, I would gladly send them some beer money, I am tired of screwing around with this.  Especially since it is something that I think Solidworks should do already.

Yes I have sent an enchanment request.
Yes I have complained to my VAR.
Yes I am going home for the night.

RE: API Opening each part file in an assembly.

Sorry, I should have explained further.  Any time you open an assembly all part files that are not suppressed or lightweight are opened automatically.  Each open file is a ModelDoc2 object and has a True/False .Visible property.  This is different from component visibility in the assembly.  The Visible property is true on any file (part, assembly, or dwg) that has a graphics window and false on files that are open just because a visible file references it.  I've stripped away lines of the macro I referenced above.  I've marked where you can call your other macro.

CODE

Sub ActivateOpenFilesAndRunMyMacro()
Dim swDoc As SldWorks.ModelDoc2
Dim swAllDocs As EnumDocuments2
Dim FirstDoc As SldWorks.ModelDoc2
Dim dummy As Boolean
Dim NumDocsReturned As Long
Dim DocCount As Long
Dim i As Long
Dim sMsg As String
Dim swApp As SldWorks.SldWorks
Dim bDocWasVisible As Boolean

Set swApp = Application.SldWorks
Set swAllDocs = swApp.EnumDocuments2
Set FirstDoc = swApp.ActiveDoc

DocCount = 0
swAllDocs.Reset
swAllDocs.Next 1, swDoc, NumDocsReturned
While NumDocsReturned <> 0
    bDocWasVisible = swDoc.Visible
    swApp.ActivateDoc swDoc.GetPathName
'************************
'*Call your macro that works on the open, visible, active
'*document here
'************************
    swDoc.Visible = bDocWasVisible
    swAllDocs.Next 1, swDoc, NumDocsReturned
    DocCount = DocCount + 1
Wend

swApp.ActivateDoc FirstDoc.GetPathName
End Sub

RE: API Opening each part file in an assembly.

You may want to have a close look at thread559-155921.  This is a very similar situation in that it iterates through the child components of an assembly looking at properties.  Be aware that iterating through child components will result in a child doc being returned once for every instance.  If you do use the iteration approach you will be calling child parts more than once when there are multiple instances and will have to reject the duplicate entries as you step through the tree.

If you take the enum documents route of iterating through the open docs as Handleman suggested, be aware that it will look at all open documents.  Therefore if you have other docs in addition to your assembly open they will be captured by the code. You should test for this if you want to make the routine bulletproof.

RE: API Opening each part file in an assembly.

Good call by Stoker.  Since you're putting together some sort of master cut list I'm guessing you'll want every instance counted.  If that's the case, (mostly) ignore my posts.  smile

RE: API Opening each part file in an assembly.

(OP)
Actually I want it to look at every instance.  Basically once I get a master cut list put together.  I can sum all the like parts in excel, export that to a one-dimensional bin packing program, and I can let our purchasing department know how many randoms to buy.

I appreciate all the help from alexit, stoker and handleman.  If I get this thing working and anybody is interested in it, I will post the final code.

cadnutcase

RE: API Opening each part file in an assembly.

This is great.

Many a time I've gone through my assembly file opening up the parts one at a time so that I can work on them. A lot of the time I also will open the drawing file for each part. Is it possible to add a few lines of code to this macro to allow the assoiciate drawing file to open when the part file opens?

I always have my drawing files and part file in the same directory and the drawing file has the same file name as its
part file.

thanks in advance
racingd98

RE: API Opening each part file in an assembly.

Are you talking about the macro I posted?  If so, and you want all the files to be shown and not re-hidden, you will have to comment out the line

swDoc.Visible = bDocWasVisible

If, as you say, the drawing file is always in the same folder as the part file with the same name, you can definitely add a few lines to do that.  You would probably use swDoc.GetPathName to get the path of the current part file, strip off the extension and replace with .slddrw, and open the file with OpenDoc6.  Of course, you'd have to first test and see if that file exists.  You can do that with standard VBA functions.

Be warned that opening a bunch of graphics windows will likely hurt your performance.

RE: API Opening each part file in an assembly.


Yes, I forgot to post that I had already stripped that piece of code from what you posted. It works really good for opening the part files up and keeping them open. I guess when I get time to play I'll add the code (or at least try) for opening the drawing files. Most of my code writting is just dabbling and not real code work. i have a lot to learn.

thanks

RE: API Opening each part file in an assembly.

Try this one, RacingD.  I think the existence check may not be necessary if we check to see if OpenDoc6 succeeds in setting myDwgDoc to an object.  No guarantees that this code even works, as I'm typing straight into my browser. smile

CODE


Sub ActivateOpenFilesAndRunMyMacro()
Dim swDoc As SldWorks.ModelDoc2
Dim swAllDocs As EnumDocuments2
Dim FirstDoc As SldWorks.ModelDoc2
Dim dummy As Boolean
Dim NumDocsReturned As Long
Dim DocCount As Long
Dim i As Long
Dim sMsg As String
Dim swApp As SldWorks.SldWorks
Dim bDocWasVisible As Boolean
Dim OpenWarnings as Long
Dim OpenErrors as Long
Dim DwgPath as String
Dim myDwgDoc as SldWorks.ModelDoc2

Set swApp = Application.SldWorks
Set swAllDocs = swApp.EnumDocuments2
Set FirstDoc = swApp.ActiveDoc

DocCount = 0
swAllDocs.Reset
swAllDocs.Next 1, swDoc, NumDocsReturned
While NumDocsReturned <> 0
    bDocWasVisible = swDoc.Visible
    swApp.ActivateDoc swDoc.GetPathName
    DwgPath = swDoc.GetPathName
    If LCase(Right(DwgPath,3)) <> "drw" Then
        DwgPath = Left(DwgPath, Len(DwgPath-3)) & "drw")
        Set myDwgDoc = swApp.OpenDoc6(DwgPath, swDocDwg, swOpenDocOptions_Silent, "", OpenErrors, OpenWarnings)
        If Not myDwgDoc Is Nothing Then
            swApp.ActivateDoc myDwgDoc.GetPathName
            set myDwgDoc = Nothing
        End If
    End If
    swAllDocs.Next 1, swDoc, NumDocsReturned
    DocCount = DocCount + 1
Wend

swApp.ActivateDoc FirstDoc.GetPathName
End Sub

RE: API Opening each part file in an assembly.


Handleman, thanks for your help. I tried your code but unfortunately, it fails on:

DwgPath = Left(DwgPath, Len(DwgPath-3)) & "drw")

I did change it to read:

DwgPath = (Left(DwgPath, Len(DwgPath - 3)) & "drw")

But it continues to fail on the -3 part, I think.

RacingD98

RE: API Opening each part file in an assembly.

Oops, misplaced parenthesis.  Should read:

DwgPath = Left(DwgPath, Len(DwgPath)-3) & "drw"

Sorry!

RE: API Opening each part file in an assembly.

Found another problem this morning when I tried to run it.  In the OpenDoc6 line swDocDwg should be swDocDRAWING, as below.  This macro now works as is on my m/c.

CODE

Sub ShowAllOpenFiles()
Dim swDoc As SldWorks.ModelDoc2
Dim swAllDocs As EnumDocuments2
Dim FirstDoc As SldWorks.ModelDoc2
Dim dummy As Boolean
Dim NumDocsReturned As Long
Dim DocCount As Long
Dim i As Long
Dim sMsg As String
Dim swApp As SldWorks.SldWorks
Dim bDocWasVisible As Boolean
Dim OpenWarnings As Long
Dim OpenErrors As Long
Dim DwgPath As String
Dim myDwgDoc As SldWorks.ModelDoc2

Set swApp = Application.SldWorks
Set swAllDocs = swApp.EnumDocuments2
Set FirstDoc = swApp.ActiveDoc

DocCount = 0
swAllDocs.Reset
swAllDocs.Next 1, swDoc, NumDocsReturned
While NumDocsReturned <> 0
    bDocWasVisible = swDoc.Visible
    swApp.ActivateDoc swDoc.GetPathName
    DwgPath = swDoc.GetPathName
    If (LCase(Right(DwgPath, 3)) <> "drw") And (DwgPath <> "") Then
        DwgPath = Left(DwgPath, Len(DwgPath) - 3) & "drw"
        Set myDwgDoc = swApp.OpenDoc6(DwgPath, swDocDRAWING, swOpenDocOptions_Silent, "", OpenErrors, OpenWarnings)
        If Not myDwgDoc Is Nothing Then
            swApp.ActivateDoc myDwgDoc.GetPathName
            Set myDwgDoc = Nothing
        End If
    End If
    swAllDocs.Next 1, swDoc, NumDocsReturned
    DocCount = DocCount + 1
Wend

swApp.ActivateDoc FirstDoc.GetPathName
End Sub

RE: API Opening each part file in an assembly.

Handleman,

oooooooooohhhhhhhh, so sweet.

thanks a ton, this will save me so much time.

thanks again, RacingD

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