Replace Derived Part Model Reference - Convert from iLogic to VB.NET
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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!
Link copied