C# drawing replace reference model

C# drawing replace reference model

hwu
Advocate Advocate
2,734 Views
21 Replies
Message 1 of 22

C# drawing replace reference model

hwu
Advocate
Advocate

I have drawing saved to different location, then need to replace its reference model (there is only one reference model). I am wondering if there are C# code to do this?

thanks,

HWU

 

0 Likes
Accepted solutions (1)
2,735 Views
21 Replies
Replies (21)
Message 2 of 22

yan.gauthier
Advocate
Advocate

in a drawing Document, you have access to the object DrawingDocument.File.ReferencedFileDescriptors which you have read/write access to it.

 

in VBA, I change my reference like this:

 

For Each oFileDescriptor In NewDrawingDocument.File.ReferencedFileDescriptors
    If oFileDescriptor.Fullfilename = DocToCopy.Fullfilename Then
        oFileDescriptor.ReplaceReference NewFilePath
    End If
Next oFileDescriptor
    
NewDrawingDocument.PropertySets.item("Design Tracking Properties").item("Part Number").Value = fso.getbasename(NewFile)

NewDrawingDocument.PropertySets.item("Design Tracking 
Properties").item("Creation Time").Value = Now()

NewDrawingDocument.DisplayName = fso.getbasename(NewFile)

 

 

0 Likes
Message 3 of 22

WCrihfield
Mentor
Mentor

In plain iLogic (which uses VB.NET) here is a single line of code to replace the referenced model of a drawing.

(This assumes a single model reference, not multiple.)

ThisDrawing.Document.Sheets(1).DrawingViews(1).ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference("C:\Temp\OtherFile.iam")

And here is an example that separates the variables out.

Dim oDoc As DrawingDocument = ThisDrawing.Document
Dim oNewModelFile As String = "C:\Temp\OtherFile.iam"
Dim oFD As FileDescriptor = oDoc.ReferencedDocumentDescriptors(1).ReferencedFileDescriptor
oFD.ReplaceReference(oNewModelFile)
oDoc.Update()

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 22

hwu
Advocate
Advocate

Thank you, Yan,

 

I cannot convert  your code to C#.

Can you please have it in C#?

 

Best Regards,

HWU

0 Likes
Message 5 of 22

WCrihfield
Mentor
Mentor

Here is a single line of iLogic (VB.NET) code to replace the model reference within a drawing.

(This assumes just one model reference within the drawing.)

 

ThisDrawing.Document.Sheets(1).DrawingViews(1).ReferencedDocumentDescriptor.ReferencedFileDescriptor.ReplaceReference("C:\Temp\OtherFile.iam")

 

And here is a version that breaks things down into more variables.

 

Dim oInvApp As Inventor.Application = GetObject(,"Inventor.Application")
Dim oDrawDoc As Inventor.DrawingDocument = oInvApp.ActiveDocument
Dim oNewModelFile As String = "C:\Temp\OtherFile.iam"
Dim oFD As Inventor.FileDescriptor = oDrawDoc.ReferencedDocumentDescriptors(1).ReferencedFileDescriptor
oFD.ReplaceReference(oNewModelFile)
oDrawDoc.Update()

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

 

Sorry.  When I originally posted, it stalled out and gave an error indicating it didn't go through, so I reposted, then found out the original went through.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 22

hwu
Advocate
Advocate

hwu_0-1603202929393.png

Thank you!

When I convert your code to C#, shows:

DrawingDocument.ReferencedDocumentDescriptors() cannot be used as method.

 

I have no idea how to fix this.

 

Best Regards,

HWU

 

 

0 Likes
Message 7 of 22

WCrihfield
Mentor
Mentor

Perhaps it just doesn't like the format.  Maybe it would work better if you replaced:

ReferencedDocumentDescriptors(1).ReferencedFileDescriptor

 with

ReferencedDocumentDescriptors.Item(1).ReferencedFileDescriptor

Or to break it down even further:

Dim oInvApp As Inventor.Application = GetObject(,"Inventor.Application")
Dim oDrawDoc As Inventor.DrawingDocument = oInvApp.ActiveDocument
Dim oNewModelFile As String = "C:\Temp\OtherFile.iam"
Dim oDDs As Inventor.DocumentDescriptorsEnumerator = oDrawDoc.ReferencedDocumentDescriptors
Dim oDD As Inventor.DocumentDescriptor = oDDs.Item(1)
Dim oFD As Inventor.FileDescriptor = oDD.ReferencedFileDescriptor
oFD.ReplaceReference(oNewModelFile)
oDrawDoc.Update()

Sorry, I'm not as familiar with C# as I am with VB.NET, but here's my attempt at the C# version of this code:

Inventor.Application oInvApp = GetObject(,"Inventor.Application");
Inventor.DrawingDocument oDrawDoc = oInvApp.ActiveDocument;
String oNewModelFile = "C:\Temp\OtherFile.iam";
Inventor.DocumentDescriptorsEnumerator oDDs = oDrawDoc.ReferencedDocumentDescriptors;
Inventor.DocumentDescriptor oDD = oDDs.Item(1);
Inventor.FileDescriptor oFD = oDD.ReferencedFileDescriptor;
oFD.ReplaceReference(oNewModelFile);
oDrawDoc.Update();

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 22

hwu
Advocate
Advocate

 

hwu_1-1603208173241.png

 

Thank you,

there is no "Item" for choosing after "oDDs".

Best Regards,

HWU

 

 

0 Likes
Message 9 of 22

AlexKorzun
Autodesk
Autodesk

You might use C# indexer, as : 

ReferencedDocumentDescriptors[1]

 

Thank you,




Alex Korzun
Inventor-Revit Interop / Inventor-Fusion Interop / Inventor ETO
Autodesk, Inc.

0 Likes
Message 10 of 22

yan.gauthier
Advocate
Advocate

he is how to handle IEnumerator in C#

 

IEnumerator fileEnumerator = drawingDocument.ReferencedFileDescriptors.GetEnumerator();
//Set index at position 0
fileEnumerator.Reset();

while (fileEnumerator.MoveNext())
{
    if (fileEnumerator.Current is FileDescriptor fileDescriptor)
    {                            
        fileDescriptor.ReplaceReference("FileString");
    }
}
0 Likes
Message 11 of 22

hwu
Advocate
Advocate

hwu_0-1603223136425.png

 

Thank all of you!

 

the code has no any problem, but it does not do the job.

 

Best Regards,

HWU

 

0 Likes
Message 12 of 22

yan.gauthier
Advocate
Advocate

if you debug your code, do you get inside the if statement ?

 

Also, I just remembered that FileDescriptor.ReplaceReference works only if files share Ancestry. So the new file has to be a copy.

0 Likes
Message 13 of 22

hwu
Advocate
Advocate

No. Not get inside of if statement.

0 Likes
Message 14 of 22

yan.gauthier
Advocate
Advocate

what is the object held by current ?

0 Likes
Message 15 of 22

hwu
Advocate
Advocate

Thank you, Yan,

 

Nothing happened. just stuck there.
Do you have any ideas?

Best Regards,

HWU

0 Likes
Message 16 of 22

yan.gauthier
Advocate
Advocate

Hi, I tested the code, the output of that is a ReferencedFileDescriptor, not a FileDescriptor. It seems to be different from what you can get in VB.

 

          IEnumerator fileEnumerator = drawDoc.ReferencedFileDescriptors.GetEnumerator();
                
                //Set index at position 0
                fileEnumerator.Reset();

                while (fileEnumerator.MoveNext())
                {
                    if (fileEnumerator.Current is ReferencedFileDescriptor referencedFileDescriptor)
                    {
                        //fileDescriptor.ReplaceReference("FileString");
                        referencedFileDescriptor.PutLogicalFileNameUsingFull("FileString");
                    }
                }

 

I would try this method to see if it works.

0 Likes
Message 17 of 22

hwu
Advocate
Advocate

Thank you, Yan,

 

HWU

0 Likes
Message 18 of 22

hwu
Advocate
Advocate

Hi Yan,

I just get a different way to meet what I need:

1. Create an iLogic,  triggered by iProperty, to replace the referenced model

hwu_0-1603370683011.png

2. Using C# to save the drawing to a different location with different name, set new drawing with new Part Number

hwu_1-1603370778261.png

Hopefully can find a direct way to do this.

 

Best Regards,

HWU

0 Likes
Message 19 of 22

hwu
Advocate
Advocate
Accepted solution

hwu_0-1607714486250.png

 

Our company IT guy creates above code for me and it works. In the past, the codes didn't work, I guess we didn't add "update()".

 

0 Likes
Message 20 of 22

talha.tufan523TD
Advocate
Advocate

Can you share code block of the Function called UpdateDrawingView? 

0 Likes