iLogic to Assign New Part Numbers and List Unchanged Files

iLogic to Assign New Part Numbers and List Unchanged Files

Jonathan.CharltonE35Y2
Contributor Contributor
881 Views
9 Replies
Message 1 of 10

iLogic to Assign New Part Numbers and List Unchanged Files

Jonathan.CharltonE35Y2
Contributor
Contributor

Hello,

I'm using Inventor Professional 2022 64-Bit, Build 492, Release: 2022.4.1.

I have a rule that I'm trying to use to assign new part numbers to all components in the Parts Only view of the BOM of an assembly.
It's been working well, but needs more error catching functionality.

I just need it to display a message box and show what parts it couldn't modify (Content Center component, Library Part, Suppressed part, etc.

I'm still a novice when it comes to iLogic, but I've been able to patch together this code after reading posts online.
Could someone explain how I could achieve what I'm wanting to do?

Public Sub Main()       

    Dim oAssemblyDocument As AssemblyDocument 
    oAssemblyDocument= ThisDoc.Document

    Dim oAssemblyComponentDefinition As AssemblyComponentDefinition 
    oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition

    Dim oAsmCustomPropSet As PropertySet = oAssemblyDocument.PropertySets.Item("Inventor User Defined Properties")
    Dim oAsmSkidNumber As String
    Dim oAsmSkidLetter As String

    Try
            'Get current custom prop value
            oAsmSkidNumber = iProperties.Value("Custom", "SKID NUMBER")
            oAsmSkidLetter = iProperties.Value("Custom", "SKID LETTER")
    Catch
            'Assume error means prop not found so create it
            oAsmCustomPropSet.Add("", "SKID NUMBER")
            oAsmCustomPropSet.Add("", "SKID LETTER")
            'Get User input
            oAsmSkidNumber = InputBox("Please enter a Skid Number", "iLogic", oAsmSkidNumber)
            oAsmSkidLetter = InputBox("Please enter a Skid Letter", "iLogic", oAsmSkidLetter)
            'Set custom prop value
            iProperties.Value("Custom", "SKID NUMBER") = oAsmSkidNumber
            iProperties.Value("Custom", "SKID LETTER") = oAsmSkidLetter
    End Try	

    Dim oBOM As BOM 
    oBOM = oAssemblyComponentDefinition.BOM

    oBOM.PartsOnlyViewEnabled = True
    Dim oBOMView As BOMView 
    oBOMView = oBOM.BOMViews.Item("Parts Only") 'or structured

    Try 
        oBOMView.Sort("Description", True,)
        oBOMView.Renumber(1, 1)
    Catch
        MessageBox.Show("The program has failed. There are no parts to renumber.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        Exit Sub
    End Try

    For Each oBOMRow As BOMRow In oBOMView.BOMRows
		
	        Try
	                
				Dim oComponentDefinition As ComponentDefinition
				oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
				MsgBox("anything")	
					
	            Dim oBOMItemNumber As String
	            oBOMItemNumber = oBOMRow.ItemNumber() 'this is item number in the BOM
	
	            Dim oAsmSkidDesignation As String = oAsmSkidNumber & "-" & oAsmSkidLetter & "-" & oBOMItemNumber
	                
	            Dim oComponentDefinitionPropertySet As PropertySet
	            oComponentDefinitionPropertySet = oComponentDefinition.Document.PropertySets.Item("Design Tracking Properties")
	                'Project property tab
	                
	            oComponentDefinitionPropertySet.Item("Part Number").Value = oAsmSkidDesignation
	                'populates the Part Number property
	        Catch
				
				Dim oComponentDefinition As ComponentDefinition
				oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
				MsgBox("anything")
				
				Dim oPropSets As PropertySets = oComponentDefinition.PropertySets
				
				Dim oPropSet As PropertySet = oPropSets.Item("Design Tracking Properties")
					
				Dim oPN As [Property] = oPropSet.Item("Part Number")
				
				Dim oPNText As String = oPN
				
				MessageBox.Show("The following components were unchanged: " & oPNText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
			End Try
        
		Next

End Sub
0 Likes
Replies (9)
Message 2 of 10

A.Acheson
Mentor
Mentor

Hi @Jonathan.CharltonE35Y2 

One line that will help you a lot is IsModifiable and here is the location to find it from the API Help.

When you have got the document object you can check if it is readonly/modifable. If it isn't then it is a CC/Library Part. As for suppressed parts you won't be able to detect that from the BOM I don't believe. You will likely need to loop through all occurrences for that info. Are you using model states? 

Syntax

PartDocument.IsModifiable() As Boolean

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 10

Jonathan.CharltonE35Y2
Contributor
Contributor

Hey @A.Acheson,

Thanks for the reply.

"One line that will help you a lot is IsModifiable and here is the location to find it from the API Help." - I had tried the .IsModifiable, but I hadn't tried it as a Boolean. I'll add that to the block and test it to see how it runs.


"As for suppressed parts you won't be able to detect that from the BOM I don't believe. You will likely need to loop through all occurrences for that info." - Got it.

 

"Are you using model states?" - Not at this time.

Your input and advice is very much appreciated.

0 Likes
Message 4 of 10

Jonathan.CharltonE35Y2
Contributor
Contributor

@A.Acheson,

I've incorporated the PartDocument.IsModifiable() As Boolean snippet into my blocks.
However, I keep getting an error on line 61 stating:

Public member 'IsModifiable' on type 'PartComponentDefinition' not found.

I am a little confused, as it seems that the .IsModifiable() is working in the Try portion of the block, but not in the Catch.
Can you offer some insight as to what I'm doing wrong?

Public Sub Main()       

    Dim oAssemblyDocument As AssemblyDocument 
    oAssemblyDocument= ThisDoc.Document

    Dim oAssemblyComponentDefinition As AssemblyComponentDefinition 
    oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition

    Dim oAsmCustomPropSet As PropertySet = oAssemblyDocument.PropertySets.Item("Inventor User Defined Properties")
    Dim oAsmSkidNumber As String
    Dim oAsmSkidLetter As String

    Try
            'Get current custom prop value
            oAsmSkidNumber = iProperties.Value("Custom", "SKID NUMBER")
            oAsmSkidLetter = iProperties.Value("Custom", "SKID LETTER")
    Catch
            'Assume error means prop not found so create it
            oAsmCustomPropSet.Add("", "SKID NUMBER")
            oAsmCustomPropSet.Add("", "SKID LETTER")
            'Get User input
            oAsmSkidNumber = InputBox("Please enter a Skid Number", "iLogic", oAsmSkidNumber)
            oAsmSkidLetter = InputBox("Please enter a Skid Letter", "iLogic", oAsmSkidLetter)
            'Set custom prop value
            iProperties.Value("Custom", "SKID NUMBER") = oAsmSkidNumber
            iProperties.Value("Custom", "SKID LETTER") = oAsmSkidLetter
    End Try	

    Dim oBOM As BOM 
    oBOM = oAssemblyComponentDefinition.BOM

    oBOM.PartsOnlyViewEnabled = True
    Dim oBOMView As BOMView 
    oBOMView = oBOM.BOMViews.Item("Parts Only") 'or structured

    Try 
        oBOMView.Sort("Description", True,)
        oBOMView.Renumber(1, 1)
    Catch
        MessageBox.Show("The program has failed. There are no parts to renumber.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        Exit Sub
    End Try

    For Each oBOMRow As BOMRow In oBOMView.BOMRows
		
			Dim oComponentDefinition As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)
			Dim oBOMItemNumber As String = oBOMRow.ItemNumber() 'this is item number in the BOM
			Dim oCompDefPropSet As PropertySet = oComponentDefinition.Document.PropertySets.Item("Design Tracking Properties")
				'Project property tab
			Dim oCompDefPN As String = oCompDefPropSet.Item("Part Number").Value
			
	   Try
		   
			If oComponentDefinition.IsModifiable() = True Then	        
				oCompDefPropSet.Item("Part Number").Value = oAsmSkidDesignation
					'populates the Part Number property
				End If
				
		Catch
			
				If oComponentDefinition.IsModifiable() = False Then
					MessageBox.Show("The following components were unchanged: " & oCompDefPN, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
				End If
				
		End Try
		
    Next

End Sub
0 Likes
Message 5 of 10

CGBenner
Community Manager
Community Manager

@Jonathan.CharltonE35Y2 

Hi!  Were you able to get this problem resolved, or do you still need some additional help from the Community or @A.Acheson ?

Did you find a post helpful? Then feel free to give likes to these posts!
Did your question get successfully answered? Then just click on the 'Accept solution' button.  Thanks and Enjoy!


Chris Benner
Community Manager

0 Likes
Message 6 of 10

Jonathan.CharltonE35Y2
Contributor
Contributor

@CGBenner ,

Hello!

Unfortunately, I was unable to get the code to perform as intended.
I instead opted for a different fix for the time being.
However, I do have some new information available that I haven't had the time to analyze, and will update this post with the functioning code upon completion.
If anyone has any input it is greatly appreciated, as I am still learning and would love to improve my knowledge of iLogic.

0 Likes
Message 7 of 10

A.Acheson
Mentor
Mentor

Is Modifiable property check is only available from the document object. So since we know these are only parts then declare the part document like below. Now when you type "." after the declared object the list of available methods and properties comes up and you can search for is Modifiable in that list. 

Dim partDoc as PartDocument = oComponentDefinition.Document

If partDoc.IsModifiable = True Then
   Logger.Info("Success " & partDoc.DisplayName & " is Writeable, do something else now")
End If

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor

Hi @Jonathan.CharltonE35Y2.  You requested some resources to help you learn iLogic in a private message, and on the forum, so I have attached a few PDF's that I created a while back that may help some with that.  These are just a few, because we are only allowed to attach 3 items per reply.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor

Here are a couple more that you may find interesting/useful.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 10

Jonathan.CharltonE35Y2
Contributor
Contributor

@A.Acheson,
Thank you for the detailed explanation and example. As time permits, I will attempt to implement this into my existing code and see if I can't get it to perform as intended. Once the code is working as intended, I will update this post with function code block to help others in the future, should they need it.
Your continued help and support is greatly appreciated!

@WCrihfield,
Thank you for sharing.
I'm looking forward to giving each of these a read, and seeing how I can improve my future code based off the information contained.

Hopefully others can find this post as well, and take advantage of the information provided.

0 Likes