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!

RUN MACRO ON EACH PART WITHIN ASSEMBLY 1

Status
Not open for further replies.

PAT44

Mechanical
Apr 4, 2012
12
CA
Hi,
I wish to open and execute a macro on each file from an open assy. How can I do it?

Thank you
 
Replies continue below

Recommended for you

The simplest way is to dump:
Code:
dir *.sldprt > tempfile.lis
and then read that file into your macro using each line as a filename.

Of course you will have to get dir to output just filenames and extensions and perhaps clean up the file in an editor a bit.

TOP
CSWP, BSSE
Phenom IIx6 1100T = 8GB = FX1400 = XP64SP2 = SW2009SP3
"Node news is good news."
 
it is possible to open my assembly, then lunch a macro that will open the first part within the assy, execute a macro, save and close the part, then do the same with second part.. with all part in the assy ?
 
Hi PAT44,

When you open an assembly it automatically opens all of the files in that assembly. If you want to run code on every part in the assembly then you would do something like this:

Code:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssy As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swCompModel As SldWorks.ModelDoc2
Dim i As Integer
Dim vComps As Variant

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swAssy = swModel
    vComps = swAssy.GetComponents(False)
    For i = 0 To UBound(vComps)
        Set swComp = vComps(i)
        Set swCompModel = swComp.GetModelDoc2
        If swCompModel.GetType = swDocPART Then
            'run your code here
        End If
    Next i
End Sub

Make sure that you use swCompModel as the reference to the part's ModelDoc2 interface. Also, if you need to edit any feature data in that part, you'll want to use IComponent2::GetCorresponding to bring the part's ModelDoc2 interface into the context of the assembly. Otherwise you might run into issues.

Hope this helps.
 
When you create a new part, you select new and part to get the template open. You can then insert a part into this new template. Is it possible to insert each part into a new template by the code you just gave to me?
 
That is because when I pack and go an assy, the desing date still the first date. But I the parts and assy get change and then I wish to reset that date, so the only way should be to import each part in a new template. I already have a code into template that set the property.
The only problem is to overwrite parts, because the assy and part will be open. And the same thing should be done with assy too. Any Ideas?
 
and the code you send me is great but it run my code only in the assy. My code is to edit equation and the equation appears only into the assy but I wish that equation to be in each parts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top