iLogic check for and delete all unresolved components

iLogic check for and delete all unresolved components

Anonymous
Not applicable
4,059 Views
9 Replies
Message 1 of 10

iLogic check for and delete all unresolved components

Anonymous
Not applicable

Hi Guys, 

 

I'm looking for some solution which allows me to check for all unresolved components within the assembly and delete them if there are any.

 

Is it possible to create the iLogic rule to do that?

 

Thanks in advance for help.

 

Best Regards

Tomek

0 Likes
Accepted solutions (1)
4,060 Views
9 Replies
Replies (9)
Message 2 of 10

AlexFielder
Advisor
Advisor

From this page: http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-0123304B-EC33-4430-B4F1-CBF225CB5B8F

comes this example iLogic Rule (Adapted slightly, because at the page above the code was VBA):

 

Public Sub Main()

  Dim oFile As File = ThisApplication.ActiveDocument.File
    
  Call ProcessReferences(oFile)
    
End Sub

Private Sub ProcessReferences ( ByVal oFile As File )
    
  Dim oFileDescriptor As FileDescriptor    
  For Each oFileDescriptor In oFile.ReferencedFileDescriptors        
       	
    Debug.Print oFileDescriptor.FullFileName
        
    If Not oFileDescriptor.ReferenceMissing Then
        
      ' Since the ReferenceMissing has returned False, the ReferencedFile will return a File
      ' Recurse unless this is a foreign file reference
      If Not oFileDescriptor.ReferencedFileType = kForeignFileType Then

        Call ProcessReferences(oFileDescriptor.ReferencedFile)           			 
      End If            
    End If        
  Next    
End Sub

In theory you could use the same approach with a simple modification, instead of just checking whether the "oFileDescriptor.ReferenceMissing", you could do this:

 

If oFileDescriptor.ReferencedDocument = nothing then
'delete oFileDescriptor
End If

Of course, I have to ask why you want to delete unresolved references at all? Since in doing so, it will likely mean you'll end up with broken Assemblies and referencing drawings. Is it a dataset you inherited/don't have the time/money to fix?

0 Likes
Message 3 of 10

frederic.vandenplas
Collaborator
Collaborator
Accepted solution

Hi, this is created in vba, switch the commented line to actually delete the occurence

Sub DeleteUnresolvedFiles()
        Dim oFile As Inventor.File
        Set oFile = ThisApplication.ActiveDocument.File
        
        Dim oDoc As AssemblyDocument
        Set oDoc = ThisApplication.ActiveDocument
        oDoc.Save
        
        Dim oDocDef As AssemblyComponentDefinition
        Set oDocDef = oDoc.ComponentDefinition
        
        Call ProcessReferences(oFile, oDoc, oDocDef)

End Sub
Private Sub ProcessReferences(ByVal oFile As Inventor.File, oDoc As AssemblyDocument, oDocDef As ComponentDefinition)

        Dim oFileDescriptor As FileDescriptor
        For Each oFileDescriptor In oFile.ReferencedFileDescriptors
            If Not oFileDescriptor.ReferenceMissing Then
                If Not oFileDescriptor.ReferencedFileType = FileTypeEnum.kForeignFileType Then
                    Call ProcessReferences(oFileDescriptor.ReferencedFile, oDoc, oDocDef)
                End If
            Else
            Dim oComp As ComponentOccurrence
            For Each oComp In oDocDef.Occurrences
            If oComp.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName = oFileDescriptor.FullFileName Then
            MsgBox (oFileDescriptor.FullFileName & vbCrLf & oFileDescriptor.RelativeFileName)
            'oComp.Delete
            End If
            Next oComp
            End If
                       
        Next
End Sub
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
Message 4 of 10

Anonymous
Not applicable

Hi Alex, 

 

The aim is I'm looking for the solution of bigger iLogic rule / problem. 

 

I've created the rule which is responsible for export of BOM to the excell file. 

 

But this is not only simple export. In fact we need the data to be ready for print as a template we use. 

 

So the rule sends BOM (row by row - missing content center parts or parts marked as "purchased") to the excell file 

and then starts the macro which is responsilbe for sorting data to looks as we would like to.

 

Because at the end I would like to equipped all Inventor user with the rule, I'd like to be sure we are able to avoid any error situation.

 

Right now the rule works quite fine but there are two problematic situations: 

 

1) When there are any missing parts (unresolved parts) within the assembly. Specialy when we share whole assemblies between users and some parts are missing during that process. 

 

2) When the view representation is not set up as main

 

Below You can see the complete rule right now i have: 

 

