×
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

RUN MACRO ON EACH PART WITHIN ASSEMBLY

RUN MACRO ON EACH PART WITHIN ASSEMBLY

RUN MACRO ON EACH PART WITHIN ASSEMBLY

(OP)
Hi,
    I wish to open and execute a macro on each file from an open assy. How can I do it?

Thank you  

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

Write a macro...?

Jeff Mirisola
Director of Engineering
M9 Defense
My Blog

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

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
www.engtran.com  www.niswug.org
www.linkedin.com/in/engineeringtransport
Phenom IIx6 1100T = 8GB = FX1400 = XP64SP2 = SW2009SP3
"Node news is good news."

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

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

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

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.

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

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

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

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

RE: RUN MACRO ON EACH PART WITHIN ASSEMBLY

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

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