<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic iCopy Adaptivity Lost During Component Replace in Inventor Programming Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-forum/icopy-adaptivity-lost-during-component-replace/m-p/12903369#M8116</link>
    <description>&lt;P&gt;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!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HogueOne_0-1721236776280.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1387877i11880BABB05941ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HogueOne_0-1721236776280.png" alt="HogueOne_0-1721236776280.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;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 " &amp;amp; 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 " &amp;amp; elevationValue)

        If Not System.IO.Directory.Exists(elevationFolder) Then
            System.IO.Directory.CreateDirectory(elevationFolder)
        End If

        ' Create folder named "ZONE " &amp;amp; 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 " &amp;amp; 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 = "\/:*?""&amp;lt;&amp;gt;|"

        ' 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 &amp;lt;&amp;gt; "" Then
            MsgBox("The following occurrences were skipped because files already exist:" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; 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: " &amp;amp; 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 &amp;amp; oExt)

        ' Check if file already exists in the folder
        If System.IO.File.Exists(oName) Then
            ' Append to skipped occurrences string
            skippedOccurrences = skippedOccurrences &amp;amp; "- " &amp;amp; oOcc.Name &amp;amp; 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 " &amp;amp; oOcc.Name &amp;amp; ": " &amp;amp; ex.Message, vbExclamation, "Error")
    End Try
End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 17 Jul 2024 17:28:39 GMT</pubDate>
    <dc:creator>HogueOne</dc:creator>
    <dc:date>2024-07-17T17:28:39Z</dc:date>
    <item>
      <title>iCopy Adaptivity Lost During Component Replace</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/icopy-adaptivity-lost-during-component-replace/m-p/12903369#M8116</link>
      <description>&lt;P&gt;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!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HogueOne_0-1721236776280.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1387877i11880BABB05941ED/image-size/medium?v=v2&amp;amp;px=400" role="button" title="HogueOne_0-1721236776280.png" alt="HogueOne_0-1721236776280.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;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 " &amp;amp; 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 " &amp;amp; elevationValue)

        If Not System.IO.Directory.Exists(elevationFolder) Then
            System.IO.Directory.CreateDirectory(elevationFolder)
        End If

        ' Create folder named "ZONE " &amp;amp; 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 " &amp;amp; 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 = "\/:*?""&amp;lt;&amp;gt;|"

        ' 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 &amp;lt;&amp;gt; "" Then
            MsgBox("The following occurrences were skipped because files already exist:" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; 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: " &amp;amp; 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 &amp;amp; oExt)

        ' Check if file already exists in the folder
        If System.IO.File.Exists(oName) Then
            ' Append to skipped occurrences string
            skippedOccurrences = skippedOccurrences &amp;amp; "- " &amp;amp; oOcc.Name &amp;amp; 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 " &amp;amp; oOcc.Name &amp;amp; ": " &amp;amp; ex.Message, vbExclamation, "Error")
    End Try
End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jul 2024 17:28:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/icopy-adaptivity-lost-during-component-replace/m-p/12903369#M8116</guid>
      <dc:creator>HogueOne</dc:creator>
      <dc:date>2024-07-17T17:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: iCopy Adaptivity Lost During Component Replace</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/icopy-adaptivity-lost-during-component-replace/m-p/12906091#M8117</link>
      <description>&lt;P&gt;Try ".replace2" it has a "[Keep Adaptivity]" option&lt;BR /&gt;&lt;A href="https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=ComponentOccurrence_Replace2" target="_blank"&gt;https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=ComponentOccurrence_Replace2&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2024 17:46:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/icopy-adaptivity-lost-during-component-replace/m-p/12906091#M8117</guid>
      <dc:creator>daltonNYAW9</dc:creator>
      <dc:date>2024-07-18T17:46:13Z</dc:date>
    </item>
  </channel>
</rss>

