Modify an existing Revit file reference in an assembly.

Modify an existing Revit file reference in an assembly.

NPieper
Participant Participant
802 Views
5 Replies
Message 1 of 6

Modify an existing Revit file reference in an assembly.

NPieper
Participant
Participant

I have an Inventor C# AddIn.
How can the Inventor API in an assembly (.iam)
change the file reference to a Revit file (.rvt)?
So I have an IAM in which a "D:\Old.rvt" is embedded.
I want to change the reference in the IAM to "D:\New.rvt".


"...Descriptor.ReplaceReference" does not work.


And with the following, the "Delete" method does not work:

  AssemblyDocument oAss = ...

  dynamic assemblyComponentDefinition = oAss.ComponentDefinition;
  dynamic importedComponents = assemblyComponentDefinition.ImportedComponents;

  dynamic importedComponentDefinition = importedComponents.CreateDefinition("D:\New.rvt");

  if(importedComponents.Count > 0)
 {
   dynamic ic = importedComponents.Item(1);
   ic.Delete(); // Method not implemented!
 }

 dynamic importedComp = importedComponents.Add(importedComponentDefinition);

 

0 Likes
Accepted solutions (1)
803 Views
5 Replies
Replies (5)
Message 2 of 6

ruthsteed
Autodesk
Autodesk

Unfortunately, there is no way to directly replace the .rvt referenced this way. Instead, you will need to replace the ComponentOccurrence that uses the reference. This means you need to import the new reference and then do the replacement. Here is a VBA sample that assumes you have already imported the replacement file:

 

Sub DoReplace(oAssy As AssemblyComponentDefinition, oldRvt As ImportedRVTComponent, newRvt As ImportedRVTComponent)
    Dim occs As ComponentOccurrences: Set occs = oAssy.Occurrences
    Dim oldOcc As ComponentOccurrence
    Dim newOcc As ComponentOccurrence
    Dim occ As ComponentOccurrence
    For Each occ In occs
        If occ.IsAssociativelyImported Then
            Dim impComp As ImportedRVTComponent: Set impComp = occ.ImportedComponent
            If impComp Is oldRvt Then
                Set oldOcc = occ
            ElseIf impComp Is newRvt Then
                Set newOcc = occ
            End If
            If Not oldOcc Is Nothing And Not newOcc Is Nothing Then
                oldOcc.Replace newOcc.Definition.Document.FullFileName, True
                Exit Sub
            End If
        End If
    Next occ
End Sub

 



Ruth Steed
Inventor Development Team
Autodesk, Inc.

0 Likes
Message 3 of 6

YuhanZhang
Autodesk
Autodesk

Can you provide non-confidential data and your code for us to reproduce the failure when replace a Revit reference? And which Inventor are you using?



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 4 of 6

NPieper
Participant
Participant

Inventor 2021.2.2 Build 289 (german) was used.

In the appendix are example files and an example cs code with questions.

 

0 Likes
Message 5 of 6

YuhanZhang
Autodesk
Autodesk

I can reproduce the issue here, I logged a wish story (INVGEN-51051) for this. Before we get this implemented for ComponentOccurrence.Replace/Replace2, can you try if below workaround works for you, open your assembly data, and run below VBA code(change the Revit file path before running it):

 

Sub UpdateRevitReference()
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oFD As FileDescriptor
    Set oFD = oDoc.File.ReferencedFileDescriptors(1)
    Debug.Print oFD.FullFileName
        
    Debug.Print oFD.FullFileName
    oFD.ReplaceReference "C:\Temp\IVTest\Revit\Railing_Samples_New.rvt"
    
    Dim oIC As ImportedComponent
    Set oIC = oDoc.ComponentDefinition.ImportedComponents(1)
        
    Dim oDef As ImportedRVTComponentDefinition
    Set oDef = oIC.Definition
    Debug.Print oDef.Imported3DView
    oDef.Imported3DView = "{3D}"
        
    
    Set oFD = oDoc.File.ReferencedFileDescriptors(1)
    Debug.Print oFD.FullFileName
End Sub

 



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

0 Likes
Message 6 of 6

NPieper
Participant
Participant
Accepted solution
Yes, this workaround works.
0 Likes