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
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.
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.
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.
swmoddoc = swChildComp.GetModelDoc()
RE: API Opening each part file in an assembly.
RE: API Opening each part file in an assembly.
RE: API Opening each part file in an assembly.
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.
CODE
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.
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.
RE: API Opening each part file in an assembly.
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.
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.
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.
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.
DwgPath = Left(DwgPath, Len(DwgPath)-3) & "drw"
Sorry!
RE: API Opening each part file in an assembly.
CODE
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.
oooooooooohhhhhhhh, so sweet.
thanks a ton, this will save me so much time.
thanks again, RacingD