Inventor Ilogic Add part names to iproperties

Inventor Ilogic Add part names to iproperties

Oguzhan_Sezer
Advisor Advisor
621 Views
9 Replies
Message 1 of 10

Inventor Ilogic Add part names to iproperties

Oguzhan_Sezer
Advisor
Advisor

Hello,

I want to add their own part names and numbers to all the parts in the assembly as a custom property for each part separately. How can I do it? 

 

I got this code from forum for getting part quantity. I made some edits to it. In the last part, when I want to print the part names, it adds the name of the assembly to the parts' iproperties (because of ThisDoc.FileName). However, I wanted to add its own names for each piece.

 

Sub Main ()
    'Check to see if this document is an assembly.
    oDoc = ThisApplication.ActiveDocument
        If oDoc.DocumentType <> kAssemblyDocumentObject Then
        MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "iLogic")
        Return
        End If
  '  If iProperties.Value("Project", "Project")=""
  '      oProj = InputBox("Change Project Name", "Project Name", iProperties.Value("Project", "Project"))
  '      iProperties.Value("Project", "Project")=oProj
  '  End If

'get the project number of this assembly and use it to build the name of a custom property, i.e. "4100_QTY"
oBOMQTY = CStr(iProperties.Value("Project", "Project")) & "Parça Numarası yazdırılacak onaylıyor musunuz?"

    oQ=MessageBox.Show(oBOMQTY,"ilogic",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question)
        If oQ=vbYes
        oDone() 'next Sub Routine
        ElseIf oQ=vbNo
        'Call oProject 'Option to change the project Number for the GA
        ElseIf oQ=vbCancel
        MessageBox.Show("İptal Edildi", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        Return
    End If
End Sub

Sub oDone
oCompDef = ThisDoc.Document.ComponentDefinition ' Don't need this, but I left it in for reference when looking for code snippets.
openDoc = ThisDoc.Document
oBOMQTY = CStr(iProperties.Value("Project", "Project")) & "_QTY"
'Open the document that inventor has in focus right now
    For Each docFile In openDoc.AllReferencedDocuments

    'FNamePos is getting the number of spaces that it takes to get to the
    'very last back slash in the full file name of our document.        
        FNamePos = InStrRev(docFile.FullFileName, "\", -1)       
    
    'We can then take that number (position) and use it to cut off all of the
    'file path that we don't need. In this case, it's the front of the path
    'that we're getting rid of, leaving us with just the file name.
        docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
       
        'Let's make sure that we can even change this part. If we can, then we'll continue.
        If docFile.IsModifiable = True Then
           
            'Because you can only grab the occurrences from an assembly document
            'we need to fill up that empty AssemblyDocument container with an object
            'that we know is definitely an assembly document. Because that was one
            'of the first checks we made up there on this open document, we can safely
            'declare that assemblyDoc (Which is an AssemblyDocument) is equal to
            ' openDoc (Which is just a regular old Document)            
            assemblyDoc = openDoc
           
            'While we're at it, let's go on and define the empty ComponentDefinition container
            '(we named ours assemblyDef) with some sort of content.
            assemblyDef = assemblyDoc.ComponentDefinition
           
            'Now we need to collect every instance of the document against
            'our open assembly and put them all inside of partQty.
            partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
           
                'Now that we have our collection of instances, we need to populate
                'our target iproperty with the total amount.
               
                'Instead of just throwing that amount in there, let's make sure that
                'the value inside of the target iProperty isn't already equal to our count.
                'If it is equal, then we don't have to change anything (which saves us time!),
                'but if it isn't equal then we will need to change it.
               
                'The Try statement is here because of the next if statement ---
              
                'If we just compare the two values, there is a small chance that our target
                'iProperty is already set to something that is NOT a number, which would create
                'an error (as you can't compare numbers against things that aren't numbers).
                'So, we TRY to complete that if statement, and if there is an error (The CATCH)
                'we just force that target to equal our part qty total.
                Try
					'Iterate through all Of the occurrences
                        If partQty.Count <>  iProperties.Value(docFName, "Custom", "Toplam Adet") Then               
                        iProperties.Value(docFName, "Custom", "Toplam Adet") = partQty.Count
                        '
                       End If                     
					    If ThisDoc.FileName(False) <>  iProperties.Value(docFName, "Custom", "Part Number") Then               
                        iProperties.Value(docFName, "Custom", "Part Number") = ThisDoc.FileName(False) 'without extension
                        '
                       End If                   
                    Catch
                    iProperties.Value(docFName, "Custom", "Toplam Adet") = partQty.Count
					iProperties.Value(docFName, "Custom", "Part Number") = ThisDoc.FileName(False) 'without extension
                End Try
        End If
    Next
'29/01/2016
'I Added the following line to add the value to the GA
' As the lack thereof was causing issues in the detailing environment.
            Try
                oASSYParam = iProperties.Value("Custom", "Toplam Adet")
				oASSYParam= iProperties.Value("Custom","Toplam Adet")
              Catch
                'iProperties.Value("Custom", "Toplam Adet") = "1"
				iProperties.Value("Custom","Part Number") = "1"
                    End Try
iLogicVb.UpdateWhenDone = True
MessageBox.Show("Başarıyla Tamamlandı", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Sub

'Sub oProject
'    oProj = InputBox("Change Project Name", "Project Name", iProperties.Value("Project", "Project"))
'    iProperties.Value("Project", "Project")=oProj
'    Main () 'Loop Back
'End Sub

 

 

Oğuzhan SEZER
Manufacturing Engineer



EESignature


0 Likes
Accepted solutions (1)
622 Views
9 Replies
Replies (9)
Message 2 of 10

TechInventor20
Advocate
Advocate

Dim oOcc as ComponentOccurrence
Dim oDoc as Document

For Each oOcc in Occurrences
oDoc = oOcc.Definition.Document

Dim oBrowserNode As String
oBrowserNode = oOcc.Name
iProperties.Value(oBrowserNode , "Custom", "Ipropvalue") = oOcc.Name
next

0 Likes
Message 3 of 10

TechInventor20
Advocate
Advocate

After inspecting your code, I think you should change this row:

  iProperties.Value(docFName, "Custom", "Part Number") = ThisDoc.FileName(False) 'without extension

Into this:

  iProperties.Value(docFName, "Custom", "Part Number") = docFile.FileName(False) 'without extension
0 Likes
Message 4 of 10

Oguzhan_Sezer
Advisor
Advisor

I tried the change in your second comment but got an error like this.

 

ogsezer_0-1662983774591.png

 

Oğuzhan SEZER
Manufacturing Engineer



EESignature


0 Likes
Message 5 of 10

TechInventor20
Advocate
Advocate

Try this

 iProperties.Value(docFName, "Custom", "Part Number") = docFile.Name 'With ":1"​

or this

PartFilename = docFile.Name 
splitter = PartFilename.split(":")
iProperties.Value(docFName, "Custom", "Part Number") = Splitter(0) 'Without ":1"
0 Likes
Message 6 of 10

Oguzhan_Sezer
Advisor
Advisor
I keep getting the same error. Maybe I'm not typing in the right place. Can you send the edited version?
Oğuzhan SEZER
Manufacturing Engineer



EESignature


0 Likes
Message 7 of 10

TechInventor20
Advocate
Advocate
Sub Main ()
    'Check to see if this document is an assembly.
    oDoc = ThisApplication.ActiveDocument
        If oDoc.DocumentType <> kAssemblyDocumentObject Then
        MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "iLogic")
        Return
        End If
  '  If iProperties.Value("Project", "Project")=""
  '      oProj = InputBox("Change Project Name", "Project Name", iProperties.Value("Project", "Project"))
  '      iProperties.Value("Project", "Project")=oProj
  '  End If

'get the project number of this assembly and use it to build the name of a custom property, i.e. "4100_QTY"
oBOMQTY = CStr(iProperties.Value("Project", "Project")) & "Parça Numarası yazdırılacak onaylıyor musunuz?"

    oQ=MessageBox.Show(oBOMQTY,"ilogic",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question)
        If oQ=vbYes
        oDone() 'next Sub Routine
        ElseIf oQ=vbNo
        'Call oProject 'Option to change the project Number for the GA
        ElseIf oQ=vbCancel
        MessageBox.Show("İptal Edildi", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        Return
    End If
End Sub

Sub oDone
oCompDef = ThisDoc.Document.ComponentDefinition ' Don't need this, but I left it in for reference when looking for code snippets.
openDoc = ThisDoc.Document
oBOMQTY = CStr(iProperties.Value("Project", "Project")) & "_QTY"
'Open the document that inventor has in focus right now
    For Each docFile In openDoc.AllReferencedDocuments

    'FNamePos is getting the number of spaces that it takes to get to the
    'very last back slash in the full file name of our document.        
        FNamePos = InStrRev(docFile.FullFileName, "\", -1)       
    
    'We can then take that number (position) and use it to cut off all of the
    'file path that we don't need. In this case, it's the front of the path
    'that we're getting rid of, leaving us with just the file name.
        docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
       
        'Let's make sure that we can even change this part. If we can, then we'll continue.
        If docFile.IsModifiable = True Then
           
            'Because you can only grab the occurrences from an assembly document
            'we need to fill up that empty AssemblyDocument container with an object
            'that we know is definitely an assembly document. Because that was one
            'of the first checks we made up there on this open document, we can safely
            'declare that assemblyDoc (Which is an AssemblyDocument) is equal to
            ' openDoc (Which is just a regular old Document)            
            assemblyDoc = openDoc
           
            'While we're at it, let's go on and define the empty ComponentDefinition container
            '(we named ours assemblyDef) with some sort of content.
            assemblyDef = assemblyDoc.ComponentDefinition
           
            'Now we need to collect every instance of the document against
            'our open assembly and put them all inside of partQty.
            partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
           
                'Now that we have our collection of instances, we need to populate
                'our target iproperty with the total amount.
               
                'Instead of just throwing that amount in there, let's make sure that
                'the value inside of the target iProperty isn't already equal to our count.
                'If it is equal, then we don't have to change anything (which saves us time!),
                'but if it isn't equal then we will need to change it.
               
                'The Try statement is here because of the next if statement ---
              
                'If we just compare the two values, there is a small chance that our target
                'iProperty is already set to something that is NOT a number, which would create
                'an error (as you can't compare numbers against things that aren't numbers).
                'So, we TRY to complete that if statement, and if there is an error (The CATCH)
                'we just force that target to equal our part qty total.
                Try
					'Iterate through all Of the occurrences
                        If partQty.Count <>  iProperties.Value(docFName, "Custom", "Toplam Adet") Then               
                        iProperties.Value(docFName, "Custom", "Toplam Adet") = partQty.Count
                        '
                       End If                     
					    If ThisDoc.FileName(False) <>  iProperties.Value(docFName, "Custom", "Part Number") Then               
                        iProperties.Value(docFName, "Custom", "Part Number") =  docFile.Name 'With ":1"​
                        '
                       End If                   
                    Catch
                    iProperties.Value(docFName, "Custom", "Toplam Adet") = partQty.Count
					iProperties.Value(docFName, "Custom", "Part Number") =  docFile.Name 'With ":1"​
                End Try
        End If
    Next
'29/01/2016
'I Added the following line to add the value to the GA
' As the lack thereof was causing issues in the detailing environment.
            Try
                oASSYParam = iProperties.Value("Custom", "Toplam Adet")
				oASSYParam= iProperties.Value("Custom","Toplam Adet")
              Catch
                'iProperties.Value("Custom", "Toplam Adet") = "1"
				iProperties.Value("Custom","Part Number") = "1"
                    End Try
iLogicVb.UpdateWhenDone = True
MessageBox.Show("Başarıyla Tamamlandı", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Sub

'Sub oProject
'    oProj = InputBox("Change Project Name", "Project Name", iProperties.Value("Project", "Project"))
'    iProperties.Value("Project", "Project")=oProj
'    Main () 'Loop Back
'End Sub
Message 8 of 10

TechInventor20
Advocate
Advocate
Accepted solution

I see you used referenced documents instead of occurence so it has to be ".displayname"

I changed row 84 and 89. Hope this works.

 

Sub Main ()
    'Check to see if this document is an assembly.
    oDoc = ThisApplication.ActiveDocument
        If oDoc.DocumentType <> kAssemblyDocumentObject Then
        MessageBox.Show("This rule can only be run in an Assembly file - exiting rule...", "iLogic")
        Return
        End If
  '  If iProperties.Value("Project", "Project")=""
  '      oProj = InputBox("Change Project Name", "Project Name", iProperties.Value("Project", "Project"))
  '      iProperties.Value("Project", "Project")=oProj
  '  End If

'get the project number of this assembly and use it to build the name of a custom property, i.e. "4100_QTY"
oBOMQTY = CStr(iProperties.Value("Project", "Project")) & "Parça Numarası yazdırılacak onaylıyor musunuz?"

    oQ=MessageBox.Show(oBOMQTY,"ilogic",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question)
        If oQ=vbYes
        oDone() 'next Sub Routine
        ElseIf oQ=vbNo
        'Call oProject 'Option to change the project Number for the GA
        ElseIf oQ=vbCancel
        MessageBox.Show("İptal Edildi", "iLogic", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
        Return
    End If
End Sub

Sub oDone
oCompDef = ThisDoc.Document.ComponentDefinition ' Don't need this, but I left it in for reference when looking for code snippets.
openDoc = ThisDoc.Document
oBOMQTY = CStr(iProperties.Value("Project", "Project")) & "_QTY"
'Open the document that inventor has in focus right now
    For Each docFile In openDoc.AllReferencedDocuments

    'FNamePos is getting the number of spaces that it takes to get to the
    'very last back slash in the full file name of our document.        
        FNamePos = InStrRev(docFile.FullFileName, "\", -1)       
    
    'We can then take that number (position) and use it to cut off all of the
    'file path that we don't need. In this case, it's the front of the path
    'that we're getting rid of, leaving us with just the file name.
        docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
       
        'Let's make sure that we can even change this part. If we can, then we'll continue.
        If docFile.IsModifiable = True Then
           
            'Because you can only grab the occurrences from an assembly document
            'we need to fill up that empty AssemblyDocument container with an object
            'that we know is definitely an assembly document. Because that was one
            'of the first checks we made up there on this open document, we can safely
            'declare that assemblyDoc (Which is an AssemblyDocument) is equal to
            ' openDoc (Which is just a regular old Document)            
            assemblyDoc = openDoc
           
            'While we're at it, let's go on and define the empty ComponentDefinition container
            '(we named ours assemblyDef) with some sort of content.
            assemblyDef = assemblyDoc.ComponentDefinition
           
            'Now we need to collect every instance of the document against
            'our open assembly and put them all inside of partQty.
            partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
           
                'Now that we have our collection of instances, we need to populate
                'our target iproperty with the total amount.
               
                'Instead of just throwing that amount in there, let's make sure that
                'the value inside of the target iProperty isn't already equal to our count.
                'If it is equal, then we don't have to change anything (which saves us time!),
                'but if it isn't equal then we will need to change it.
               
                'The Try statement is here because of the next if statement ---
              
                'If we just compare the two values, there is a small chance that our target
                'iProperty is already set to something that is NOT a number, which would create
                'an error (as you can't compare numbers against things that aren't numbers).
                'So, we TRY to complete that if statement, and if there is an error (The CATCH)
                'we just force that target to equal our part qty total.
                Try
					'Iterate through all Of the occurrences
                        If partQty.Count <>  iProperties.Value(docFName, "Custom", "Toplam Adet") Then               
                        iProperties.Value(docFName, "Custom", "Toplam Adet") = partQty.Count
                        '
                       End If                     
					    If ThisDoc.FileName(False) <>  iProperties.Value(docFName, "Custom", "Part Number") Then               
                        iProperties.Value(docFName, "Custom", "Part Number") =  docFile.displayName 'With ":1"​
                        '
                       End If                   
                    Catch
                    iProperties.Value(docFName, "Custom", "Toplam Adet") = partQty.Count
					iProperties.Value(docFName, "Custom", "Part Number") =  docFile.displayName 'With ":1"​
                End Try
        End If
    Next
'29/01/2016
'I Added the following line to add the value to the GA
' As the lack thereof was causing issues in the detailing environment.
            Try
                oASSYParam = iProperties.Value("Custom", "Toplam Adet")
				oASSYParam= iProperties.Value("Custom","Toplam Adet")
              Catch
                'iProperties.Value("Custom", "Toplam Adet") = "1"
				iProperties.Value("Custom","Part Number") = "1"
                    End Try
iLogicVb.UpdateWhenDone = True
MessageBox.Show("Başarıyla Tamamlandı", "Completed", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End Sub

'Sub oProject
'    oProj = InputBox("Change Project Name", "Project Name", iProperties.Value("Project", "Project"))
'    iProperties.Value("Project", "Project")=oProj
'    Main () 'Loop Back
'End Sub

 

Message 9 of 10

Oguzhan_Sezer
Advisor
Advisor
It worked, thank you very much.
Oğuzhan SEZER
Manufacturing Engineer



EESignature


0 Likes
Message 10 of 10

TechInventor20
Advocate
Advocate

Finally, sorry that it took so long....

Next time I will look into it more discretely