Update to FX Auto-Populate Code

Update to FX Auto-Populate Code

harvey3ELEA
Advocate Advocate
400 Views
4 Replies
Message 1 of 5

Update to FX Auto-Populate Code

harvey3ELEA
Advocate
Advocate

I'm looking to take this excellent code supplied by Curtis W. and have it auto-populate my FX parameters at part level.

 

Currently, I have to drop a part or parts into an assembly to have this run properly, which works great, but I'm looking to eliminate that assembly requirement for individual parts.

 

While I can sometimes tweak a routine successfully, I'm not good enough at ilogic coding to understand how to do this.  Suggestions welcome...

 

Sub Main

	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences

	'step into sub with the target occurrence being the the top level assembly
	Call TraverseAssembly(oOccs)

End Sub

Sub TraverseAssembly(oOccs As ComponentOccurrences)

	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs

		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'step back into this sub with the target occurrence being the subassembly
			Call TraverseAssembly(oOcc.SubOccurrences)
		Else 'parts

			Dim partDoc As PartDocument = oOcc.Definition.Document

			If Not partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For' Check if sheet metal part

			'step into sub routine
			Call FlatExtents(partDoc)
		End If
	Next

End Sub


Sub FlatExtents(partDoc As PartDocument)

	Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

	If oCompDef.HasFlatPattern = False Then
		oCompDef.Unfold
		oCompDef.FlatPattern.ExitEdit
	End If

	oA = oCompDef.FlatPattern.Length
	oB = oCompDef.FlatPattern.Width

	oFlat_Length = Max(oA, oB)
	oFlat_Wdith = Min(oA, oB)

	' Get the parameters of the component definition
	Dim params As Parameters = oCompDef.Parameters

	' Check if parameters "LENGTH" and "WIDTH" exist, update or add them
	Try
		params.UserParameters.Item("LENGTH").Value = oFlat_Length
	Catch
		params.UserParameters.AddByValue("LENGTH", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("WIDTH").Value = oFlat_Wdith
	Catch
		params.UserParameters.AddByValue("WIDTH", oFlat_Wdith, "in")
	End Try

	' Set parameters to be exposed as properties
	params.Item("LENGTH").ExposedAsProperty = True
	params.Item("LENGTH").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
	params.Item("LENGTH").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
	params.Item("LENGTH").CustomPropertyFormat.ShowUnitsString = False

	params.Item("WIDTH").ExposedAsProperty = True
	params.Item("WIDTH").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
	params.Item("WIDTH").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
	params.Item("WIDTH").CustomPropertyFormat.ShowUnitsString = False
	
	params.Item("Thickness").ExposedAsProperty = True
	params.Item("Thickness").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
	params.Item("Thickness").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
	params.Item("Thickness").CustomPropertyFormat.ShowUnitsString = False

	iProperties.Value(partDoc.DisplayName, "Custom", "LENGTH") = "=<Sheet Metal Length>"
	iProperties.Value(partDoc.DisplayName, "Custom", "WIDTH") = "=<Sheet Metal Width>"

End Sub

 

Accepted solutions (1)
401 Views
4 Replies
Replies (4)
Message 2 of 5

Curtis_Waguespack
Consultant
Consultant

@harvey3ELEA, if you're running this as an external rule, you can just replace the Main sub with this and it will work for assemblies or parts, or you could use the 2nd example on just parts

 

use this for parts and assemblies ( replace the main sub in your original rule with this)

Sub Main

	Dim oDoc As Document = ThisDoc.Document

	If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

		Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
		Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences

		'step into sub with the target occurrence being the the top level assembly
		Call TraverseAssembly(oOccs)
		
	ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

		If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
			Call FlatExtents(oDoc)
		End If
	End If

End Sub

 

Use this for only parts ( use this as the entire rule)

Dim partDoc As Document = ThisDoc.Document

Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

If oCompDef.HasFlatPattern = False Then
	oCompDef.Unfold
	oCompDef.FlatPattern.ExitEdit
End If

oA = oCompDef.FlatPattern.Length
oB = oCompDef.FlatPattern.Width

oFlat_Length = Max(oA, oB)
oFlat_Wdith = Min(oA, oB)

' Get the parameters of the component definition
Dim params As Parameters = oCompDef.Parameters

' Check if parameters "LENGTH" and "WIDTH" exist, update or add them
Try
	params.UserParameters.Item("LENGTH").Value = oFlat_Length
Catch
	params.UserParameters.AddByValue("LENGTH", oFlat_Length, "in")
End Try
Try
	params.UserParameters.Item("WIDTH").Value = oFlat_Wdith
Catch
	params.UserParameters.AddByValue("WIDTH", oFlat_Wdith, "in")
End Try

' Set parameters to be exposed as properties
params.Item("LENGTH").ExposedAsProperty = True
params.Item("LENGTH").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
params.Item("LENGTH").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
params.Item("LENGTH").CustomPropertyFormat.ShowUnitsString = False

params.Item("WIDTH").ExposedAsProperty = True
params.Item("WIDTH").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
params.Item("WIDTH").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
params.Item("WIDTH").CustomPropertyFormat.ShowUnitsString = False

params.Item("Thickness").ExposedAsProperty = True
params.Item("Thickness").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
params.Item("Thickness").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
params.Item("Thickness").CustomPropertyFormat.ShowUnitsString = False

iProperties.Value(partDoc.DisplayName, "Custom", "LENGTH") = "=<Sheet Metal Length>"
iProperties.Value(partDoc.DisplayName, "Custom", "WIDTH") = "=<Sheet Metal Width>"

 

EESignature

0 Likes
Message 3 of 5

harvey3ELEA
Advocate
Advocate

Thanks, Curtis.  I'm seeing this error however when I implement your parts-only code from above into a new external rule on a dummy 6" x 4" x 1/2" plate .  Not sure what the error is telling me:

 

harvey3ELEA_1-1722609460025.png

 

 

 

Message 4 of 5

Curtis_Waguespack
Consultant
Consultant
Accepted solution

ooops! try this version

 

Dim partDoc As Document = ThisDoc.Document

If Not partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Exit Sub

Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

If oCompDef.HasFlatPattern = False Then
	oCompDef.Unfold
	oCompDef.FlatPattern.ExitEdit
End If

oA = oCompDef.FlatPattern.Length
oB = oCompDef.FlatPattern.Width

oFlat_Length = Max(oA, oB)
oFlat_Wdith = Min(oA, oB)

' Get the parameters of the component definition
Dim params As Parameters = oCompDef.Parameters

' Check if parameters "LENGTH" and "WIDTH" exist, update or add them
Try
	params.UserParameters.Item("LENGTH").Value = oFlat_Length
Catch
	params.UserParameters.AddByValue("LENGTH", oFlat_Length, "in")
End Try
Try
	params.UserParameters.Item("WIDTH").Value = oFlat_Wdith
Catch
	params.UserParameters.AddByValue("WIDTH", oFlat_Wdith, "in")
End Try

' Set parameters to be exposed as properties
params.Item("LENGTH").ExposedAsProperty = True
params.Item("LENGTH").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
params.Item("LENGTH").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
params.Item("LENGTH").CustomPropertyFormat.ShowUnitsString = False

params.Item("WIDTH").ExposedAsProperty = True
params.Item("WIDTH").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
params.Item("WIDTH").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
params.Item("WIDTH").CustomPropertyFormat.ShowUnitsString = False

params.Item("Thickness").ExposedAsProperty = True
params.Item("Thickness").CustomPropertyFormat.Units = UnitsTypeEnum.kInchLengthUnits
params.Item("Thickness").CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
params.Item("Thickness").CustomPropertyFormat.ShowUnitsString = False

iProperties.Value("Custom", "LENGTH") = "=<Sheet Metal Length>"
iProperties.Value("Custom", "WIDTH") = "=<Sheet Metal Width>"

 

EESignature

Message 5 of 5

harvey3ELEA
Advocate
Advocate

Works great.  Thanks for that quick code fix for individual parts.

As always, I appreciate your time & efforts.

Harvey