Replace and rename Component

Replace and rename Component

machiel.veldkamp
Collaborator Collaborator
606 Views
2 Replies
Message 1 of 3

Replace and rename Component

machiel.veldkamp
Collaborator
Collaborator

I saw this piece of code last week somewhere online. I'm looking for it but I can't find it. 

 

 

It does the following:

 

Setup: 

 

ASSY.iam 

- Part1.ipt

- Part2.ipt

- Assy2.ipt

 

_____

 

Run rule does:

 

STEP 1: Ask user to select component (e.g. part1.ipt)

STEP 2: Input new name (e.g. B500.ipt)

STEP 3: Copy selected file and rename to INPUT NAME

STEP 4: Replace component in ASSY.iam

STEP 5: Rename Part1.ipt to Part1 - B500.ipt

 

New Situation:

 

ASSY.iam 

- B500.ipt

- Part2.ipt

- Assy2.ipt

 

 

____________________

 

Has anyone encountered this one? I'm looking for exactly this at the moment. 

 

If not... Help making it pretty please? 

 

 

Thanks for reading. 

 

Cheers

 

 

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

___________________________
0 Likes
607 Views
2 Replies
Replies (2)
Message 2 of 3

Owner2229
Advisor
Advisor

Here you go. Note: This rule won't affect any occurrences of the replaced part in subassemblies.

 

'Get the selection from user
Dim oSelect As Object = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Pick a part occurrence.")
'Exit if nothing is selected
If oSelect Is Nothing Then Exit Sub
'Convert to occurrence
Dim oOcc As ComponentOccurrence = oSelect
'Get the document
Dim oDoc As Document = oOcc.Definition.Document
'Check the document type If oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then Exit Sub
'Get the new name
Dim sNN As String = InputBox("Enter new file name:" & vbLf & "(with or without extension)""New file name")
'Handle cancelation and empty input
If sNN = vbNullString Then Exit Sub

'Handle name without extension
If Not sNN.ToUpper.EndsWith(".IPT") Then sNN = sNN & ".ipt"
'Get the document path Dim sFN As String = oDoc.FullFileName Dim iFP As Integer = InStrRev(sFN, "\", -1) sNN = Microsoft.VisualBasic.Left(sFN, iFP) & sNN
'Save under new name oDoc.SaveAs(sNN, True)
'Replace all occurrences in the top assy
oOcc.Replace(sNN, True)

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 3

Anonymous
Not applicable

Additionally, you can use the Save and Replace component command.

 

    Sub Main()
        Dim oCmdMgr As CommandManager = ThisApplication.CommandManager
        Dim oSelObj As Object = oCmdMgr.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Pick part occurrence")
        If oSelObj Is Nothing Then Exit Sub

        Dim oCtrlDefs As ControlDefinitions = oCmdMgr.ControlDefinitions
        oCtrlDefs.Item("AssemblyBonusTools_SaveAndReplaceComponentCmd").Execute()

    End Sub

This will allow more control over file structure too.

0 Likes