iCopy Adaptivity Lost During Component Replace

iCopy Adaptivity Lost During Component Replace

HogueOne
Advocate Advocate
372 Views
1 Reply
Message 1 of 2

iCopy Adaptivity Lost During Component Replace

HogueOne
Advocate
Advocate

In my workflow I need to use iCopy hundreds of times per job, so it is extremely time consuming to change the file name and save location for each iCopy assembly I generate. Instead, I just accept the defaults. I have a code that pushes iProperties from the active assembly down to the iCopies, and renames the occurrence name and part number with a number sequence suffix. The code you see below basically allows me to do a save and replace with a file name that matches the new part number. This works great except for one big problem, highlighted in the image below. During the process, adaptivity is turned off, so I can no longer edit the iCopy definition. Is there a way to maintain or restore this adaptivity with iLogic? I'm really new to programming so I hope you can simplify your answer for my small brain. Thank you!

 

HogueOne_0-1721236776280.png

Sub Main()
    ' Prompt user with confirmation dialog
    Dim confirmMsg As String
    confirmMsg = "Are you sure you want to reorganize your files into a new folder? Adaptivity will be lost."
    
    If MsgBox(confirmMsg, vbYesNo + vbQuestion, "Confirmation") = vbNo Then
        Exit Sub ' Exit script if user selects No
    End If
    
    ' Prompt user to select a folder
    Dim folderDialog As Object
    folderDialog = CreateObject("Shell.Application").BrowseForFolder(0, "SELECT THE PROJECT FOLDER", 0, 0)

    If folderDialog Is Nothing Then
        MsgBox("No folder selected. Exiting script.", vbExclamation, "No Folder Selected")
        Exit Sub
    End If

    ' Get selected folder path
    Dim sPath As String
    sPath = folderDialog.Self.Path

    ' Create folder named "ELEVATION " & value of custom iProperty "Elevation"
    Try
        Dim elevationValue As String
        elevationValue = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties").Item("Elevation").Value

        Dim elevationFolder As String
        elevationFolder = System.IO.Path.Combine(sPath, "ELEVATION " & elevationValue)

        If Not System.IO.Directory.Exists(elevationFolder) Then
            System.IO.Directory.CreateDirectory(elevationFolder)
        End If

        ' Create folder named "ZONE " & value of custom iProperty "ZoneID" inside "ELEVATION" folder
        Dim zoneIDValue As String
        zoneIDValue = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties").Item("ZoneID").Value

        Dim zoneFolder As String
        zoneFolder = System.IO.Path.Combine(elevationFolder, "ZONE " & zoneIDValue)

        If Not System.IO.Directory.Exists(zoneFolder) Then
            System.IO.Directory.CreateDirectory(zoneFolder)
        End If

        ' Update sPath to point to the zoneFolder
        sPath = zoneFolder

        ' Get all occurrences in the active assembly document
        Dim oOccurrences As ComponentOccurrences
        oOccurrences = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences

        ' String to collect skipped occurrences
        Dim skippedOccurrences As String
        skippedOccurrences = ""

        ' Characters not allowed in file names
        Dim invalidChars As String
        invalidChars = "\/:*?""<>|"

        ' Process each top-level occurrence
        For Each oOcc As ComponentOccurrence In oOccurrences
            ProcessOccurrence(oOcc, sPath, invalidChars, skippedOccurrences) ' Process occurrences in the "ZONE" folder
        Next oOcc

        ' Display message box with skipped occurrences
        If skippedOccurrences <> "" Then
            MsgBox("The following occurrences were skipped because files already exist:" & vbCrLf & vbCrLf & skippedOccurrences, vbInformation, "Skipped Occurrences")
        Else
            MsgBox("All occurrences processed successfully.", vbInformation, "Process Complete")
        End If

    Catch ex As Exception
        MsgBox("Error creating folders or processing occurrences: " & ex.Message, vbExclamation, "Error")
        Exit Sub
    End Try
End Sub

Sub ProcessOccurrence(ByVal oOcc As ComponentOccurrence, ByVal sPath As String, ByVal invalidChars As String, ByRef skippedOccurrences As String)
    Try
        ' Get document from occurrence
        Dim oDoc As Document
        oDoc = oOcc.Definition.Document

        ' Get part number
        Dim partNumber As String
        partNumber = iProperties.Value(oOcc.Name, "Project", "Part Number")

        ' Sanitize part number for file name compatibility
        For Each ch As Char In invalidChars.ToCharArray()
            partNumber = partNumber.Replace(ch, "")
        Next

        Dim oExt As String
        oExt = System.IO.Path.GetExtension(oDoc.FullFileName)

        ' Construct full path
        Dim oName As String
        oName = System.IO.Path.Combine(sPath, partNumber & oExt)

        ' Check if file already exists in the folder
        If System.IO.File.Exists(oName) Then
            ' Append to skipped occurrences string
            skippedOccurrences = skippedOccurrences & "- " & oOcc.Name & vbCrLf
            Exit Sub ' Skip processing sub occurrences since this occurrence is skipped
        End If

        ' Save new file
        oDoc.SaveAs(oName, True)

        ' Replace occurrence in assembly with new file
        oOcc.Replace(oName, True)

        ' Process sub occurrences recursively
        Dim subOccurrences As ComponentOccurrences
        subOccurrences = oOcc.SubOccurrences

        For Each subOcc As ComponentOccurrence In subOccurrences
            ProcessOccurrence(subOcc, sPath, invalidChars, skippedOccurrences)
        Next subOcc

    Catch ex As Exception
        MsgBox("Error processing component " & oOcc.Name & ": " & ex.Message, vbExclamation, "Error")
    End Try
End Sub

 

0 Likes
Accepted solutions (1)
373 Views
1 Reply
Reply (1)
Message 2 of 2

daltonNYAW9
Advocate
Advocate
Accepted solution

Try ".replace2" it has a "[Keep Adaptivity]" option
https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=ComponentOccurrence_Replace2

If this doesn't work you might be out of luck. Adaptivity links to a specific assembly, so if your trying to create a new assembly and have all the adaptive parts carry over it might be difficult.

0 Likes