Need VBA code to save & replace component

Need VBA code to save & replace component

Anonymous
Not applicable
1,439 Views
7 Replies
Message 1 of 8

Need VBA code to save & replace component

Anonymous
Not applicable

hi,

 

I have a assembly, which contain a library & customized parts. I want to use this assembly as master assembly. Need a VBA code by the help of that I can save & replace these parts in one go.

 

@JhoelForshav 

@JDMather

@ekinsb

@sundars 

@bradeneuropeArthur 

 

0 Likes
1,440 Views
7 Replies
Replies (7)
Message 2 of 8

bradeneuropeArthur
Mentor
Mentor

Can't you use i-logic copy design for this.

This is shipped with inventor and can be found in the ribbon when NO document is opened!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

currently I am using iLogic copy design, in this tool every time I have to close all part.
I want a VBA code that I can use as macros. so in single click I can do all these activities.

0 Likes
Message 4 of 8

sundars
Autodesk
Autodesk

Hi @Anonymous 

 

My apologies for the late response. We have been on our annual shutdown and I had not read my email in a while. For your issue, can you post a copy of your iLogic Rule. You can take your iLogic rule and wrap it up in a VBA script with slight modifications. 

 

We also have a sample vba program in the API help which replaces content parts - you could take a look at that and see if you can modify it to suit your needs. (See attached).

 

Sub ReplaceContentCenterPart()
    ' Set a reference to the active assembly document.
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    ' Prompt user to pick an occurrence
    Dim oOcc As ComponentOccurrence
    Set oOcc = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Pick occurrence to replace")

    If oOcc.DefinitionDocumentType <> kPartDocumentObject Then
        MsgBox "Occurrence does not reference a content part."
        Exit Sub
    End If

    Dim oOccDef As PartComponentDefinition
    Set oOccDef = oOcc.Definition

    If Not oOccDef.IsContentMember Then
        MsgBox "The occurrence does not reference a content part."
        Exit Sub
    End If

    ' Set a reference to the ContentCenter object.
    Dim oContentCenter As ContentCenter
    Set oContentCenter = ThisApplication.ContentCenter

    ' Get the content node (category) "Fasteners:Bolts:Hex Head"
    Dim oContentNode As ContentTreeViewNode
    Set oContentNode = oContentCenter.TreeViewTopNode.ChildNodes.Item("Fasteners").ChildNodes.Item("Bolts").ChildNodes.Item("Hex Head")

    ' Get the "ISO 4015" Family object.
    Dim oFamily As ContentFamily
    For Each oFamily In oContentNode.Families
        If oFamily.DisplayName = "ISO 4015" Then
            Exit For
        End If
    Next

    ' Create a member based on the second row of the family.
    Dim error As MemberManagerErrorsEnum
    Dim strContentPartFileName As String
    Dim strErrorMessage As String
    strContentPartFileName = oFamily.CreateMember(2, error, strErrorMessage)

    Call oOcc.Replace(strContentPartFileName, False)
End Sub

Thanks and Happy New Year

-shiva

Shiva Sundaram
Inventor Development
0 Likes
Message 5 of 8

Anonymous
Not applicable

Happy new year Shiva

 

I have a assembly, which contain a library & customized parts. Actually I want to use this assembly as master assembly & when I run VBA code then all customized parts (not library parts) will save as & replace automatically.

 

above code is for replace content center parts.

0 Likes
Message 6 of 8

sundars
Autodesk
Autodesk

Hi @Anonymous 

 

When you say "Customized parts" are you referring to custom content parts? If so, in the above example we iterate through the content family. For each family you can query whether it's custom? oFamily.IsCustom function will tell you if it's a custom content.

 

    ' Get the "ISO 4015" Family object.
    Dim oFamily As ContentFamily
    For Each oFamily In oContentNode.Families
        If oFamily.IsCustom Then
            Exit For
        End If
    Next

If not, then you would have to somehow figure out a way to tag your custom parts with some unique property/attribute and then search for that.

 

Thanks

-shiva

 

Shiva Sundaram
Inventor Development
0 Likes
Message 7 of 8

Anonymous
Not applicable

Sorry for not to explain in detail
Actually I am not using parts from content center.

For Library parts means it is not from contain center. These are local standard parts/assemblies. I just create a folder in local & define path in project file to locate these parts/assemblies.

And for customized parts it means local parts/assemblies saved in same folder where I saved master assembly.

0 Likes
Message 8 of 8

sundars
Autodesk
Autodesk

HI @Anonymous 

 

I am afraid, we dont have any VBA utility in the API to achieve the kind of automation you want. ILogic Copy design is the closest thing to your needs. If not, you would essentially have to build something like that in the API using VBA.

 

You could still take the sample program I attached and modify it to read a source and target list of components from some text file and execute the replace. It could be as simple as some comma delimted text format containing source and target files. Then your vba program could read that and call replace via the API.

 

Thanks

-shiva

Shiva Sundaram
Inventor Development
0 Likes