SyntaxEditor Code Snippet

Dim oDoc As Inventor.Document
oDoc = ThisApplication.ActiveDocument 

'At first we're chcecking if opened file is an assembly or single part file. For single part system is running other rule.
If oDoc.DocumentType <> kAssemblyDocumentObject Then
    
    If oDoc.DocumentType = kPartDocumentObject Then
    
    UserChoice0 = MessageBox.Show ( "Czy chcesz wygenerowa� Kart� Ci�cia dla pojedy�czej cz�ci?", "iLogic",MessageBoxButtons.YesNo)

        If UserChoice0 = vbNo Then
        Exit Sub
        Else

        iLogicVb.RunExternalRule("Karta Ci�cia Cz��")
        Exit Sub
        End If
        
    End If
    
End If    

'We're checking if the assembly file was already saved.
FilePath = ThisDoc.Path

    If FilePath = "" Then
    
    MessageBox.Show("Plik z�o�enia nie zosta� zapisany!" _
    & vbLf & " " _
    & vbLf & "Zapisz plik, nast�pnie uruchom regu�� ponownie.", "iLogic")
    
    Exit Sub
    End If

'Notification for user to be sure he would like to run the rule.
UserChoice2 = MessageBox.Show ( "Czy chcesz wygenerowa� Kart� Ci�cia?" _
    & vbLf & " " _
    & vbLf & "Upewnij si� �e wszystkie niezb�dne dane zosta�y uzupe�nione.", "iLogic",MessageBoxButtons.YesNo)

    If UserChoice2 = vbNo Then
    
    Exit Sub
    End If
'Input box to get user name
Opracowal = InputBox("Kart� Ci�cia Opracowa�:", "iLogic","Tomasz Pa�asz")

Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = oDoc.ComponentDefinition 

'Below a peace of code to check if the detail representation - main is set up. Currently it doesn't work I don't know why?
    'Dim oLOD As LevelOfDetailRepresentation 
    
    'Try
    'oLOD = oDoc.RepresentationsManager.LevelOfDetailRepresentations.Item("G��wna").Activate(True)
    'Catch
    'MessageBox.Show("Wyst�pi� problem z uaktywnieniem reprezentacji widoku - G��wna", "iLogic")
    'Exit Sub
    'End Try

'Below a peace of code to check if there are any unresolved parts within the assembly. If there are any it would be 'impossible to open BOM
Dim oRefFile As FileDescriptor

For Each oRefFile In oDoc.File.ReferencedFileDescriptors
    
    Dim AsmFilePath As String = oRefFile.FullFileName
    
    If Not System.IO.File.Exists(AsmFilePath) Then
    
    MessageBox.Show("Prawdopodobnie otwarte z�o�enie zawiera nierozwi�zane komponenty." _
    & vbLf & "Upewnij si� �e w z�o�eniu nie ma nierozwi�zanych komponent�w." _
    & vbLf & "Nast�pnie uruchom regu�� ponownie", "iLogic")
    Exit Sub
    
    Else
    End If

Next

'Now main part - which is responsible for BOM file export into an Excell file. After that the excell macro starts to sort the data within an excell file.

Dim oBOM As BOM

oBOM = oAsmCompDef.BOM

oBOM.PartsOnlyViewEnabled = True

Dim oBOMView As BOMView

oBOMView = oBOM.BOMViews.Item("Tylko Cz�ci")

oBOMView.Sort("Part Number", True)

oBOMView.Renumber(001)

Dim oBOMRow As BOMRow

'The rule checks for file path where an excell file is located.

Dim Filename As String = "C:\Users\Public\Documents\Autodesk\Inventor 2018\Templates\Karta Ci�cia Wz�r.xlsm"  '<---- TUTAJ ZMIE� �CIE�K� !

'If an excell file is already opened the rule will try to close it.

Try

    ObjXL = GetObject(Filename)

    ObjXL.Application.Quit
    
Catch
End Try

GoExcel.Open(Filename,"Arkusz2")

i=3

'The rule is counting total number of rows in the BOM
j=0

