Replace Derived Part Model Reference - Convert from iLogic to VB.NET

ngnam1988
Advocate
Advocate

Replace Derived Part Model Reference - Convert from iLogic to VB.NET

ngnam1988
Advocate
Advocate

Dears,

I'm trying convert iLogic Code to VB.NET as this post:
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/derived-part-replace-model-reference... 
https://forums.autodesk.com/autodesk/attachments/autodesk/78/469742/1/BasePartReplacer.iLogicVb.txt 
And my working code:

        Private Sub TestingButton_OnExecute(Context As NameValueMap) Handles TestingButton.OnExecute
            TopDOC = _InventorApp.Document
            Replace()
        End Sub
        Private TopDOC As Document
        Public Sub Replace()
            If (Not _InventorApp.ComponentDefinition.RepresentationsManager.LevelofDetailRepresentations("Master").Activate()) Then Return
            Dim DOCtoReplace As Document = FindDocToReplace()
            If (DOCtoReplace Is Nothing) Then Return
            Dim ReplacementFileName As String = SelectReplacementFilename(DOCtoReplace.DisplayName)
            If (String.IsNullOrEmpty(ReplacementFileName)) Then Return
            If (String.Equals(DOCtoReplace.FullFileName, ReplacementFileName, StringComparison.OrdinalIgnoreCase)) Then Return
            Dim ReplacementPart As Document = _InventorApp.Documents.Open(ReplacementFileName, False)
            Dim DoReplace As Boolean = True
            If (ReplacementPart.InternalName <> DOCtoReplace.InternalName) Then
                MessageBox.Show("THE REPLACEMENT PART (" & ReplacementPart.DisplayName & ") CANNOT BE USED.", "BASE PART REPLACER", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                DoReplace = False
            End If
            ReplacementPart.ReleaseReference()
            If (Not DoReplace) Then Return
            Dim FilenameToReplace As String = DOCtoReplace.FullFileName
            ReplaceReferences(TopDOC, FilenameToReplace, ReplacementFileName)
            TopDOC.Update()
        End Sub
        Function FindDocToReplace() As Document
            Dim BasePartList As New List(Of Document)
            If (TopDOC.DocumentType = DocumentTypeEnum.kPartDocumentObject) Then
                AddBaseParts(BasePartList, TopDOC)
            Else
                For Each RefDoc As Inventor.Document In TopDOC.AllReferencedDocuments
                    If (RefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject) Then
                        AddBaseParts(BasePartList, RefDoc)
                    End If
                Next
            End If
            If (BasePartList.Count = 0) Then
                MessageBox.Show("NO BASE PARTS WERE FOUND IN THE DOCUMENT: " & TopDOC.DisplayName, "BASE PART REPLACER")
            ElseIf (BasePartList.Count = 1) Then
                Return BasePartList(0)
            Else
                Dim PartNameList As New List(Of String)
                For Each baseDoc As Document In BasePartList
                    PartNameList.Add(baseDoc.DisplayName)
                Next
                'Dim SelectedName As String = InputListBox("SELECT THE PART TO REPLACE", PartNameList, PartNameList(0), "REPLACE PART", "PARTS").ToString()
                Dim selectedName As String = InputBox("SELECT THE PART TO REPLACE", "PartNameList", "PartNameList(0)", , )
                Dim SelectedIndex As Integer = PartNameList.IndexOf(SelectedName)
                Return BasePartList(SelectedIndex)
            End If
            Return Nothing
        End Function
        Sub AddBaseParts(ByVal BasePartList As List(Of Document), ByVal DOC As Document)
            For Each RefDoc As Document In DOC.ReferencedDocuments
                If (RefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso Not IsiPartMember(RefDoc)) Then
                    If (Not BasePartList.Contains(RefDoc)) Then
                        BasePartList.Add(RefDoc)
                    End If
                End If
            Next
        End Sub

        Function IsiPartMember(ByVal DOC As Document) As Boolean
            If (DOC.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Return False
            Dim PartDoc As PartDocument = DirectCast(DOC, PartDocument)
            Return PartDoc.ComponentDefinition.IsiPartMember
        End Function

        Function SelectReplacementFilename(ByVal FilenameToReplace As String) As String
            Dim oFileDlg As Inventor.FileDialog = Nothing
            _InventorApp.CreateFileDialog(oFileDlg)
            oFileDlg.Filter = "Part Files (*.ipt)|*.ipt"
            oFileDlg.DialogTitle = "Replace " & FilenameToReplace
            oFileDlg.InitialDirectory = _InventorApp.Path
            oFileDlg.CancelError = False
            Try
                oFileDlg.ShowOpen()
                Return oFileDlg.FileName
            Catch
            End Try
            Return String.Empty
        End Function

        Sub ReplaceReferences(ByVal DOC As Document, ByVal FilenameToReplace As String, ByVal ReplacementFileName As String)
            ReplaceReferencesInOneDoc(DOC, FilenameToReplace, ReplacementFileName)
            For Each SubDoc As Document In DOC.AllReferencedDocuments
                If (String.Equals(SubDoc.FullFileName, FilenameToReplace, StringComparison.OrdinalIgnoreCase) OrElse String.Equals(SubDoc.FullFileName, ReplacementFileName, StringComparison.OrdinalIgnoreCase)) Then
                    Continue For
                End If
                ReplaceReferencesInOneDoc(SubDoc, FilenameToReplace, ReplacementFileName)
            Next
        End Sub

Could you please help me correct my code. Thanks! 

0 Likes
Reply
331 Views
4 Replies
Replies (4)

ngnam1988
Advocate
Advocate

Dears,
When I convert in VS, I got 2 errors:

ngnam1988_0-1689426992498.pngngnam1988_1-1689427009042.png

0 Likes

A.Acheson
Mentor
Mentor

Hi @ngnam1988 

 

Your missing the sub routine  "ReplaceReferencesInOneDoc". So your calling a sub routine that does not exist. This is why the error message statement "ReplaceReferencesInOneDoc is not declared is shown. So you need to either delete that sub routine  call or create the sub routine. 

Sub ReplaceReferencesInOneDoc(DOC, FilenameToReplace, ReplacementFileName)

    'Need some code.
End SUb

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan

ngnam1988
Advocate
Advocate

Thanks @A.Acheson ,
I just add them to working code, but it can't run in correct way - with out error report. Could you please help me? Thanks,

        Private Sub TestingButton_OnExecute(Context As NameValueMap) Handles TestingButton.OnExecute
            TopDOC = _InventorApp.Document
            Replace()
        End Sub
        Public TopDOC As Document
        Public Sub Replace()
            If (Not LevelOfDetailIsMaster()) Then Return
            Dim DOCtoReplace As Document = FindDocToReplace()
            If (DOCtoReplace Is Nothing) Then Return
            Dim ReplacementFileName As String = SelectReplacementFilename(DOCtoReplace.DisplayName)
            If (String.IsNullOrEmpty(ReplacementFileName)) Then Return
            If (String.Equals(DOCtoReplace.FullFileName, ReplacementFileName, StringComparison.OrdinalIgnoreCase)) Then Return
            Dim ReplacementPart As Document = _InventorApp.Documents.Open(ReplacementFileName, False)
            Dim DoReplace As Boolean = True
            If (ReplacementPart.InternalName <> DOCtoReplace.InternalName) Then
                MessageBox.Show("THE REPLACEMENT PART (" & ReplacementPart.DisplayName & ") CANNOT BE USED.", "BASE PART REPLACER", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                DoReplace = False
            End If
            ReplacementPart.ReleaseReference()
            If (Not DoReplace) Then Return
            Dim FilenameToReplace As String = DOCtoReplace.FullFileName
            ReplaceReferences(TopDOC, FilenameToReplace, ReplacementFileName)
            TopDOC.Update()
        End Sub
        Function FindDocToReplace() As Document
            Dim BasePartList As New List(Of Document)
            If (TopDOC.DocumentType = DocumentTypeEnum.kPartDocumentObject) Then
                AddBaseParts(BasePartList, TopDOC)
            Else
                For Each RefDoc As Inventor.Document In TopDOC.AllReferencedDocuments
                    If (RefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject) Then
                        AddBaseParts(BasePartList, RefDoc)
                    End If
                Next
            End If
            If (BasePartList.Count = 0) Then
                MessageBox.Show("NO BASE PARTS WERE FOUND IN THE DOCUMENT: " & TopDOC.DisplayName, "BASE PART REPLACER")
            ElseIf (BasePartList.Count = 1) Then
                Return BasePartList(0)
            Else
                Dim PartNameList As New List(Of String)
                For Each baseDoc As Document In BasePartList
                    PartNameList.Add(baseDoc.DisplayName)
                Next
                Dim SelectedName As String = InputListBox("SELECT THE PART TO REPLACE", PartNameList, PartNameList(0), "REPLACE PART", "PARTS").ToString()
                'Dim selectedName As String = InputBox("SELECT THE PART TO REPLACE", "PartNameList", "PartNameList(0)", , )
                Dim SelectedIndex As Integer = PartNameList.IndexOf(SelectedName)
                Return BasePartList(SelectedIndex)
            End If
            Return Nothing
        End Function
        Sub AddBaseParts(ByVal BasePartList As List(Of Document), ByVal DOC As Document)
            For Each RefDoc As Document In DOC.ReferencedDocuments
                If (RefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso Not IsiPartMember(RefDoc)) Then
                    If (Not BasePartList.Contains(RefDoc)) Then
                        BasePartList.Add(RefDoc)
                    End If
                End If
            Next
        End Sub

        Function IsiPartMember(ByVal DOC As Document) As Boolean
            If (DOC.DocumentType <> DocumentTypeEnum.kPartDocumentObject) Then Return False
            Dim PartDoc As PartDocument = DirectCast(DOC, PartDocument)
            Return PartDoc.ComponentDefinition.IsiPartMember
        End Function

        Function SelectReplacementFilename(ByVal FilenameToReplace As String) As String
            Dim oFileDlg As Inventor.FileDialog = Nothing
            _InventorApp.CreateFileDialog(oFileDlg)
            oFileDlg.Filter = "Part Files (*.ipt)|*.ipt"
            oFileDlg.DialogTitle = "Replace " & FilenameToReplace
            oFileDlg.InitialDirectory = _InventorApp.Path
            oFileDlg.CancelError = False
            Try
                oFileDlg.ShowOpen()
                Return oFileDlg.FileName
            Catch
            End Try
            Return String.Empty
        End Function
        Sub ReplaceReferences(ByVal DOC As Document, ByVal FilenameToReplace As String, ByVal ReplacementFileName As String)
            ReplaceReferencesInOneDoc(DOC, FilenameToReplace, ReplacementFileName)
            For Each SubDoc As Document In DOC.AllReferencedDocuments
                If (String.Equals(SubDoc.FullFileName, FilenameToReplace, StringComparison.OrdinalIgnoreCase) OrElse String.Equals(SubDoc.FullFileName, ReplacementFileName, StringComparison.OrdinalIgnoreCase)) Then
                    Continue For
                End If
                ReplaceReferencesInOneDoc(SubDoc, FilenameToReplace, ReplacementFileName)
            Next
        End Sub
        Sub ReplaceReferencesInOneDoc(ByVal doc As Document, ByVal fileNameToReplace As String, ByVal replacementFileName As String)
            For Each docDesc As DocumentDescriptor In doc.ReferencedDocumentDescriptors
                Dim desc As FileDescriptor = docDesc.ReferencedFileDescriptor
                If (desc.ReferenceMissing) Then Continue For
                Console.WriteLine("Referenced RelativeFileName = " & desc.RelativeFileName)
                Trace.WriteLine("Referenced RelativeFileName = " & desc.RelativeFileName)
                If (String.Equals(desc.FullFileName, fileNameToReplace, StringComparison.OrdinalIgnoreCase)) Then
                    desc.ReplaceReference(replacementFileName)
                    Exit For
                End If
            Next
        End Sub
        Function LevelOfDetailIsMaster() As Boolean
            Dim assemDoc As AssemblyDocument = TryCast(TopDOC, AssemblyDocument)
            If (assemDoc Is Nothing) Then Return True
            Dim repMgr As RepresentationsManager = assemDoc.ComponentDefinition.RepresentationsManager
            Dim lodType As LevelOfDetailEnum = repMgr.ActiveLevelOfDetailRepresentation.LevelOfDetail
            If (lodType <> LevelOfDetailEnum.kMasterLevelOfDetail) Then
                MessageBox.Show("This rule can only be run in the Master Level of Detail.", "Base Part Replacer")
                Return False
            End If
            Return True
        End Function
0 Likes

A.Acheson
Mentor
Mentor

Hi @ngnam1988 

 

From the code supplied you are missing some key ingredients. 

Imports Inventor
Imports System.Runtime.InteropServices
Dim _InventorApp As Inventor.Application = Marshal.GetActiveObject("Inventor.Application")
Dim TopDOC As Document = _InventorApp.ActiveDocument

See this video for setting up and accessing Inventor Application. 

https://www.youtube.com/watch?v=NrNXxYCwAfc

 

Here is the error list I received from the code provided. Inputlist box is not allowed in Visual studio so you will either need to create your own dialog or alternatively just comment during testing. 

 

AAcheson_0-1689530374756.png

 

Are you trying to create an application or will this be run as an external rule in inventor? I do not have visual studio and inventor together so I cannot test the entire code. Maybe other forum users can assist here.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan