iLogic: replace item number with filename (3D Template)

iLogic: replace item number with filename (3D Template)

Anonymous
Not applicable
2,239 Views
13 Replies
Message 1 of 14

iLogic: replace item number with filename (3D Template)

Anonymous
Not applicable

Would it be possible to overwrite the existing item number, marked red on the attached file, with the filename of the assembly, by adding an iLogic code to the template?

0 Likes
2,240 Views
13 Replies
Replies (13)
Message 2 of 14

WCrihfield
Mentor
Mentor

Just to be clear...this is an Assembly File Template, right?

Does this template file already have items in it to start with, as part of the template?

The items in the BOM in the image appear to be Multi-Body Part files, so I asume you are wanting each component within this main assembly to be named something like: = oAssemblyFileName & " - " & oOccuranceNumber, right?

If so, this definately sounds doable, asuming it will accept a String type data instead of Integer.

I could give it a try if you'd like.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 14

WCrihfield
Mentor
Mentor

Try the following iLogic code:

Dim oAdoc As AssemblyDocument
oAdoc = ThisApplication.ActiveDocument

Dim oCompDef As AssemblyComponentDefinition
oCompDef = oAdoc.ComponentDefinition

Dim oBOM As BOM
oBOM = oCompDef.BOM

oBOM.StructuredViewEnabled = True

Dim oBOMviews As BOMViews
oBOMviews = oBOM.BOMViews

Dim oBOMview As BOMView

Dim oBOMrows As BOMRowsEnumerator

Dim oBOMrow As BOMRow

'Define this assembly's File Name
Dim oAFN As String
oAFN = ThisDoc.FileName(False)

i = 1
For Each oBOMview In oBOMviews
	If oBOMview.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
		oBOMrows = oBOMview.BOMRows
		For Each oBOMrow In oBOMrows
			oBOMrow.ItemNumber = oAFN & " - " & i.ToString
		Next
	End If
Next

I hope this helps.

If this solves your problem, please click 'Accept As Solution'.

Or if this helps you on your way to achieving your goal, please click 'Like'.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 14

Anonymous
Not applicable

I've just uploaded a better picture. I have overwritten the item number manually for illustration purpose. For this example. the top level is called 12345, 1st level sub-assembly 12345-01 and 2nd level 12345-01-01. Those are all the filenames as well.

 

- The Item Number must be replaced with an .iam's exact filename

 

- The Item number should not be replaced for an .ipt

 

- If possible, we would like to be able to turn on/off this feature, maybe with a fx parameter called ReplaceItemNumber (Yes/no)

 

Can this be coded? I'm an entry-level coder, so I have limited coding experience

0 Likes
Message 5 of 14

WCrihfield
Mentor
Mentor

OK. I need to further clarify before doing more coding.

Within this image, it appears that you intend to have every Item Number be exactly the same as the Part Number on the same row.  Do you want both the Part Number & the Item Number edited to make this happen?  Both can be edited within the BOM, but I don't know if this may cause you problems later or not.  Is each line item's Part Number already exactly the same as its File Name, before any code is ran?  If you just want the Item Number of each Row to be exactly the same as that Row's Part Number, and there were multiple line items with the same part number, you'd have to add something to the end of the item number, or it will give an error and not work.  Each line item within the BOM must have a unique Item Number.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 14

Anonymous
Not applicable

OK. I need to further clarify before doing more coding.

Within this image, it appears that you intend to have every Item Number be exactly the same as the Part Number on the same row. 

Correct. But only for assemblies and a ON/OFF function would be nice. so that we can revert back to normal item numbers if needed, like a I described in the previous message

 

Do you want both the Part Number & the Item Number edited to make this happen?  Both can be edited within the BOM, but I don't know if this may cause you problems later or not.

Only Item number. It's for ERP purpose, but we can't use the part number column

 

Is each line item's Part Number already exactly the same as its File Name, before any code is ran?

Yes. It's for layout purpose, which means that we do not use are normal item number structure

 

If you just want the Item Number of each Row to be exactly the same as that Row's Part Number, and there were multiple line items with the same part number, you'd have to add something to the end of the item number, or it will give an error and not work.  Each line item within the BOM must have a unique Item Number.

All assemblies will have a unique filename, so that no doublets should occur

0 Likes
Message 7 of 14

WCrihfield
Mentor
Mentor

OK. Try This.

Dim oAdoc As AssemblyDocument
oAdoc = ThisApplication.ActiveDocument

Dim oCompDef As AssemblyComponentDefinition
oCompDef = oAdoc.ComponentDefinition

Dim oUParams As UserParameters
oUParams = oCompDef.Parameters.UserParameters

Dim oParamName As String
oParamName = "ReplaceItemNumber"

Dim oExists As Boolean
oExists = False

For Each oUParam As UserParameter In oUParams
	If oUParam.Name = oParamName Then
		oExists = True
	End If
Next

If oExists = False Then
	'The specified Parameter doesn't exist, so create it, sets its value(s) & options.
	oParameter = oUParams.AddByExpression(oParamName, False ,UnitsTypeEnum.kBooleanUnits)
	Parameter.Param(oParamName).IsKey = True
ElseIf oExists = True Then
	oAnswer = MessageBox.Show("Do you want to replace the Item Numbers within the BOM?","BOM ITEM NUMBERS",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)
	If oAnswer = vbNo Then
		Parameter(oParamName) = False
	ElseIf oAnswer = vbYes Then
		Parameter(oParamName) = True
	End If
End If

Dim oBOM As BOM
oBOM = oCompDef.BOM

oBOM.StructuredViewEnabled = True

Dim oItemCompDef As ComponentDefinition

Dim oItemPN As [Property]

If Parameter(oParamName) = True Then
	i = 1
	For Each oBOMview As BOMView In oBOM.BOMViews
		If oBOMview.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
			For Each oBOMrow As BOMRow In oBOMview.BOMRows
				oItemCompDef = oBOMrow.ComponentDefinitions.Item(1)
				If TypeOf oItemCompDef Is AssemblyComponentDefinition Then
					oItemPN = oItemCompDef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number")
					oBOMrow.ItemNumber = oItemPN
				End If
			Next
		End If
	Next
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 14

Anonymous
Not applicable

It returned the following error

 

If possible, i would be great if could be a text parameter with Yes/No

 

 


Error in rule: Rule0, in document: 12345.iam

Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.


 

0 Likes
Message 9 of 14

WCrihfield
Mentor
Mentor

OK. I think I've corrected the problem now. Try this.

Dim oAdoc As AssemblyDocument
oAdoc = ThisApplication.ActiveDocument

Dim oCompDef As AssemblyComponentDefinition
oCompDef = oAdoc.ComponentDefinition

Dim oUParams As UserParameters
oUParams = oCompDef.Parameters.UserParameters

Dim oParamName As String
oParamName = "ReplaceItemNumber"

Dim oExists As Boolean
oExists = False

For Each oUParam As UserParameter In oUParams
	If oUParam.Name = oParamName Then
		oExists = True
	End If
Next

If oExists = False Then
	'The specified Parameter doesn't exist, so create it, sets its value(s) & options.
	oParameter = oUParams.AddByValue(oParamName, "False", UnitsTypeEnum.kTextUnits)
	MultiValue.SetList(oParamName,"True","False")
	Parameter.Param(oParamName).IsKey = True
ElseIf oExists = True Then
	oAnswer = MessageBox.Show("Do you want to replace the Item Numbers within the BOM?","BOM ITEM NUMBERS",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)
	If oAnswer = vbNo Then
		Parameter(oParamName) = "False"
	ElseIf oAnswer = vbYes Then
		Parameter(oParamName) = "True"
	End If
End If

Dim oBOM As BOM
oBOM = oCompDef.BOM

oBOM.StructuredViewEnabled = True

Dim oItemPN As [Property]

If Parameter(oParamName) = "True" Then
	i = 1
	For Each oBOMview As BOMView In oBOM.BOMViews
		If oBOMview.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
			For Each oBOMrow As BOMRow In oBOMview.BOMRows
				If oBOMrow.ReferencedFileDescriptor.ReferencedFileType = FileTypeEnum.kAssemblyFileType Then
					For Each oDoc As Document In oAdoc.AllReferencedDocuments
						If oDoc.FullFileName = oBOMrow.ReferencedFileDescriptor.FullFileName Then
							oItemPN = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
							oBOMrow.ItemNumber = oItemPN.Value.ToString
						End If
					Next
				End If
			Next
		End If
	Next
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 14

Anonymous
Not applicable

- The item doesn't quite look like the part number/file name 

- Also the parameter doesn't trigger any change to the item number

 

I've attached screen shots

0 Likes
Message 11 of 14

WCrihfield
Mentor
Mentor

This version will deal with "ChildRows" of the main BOM rows too.

There are a couple other variables that this rule identifies and notifies you of, but doesn't do anything with at this point, because I don't know if you have any of these within your BOM.

These variables are:

  • If the row is "Merged"
  • If the row is "Promoted"
  • If the row is "RolledUp"

Right now I have just set-up a MessageBox to tell you if one of these conditions is true of the BOM row.

As for the triggering, of the rule to run when the Parameter value changes, this can be done in a few different ways.

Simply using the Manage tab / iLogic panel / Event Triggers tool, most likely won't work for this rule, because it is a UserParameter and not a ModelParameter, for one, and it is a Text type parameter secondly.  Also it wouldn't be a good idea to use the iProperty Change trigger in those settings unless you have a rule which converts your Text & Boolean style parameters to Custom iProperties (which is easy to do).

I think the best two options for firing this rule would be the following:

  • If you are changing the "ReplaceItemNumber" parameter manually from a Form, you could unclude a button below that within your form, that runs this rule.
  • If you are changing the "ReplaceItemNumber" parameter manually from the Parameters dialog box, then you should create a local rule within the document, that contains a RunExternalRule("RuleName") line.  Then above that line, create a line something like oDummyVariable1 = ReplaceItemNumber.  The parameter name should not be in quotation marks, it should be blue (the default color code for local parameters).  This causes the file to 'watch' that local parameter for changes, then run the rule when it does.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 14

WCrihfield
Mentor
Mentor

Forgot to insert the code:

Dim oAdoc As AssemblyDocument
oAdoc = ThisApplication.ActiveDocument

Dim oCompDef As AssemblyComponentDefinition
oCompDef = oAdoc.ComponentDefinition

Dim oUParams As UserParameters
oUParams = oCompDef.Parameters.UserParameters

Dim oParamName As String
oParamName = "ReplaceItemNumber"

Dim oExists As Boolean
oExists = False

For Each oUParam As UserParameter In oUParams
	If oUParam.Name = oParamName Then
		oExists = True
	End If
Next

If oExists = False Then
	'The specified Parameter doesn't exist, so create it, sets its value(s) & options.
	oParameter = oUParams.AddByValue(oParamName, "False", UnitsTypeEnum.kTextUnits)
	MultiValue.SetList(oParamName,"True","False")
	Parameter.Param(oParamName).IsKey = True
ElseIf oExists = True Then
	oAnswer = MessageBox.Show("Do you want to replace the Item Numbers within the BOM?","BOM ITEM NUMBERS",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2)
	If oAnswer = vbNo Then
		Parameter(oParamName) = "False"
	ElseIf oAnswer = vbYes Then
		Parameter(oParamName) = "True"
	End If
End If

Dim oBOM As BOM
oBOM = oCompDef.BOM

oBOM.StructuredViewEnabled = True

Dim oItemPN As [Property]

Dim oChildItemPN As [Property]

Dim oDoc As Document

If Parameter(oParamName) = "False" Then
	Return
ElseIf Parameter(oParamName) = "True" Then
	i = 1
	For Each oBOMview As BOMView In oBOM.BOMViews
		If oBOMview.ViewType = BOMViewTypeEnum.kStructuredBOMViewType Then
			For Each oBOMrow As BOMRow In oBOMview.BOMRows
				If oBOMrow.ReferencedFileDescriptor.ReferencedFileType = FileTypeEnum.kAssemblyFileType Then
					If oBOMrow.Merged = True Then
						MessageBox.Show("This BOM row: " & oBOMrow.ItemNumber & " has been 'Merged'.")
					End If
					If oBOMrow.Promoted = True Then
						MessageBox.Show("This BOM row: " & oBOMrow.ItemNumber & " has been 'Promoted'.")
					End If
					If oBOMrow.RolledUp = True Then
						MessageBox.Show("This BOM row: " & oBOMrow.ItemNumber & " has been 'RolledUp'.")
					End If
					If oBOMrow.ChildRows.Count > 0 Then
						For Each oChildRow As BOMRow In oBOMrow.ChildRows
							For Each oDoc In oAdoc.AllReferencedDocuments
								If oDoc.FullFileName = oChildRow.ReferencedFileDescriptor.FullFileName Then
									oChildItemPN = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
									oChildRow.ItemNumber = oChildItemPN.Value.ToString
								End If
							Next
						Next
					End If
					If oBOMrow.ChildRows Is Nothing Then
						For Each oDoc In oAdoc.AllReferencedDocuments
							If oDoc.FullFileName = oBOMrow.ReferencedFileDescriptor.FullFileName Then
								oItemPN = oDoc.PropertySets.Item("Design Tracking Properties").Item("Part Number")
								oBOMrow.ItemNumber = oItemPN.Value.ToString
							End If
						Next
					End If
				End If
			Next
		End If
	Next
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 14

Anonymous
Not applicable

FX Parameter

- If I set the parameter to false nothing happens

- Also, if I set the parameter to false and I run the rule, it reverts back to True

 

BOM

- Level 1 and 2 looks ok. Level 3 doesn't resemble the file name

- The item no. has changed on the part which is shouldn't. Don't know if that is "codeable" 

 

Picture is attached

 

0 Likes
Message 14 of 14

Anonymous
Not applicable

Hello @WCrihfield 

 

I have also a problem to renumber my item numbers and I can't find a solution.
I like to renumber my item numbers automaticly and not manually. So I'm looking for a little code to set the item number with ilogic.
 
 
 
For expample I like to renumber the item number of my part named with the part number: 1337 to item number 33:


Item.Number("1337", "33")
0 Likes