For Each oBOMRow In oBOMView.BOMRows

    If oBOMRow.BOMStructure <> 51973 Then
 
    Dim oCompDef As ComponentDefinition
    
    oCompDef = oBOMRow.ComponentDefinitions.Item(1)
    
    FullFileName  = oCompDef.Document.FullFileName
    
    Pozycja=InStrRev(FullFileName,"\", -1)
    
    NazwaModelu=Right(FullFileName,Len(FullFileName)-Pozycja)
 
    NumerCzesci=iProperties.Value(NazwaModelu, "Project", "Part Number")
    
    Opis=iProperties.Value(NazwaModelu, "Project", "Description")

'For every row in the BOM we're trying to catch custom iProperties vaulues
    Try
    Nazwa=iProperties.Value(NazwaModelu, "Custom", "Nazwa")
    Catch
    Nazwa=""
    End Try
    
    Try
    Rodzaj=iProperties.Material(NazwaModelu)
    Catch
    Rodzaj=""
    End Try
    
    Try
    Wymiar=iProperties.Value(NazwaModelu, "Custom", "Wymiar")
    Catch
    Wymiar=""
    End Try

    Try
    Uwagi=iProperties.Value(NazwaModelu, "Custom", "Uwagi")
    Catch
    Uwagi=""
    End Try
    
    Ilosc=oBOMRow.Totalquantity
    
    GoExcel.CellValue("B" & i)=Opis
    
    GoExcel.CellValue("C" & i)=NumerCzesci
    
    GoExcel.CellValue("D" & i)=Nazwa
    
    GoExcel.CellValue("E" & i)=Rodzaj
    
    GoExcel.CellValue("F" & i)=Wymiar
    
    GoExcel.CellValue("H" & i)=Uwagi
    
    GoExcel.CellValue("G" & i)=Ilosc

    i=i+1
    
    j=j+1
    
    End If
    
Next

'Based on total rows number the rule takes proper no. of excell sheet pages and sends it to the excell file.
'[
    Select Case j
    
    Case 1 To 12
    
    Iloscstr = 1
    
    Case 13 To 24
    
    Iloscstr = 2
    
    Case 25 To 36
    
    Iloscstr = 3
    
    Case 37 To 48
    
    Iloscstr = 4
    
    Case 49 To 60
    
    Iloscstr = 5
    
    End Select
']

    Try
    Artykul = iProperties.Value("Project", "Stock Number")
    Catch
    End Try    
    
    GoExcel.CellValue("A1")=iProperties.Value("Project", "Part Number")
    
    GoExcel.CellValue("B1")= Iloscstr
    
    GoExcel.CellValue("A2")= "Nr Art." & " " & Artykul

    GoExcel.CellValue("D1")=iProperties.Value("Project", "Description")
    
    GoExcel.CellValue("G1")= Opracowal

    GoExcel.DisplayAlerts = False
    
GoExcel.Save
GoExcel.Close

'Here the rule fires up the macro.

    Dim ExApp As Object
        
        ExApp = CreateObject("Excel.Application")
        
        Try 'Tutaj dodano fragment.
        
        For Each wb As Object In ExApp.workbooks
        
        If UCase(wb.fullname) = UCase(filename) Then
      
        'wb.Save
        
        wb.Close   
        Exit For
        End If
    Next
Catch
Finally
    excelApp = Nothing
End Try  'Tutaj ko�czy si� dodany fragment.
        
        ExApp.workbooks.Open (Filename)
        
        ExApp.Application.Run ("KartaCieciaWzor")
        
        ExApp.Visible = False
      
        ExApp = Nothing

Dim Numer As String

'The rule tries to check if the end excell file was properly created and saved.
Numer = "C:\Users\t.palasz\Desktop\Karty Ci�cia\" & iProperties.Value("Project", "Part Number") & ".xlsx" '<---- TUTAJ ZMIE� �CIEK�

If System.IO.File.Exists(Numer) Then
    
    UserChoice3 = MessageBox.Show ("Karta Ci�cia zosta�a wygenerowana pomy�lnie!" & vbLf & " " & vbLf & "Czy chcesz otworzy� Kart� Ci�cia?", "iLogic",MessageBoxButtons.YesNo)

        If UserChoice3 = vbYes Then

        ThisDoc.Launch(Numer)

        Exit Sub

        Else
        Exit Sub
        End If

    Else
    
    MessageBox.Show("Upss... Co poszo nie tak!", "iLogic")
    
    Exit Sub
    
    End If





 

 

 

Message 5 of 10

AlexFielder
Advisor
Advisor

Hi @Anonymous,

Thanks for the detailed explanation and code sample.

 

I can see now why you want to remove unresolved components. I agree that if it's going to cause the exported BOM process to fail then you'd be correct to remove them.

 

BUT (And it's a big but!), it would seem to me to be a better question/solution if you were able to address why you are seeing unresolved components.

 

Typical reasons for this are (and I'm sure someone else will add to this list if I miss anything):

  1. Assemblies not saved in consistent network paths i.e. some users have Z:\path\to\assembly whereas others may have \\servername\zdriveroot\path\to\assembly or a mixture of both.
  2. Users creating assemblies locally on C: and not sharing all files.
  3. Users creating assemblies locally and not using/being aware of "Pack & Go" to grab all referenced files.
  4. (and by no means least) users working on assemblies locally and not setting the correct project file.
  5. Inconsistent naming of part files (user A creates a new part called "widget", user then creates another part called "widget" using a different project file and (after sharing the assembly) when a different user opens the assembly they get prompted as to which they want to use and choose neither.

So really this boils down to:

 

a) Do you use Vault? All versions provide unique naming checks which can prevent users checking in multiple parts called "widget" or whatever. Vault also allows enforcement of a common project file for all users.

b) Are your team trained sufficiently that they're aware of the company's unique naming/path conventions?

c) Is any of ^ documented (for new/existing users)?

 

Since you're here and asking for help on this, there are steps you can take whilst you get a thru c above sorted out.

 

The first of which would be put together (or update) a document showing what is stored where on your network as well as the reason for it's importance in getting paths correct (point them to this post if necessary).

 

Second, I would think about changing your iLogic rule above such that when it finds an unresolved file to flag to the user the points I noted above, such that they can set the correct project file, check they received a complete pack & go and/or are using the correct file paths etc. etc.

 

Third, if you have users (even especially those who are self-taught!) who haven't been trained (or trained recently) I would heartily recommend you seek out the services of your local Autodesk reseller who will be more than happy to help.

 

Full disclosure: I work for a reseller here in the UK, so have trained Inventor users on both the main product, as well as iLogic in the past and the unresolved file problem typically stems from the issues I touched on above.

 

Apologies for the wall of text, but I hope it gives you food for thought.

 

Cheers,

 

Alex.

0 Likes
Message 6 of 10

AlexFielder
Advisor
Advisor

PS. I would recommend that if you insist on continuing to delete unresolved files, that you wrap the whole method in a transaction, thereby giving the user a chance to undo the whole thing, otherwise (if there are a lot) it'll be a painful process to sit and click "Undo" repeatedly:

 

Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(oAssemblyDocument, "Clean up unresolved files")

Try
    'Do Stuff 
    trans.Commit()
Catch 'any errors
    trans.Abort()
End Try

🙂

 

0 Likes
Message 7 of 10

Anonymous
Not applicable

Hi Alex, 

 

Thanks for fast reply and a bounch of information. 

In fact our team use the Vault aplication and we're working only on single project file. 

So problem with unresolved files occures really rarely. 

But because I would like to be sure my rule works 100% reliably before I'll redistribute it to all users I need to check if it would work with all our files. 

Right now it looks not bad but there are still two problems I've mantioned above. 

One with unresolved files (most of them are parts from content center library - If anybody uses different from project library). I'm wondering about using the snippet of code given by frederic but I'm not sure which part I can cut to adapt the code to iLogic syntax.

Second one is problem with switching the view representation into main. 

Now I have a piece of code wich should be able to do that but it's not working.

SyntaxEditor Code Snippet

Dim oLOD As LevelOfDetailRepresentation 
    
    Try
    oLOD = oDoc.RepresentationsManager.LevelOfDetailRepresentations.Item("G��wna").Activate(True)
    Catch
    MessageBox.Show("Wyst�pi� problem z uaktywnieniem reprezentacji widoku - G��wna", "iLogic")
    Exit Sub
    End Try
0 Likes
Message 8 of 10

Anonymous
Not applicable

Hi Frederic, 

 

I've made some small adjusments to adopt syntax to iLogic and now it works great. 

 

Thanks a lot for help. 

 

Best Regards

0 Likes
Message 9 of 10

Jason.Rugg
Collaborator
Collaborator

@Anonymous @frederic.vandenplas  I have the need to delete unresolved references as well, can you share the ilogic code you made to do this?

0 Likes
Message 10 of 10

Anonymous
Not applicable

Please try this snippet:

Dim oFile As Inventor.File
oFile = oAsmDoc.File

Dim oFileDescriptor As FileDescriptor

For Each oFileDescriptor In oFile.ReferencedFileDescriptors
If Not oFileDescriptor.ReferenceMissing Then
If Not oFileDescriptor.ReferencedFileType = FileTypeEnum.kForeignFileType Then
End If
Else
Dim oComp As ComponentOccurrence
For Each oComp In oAsmCompDef.Occurrences
If oComp.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName = oFileDescriptor.FullFileName Then
oComp.Delete

I hope You'll find it helpful 

 

Regards 

Tomek