Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FLAT PATTERN DIMS FILLED OUT AND ADDED TO PARAMETERS

17 REPLIES 17
SOLVED
Reply
Message 1 of 18
_bmiller_
861 Views, 17 Replies

FLAT PATTERN DIMS FILLED OUT AND ADDED TO PARAMETERS

_bmiller_
Contributor
Contributor

Getting stuck on this one. i have a rule to fire in a sheet metal part that will add parameters and up date them with flat extents, this works fine but i need it to fire from assembly and catch each sheetmetal part in assembly. if parameters exist then it updates them if it dont exist then it adds them and updates them.

 

this is the simple rule:

Sub Main



	' Check if the active document is a part document
	Dim curDoc = ThisApplication.ActiveDocument
	If curDoc.DocumentType = kPartDocumentObject Then
	Else
		' Display an error message if the document is not a part document
		'        MessageBox.Show("This document is not a Sheet Metal Document and can not be run in this document ", "Ope Sorry!")
		Exit Sub
	End If

	' Get the active document
	Dim oDoc As PartDocument
	oDoc = ThisApplication.ActiveDocument

	' Get the component definition of the document
	Dim oDef As PartComponentDefinition
	oDef = oDoc.ComponentDefinition

	' Check if the document subtype indicates it's a sheet metal document
	If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
	Else
		' Display an error message if the document is not a sheet metal document
		'        MessageBox.Show("This document is not a Sheet Metal Document and can not be run in this document ", "Ope Sorry!")
		Exit Sub
	End If

	' Get the parameters of the component definition
	Dim oparams As Parameters
	Dim oparam As Parameter
	oparams = oDoc.ComponentDefinition.Parameters

	' Check if the parameters for flat extents length and width already exist
	Dim ExtentsLengthExists As Boolean = False
	Dim ExtentsWidthExists As Boolean = False
	For Each oparam In oparams
		If oparam.Name = "DIMA" Then ExtentsLengthExists = True
		If oparam.Name = "DIMB" Then ExtentsWidthExists = True
	Next oparam

	' Update or add the parameters for flat extents length and width

	If ExtentsLengthExists Then
		oparams.Item("DIMA").Value = SheetMetal.FlatExtentsLength * 2.54
	Else
		oparams.UserParameters.AddByValue("DIMA", SheetMetal.FlatExtentsLength * 2.54, "in")
	End If

	If ExtentsWidthExists Then
		oparams.Item("DIMB").Value = SheetMetal.FlatExtentsWidth * 2.54
	Else
		oparams.UserParameters.AddByValue("DIMB", SheetMetal.FlatExtentsWidth * 2.54, "in")
	End If

	' Set the parameters to be exposed as properties
	oparams.Item("DIMA").ExposedAsProperty = True
	oparams.Item("DIMB").ExposedAsProperty = True

	' Format the custom properties
	Dim oParameter As Parameter
	For Each oParameter In oDoc.ComponentDefinition.Parameters.UserParameters
		Try
			Dim oFormat As CustomPropertyFormat
			oFormat = oParameter.CustomPropertyFormat
			oFormat.ShowUnitsString = False
			oFormat.ShowLeadingZeros = False
			oFormat.ShowTrailingZeros = False
			oFormat.Precision = kThreeDecimalPlacesPrecision
		Catch
		End Try

	Next oParameter

	' Check if the project description is empty and prompt user to enter a description
	'    Dim description As String
	'    If iProperties.Value("Project", "Description") = "" Then
	'        description = InputBox("There is no description entered. Please enter one now. This message box will only appear if the Description field in iProperties is empty.", "Enter Description", "Description")
	'        iProperties.Value("Project", "Description") = description
	'    End If

	' Update the document
	oDoc.Update

	' Set iLogic to update when done
	iLogicVb.UpdateWhenDone = True
End Sub
0 Likes

FLAT PATTERN DIMS FILLED OUT AND ADDED TO PARAMETERS

Getting stuck on this one. i have a rule to fire in a sheet metal part that will add parameters and up date them with flat extents, this works fine but i need it to fire from assembly and catch each sheetmetal part in assembly. if parameters exist then it updates them if it dont exist then it adds them and updates them.

 

this is the simple rule:

Sub Main



	' Check if the active document is a part document
	Dim curDoc = ThisApplication.ActiveDocument
	If curDoc.DocumentType = kPartDocumentObject Then
	Else
		' Display an error message if the document is not a part document
		'        MessageBox.Show("This document is not a Sheet Metal Document and can not be run in this document ", "Ope Sorry!")
		Exit Sub
	End If

	' Get the active document
	Dim oDoc As PartDocument
	oDoc = ThisApplication.ActiveDocument

	' Get the component definition of the document
	Dim oDef As PartComponentDefinition
	oDef = oDoc.ComponentDefinition

	' Check if the document subtype indicates it's a sheet metal document
	If oDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
	Else
		' Display an error message if the document is not a sheet metal document
		'        MessageBox.Show("This document is not a Sheet Metal Document and can not be run in this document ", "Ope Sorry!")
		Exit Sub
	End If

	' Get the parameters of the component definition
	Dim oparams As Parameters
	Dim oparam As Parameter
	oparams = oDoc.ComponentDefinition.Parameters

	' Check if the parameters for flat extents length and width already exist
	Dim ExtentsLengthExists As Boolean = False
	Dim ExtentsWidthExists As Boolean = False
	For Each oparam In oparams
		If oparam.Name = "DIMA" Then ExtentsLengthExists = True
		If oparam.Name = "DIMB" Then ExtentsWidthExists = True
	Next oparam

	' Update or add the parameters for flat extents length and width

	If ExtentsLengthExists Then
		oparams.Item("DIMA").Value = SheetMetal.FlatExtentsLength * 2.54
	Else
		oparams.UserParameters.AddByValue("DIMA", SheetMetal.FlatExtentsLength * 2.54, "in")
	End If

	If ExtentsWidthExists Then
		oparams.Item("DIMB").Value = SheetMetal.FlatExtentsWidth * 2.54
	Else
		oparams.UserParameters.AddByValue("DIMB", SheetMetal.FlatExtentsWidth * 2.54, "in")
	End If

	' Set the parameters to be exposed as properties
	oparams.Item("DIMA").ExposedAsProperty = True
	oparams.Item("DIMB").ExposedAsProperty = True

	' Format the custom properties
	Dim oParameter As Parameter
	For Each oParameter In oDoc.ComponentDefinition.Parameters.UserParameters
		Try
			Dim oFormat As CustomPropertyFormat
			oFormat = oParameter.CustomPropertyFormat
			oFormat.ShowUnitsString = False
			oFormat.ShowLeadingZeros = False
			oFormat.ShowTrailingZeros = False
			oFormat.Precision = kThreeDecimalPlacesPrecision
		Catch
		End Try

	Next oParameter

	' Check if the project description is empty and prompt user to enter a description
	'    Dim description As String
	'    If iProperties.Value("Project", "Description") = "" Then
	'        description = InputBox("There is no description entered. Please enter one now. This message box will only appear if the Description field in iProperties is empty.", "Enter Description", "Description")
	'        iProperties.Value("Project", "Description") = description
	'    End If

	' Update the document
	oDoc.Update

	' Set iLogic to update when done
	iLogicVb.UpdateWhenDone = True
End Sub
17 REPLIES 17
Message 2 of 18
_bmiller_
in reply to: _bmiller_

_bmiller_
Contributor
Contributor

THIS IS WHAT I HAVE SO FAR BUT ITS NOT WORKING:

' Rule to update flat extents parameters for sheet metal parts in an assembly

Dim flatExtentsLength As Double = SheetMetal.FlatExtentsLength * 2.54 ' Convert to inches
Dim flatExtentsWidth As Double = SheetMetal.FlatExtentsWidth * 2.54 ' Convert to inches

' Loop through all components in the active assembly
For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
' Check if the component occurrence is a part document and a sheet metal part
If comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim partDoc As PartDocument = comp.Definition.Document

If partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then ' Check if sheet metal part
' Get the parameters of the component definition
Dim params As Parameters = partDoc.ComponentDefinition.Parameters

' Check if parameters "DIMA" and "DIMB" exist, update or add them
If Not params.UserParameters.Item("DIMA").Exists Then
params.UserParameters.AddByValue("DIMA", flatExtentsLength, "in")
Else
params.UserParameters.Item("DIMA").Value = flatExtentsLength
End If

If Not params.UserParameters.Item("DIMB").Exists Then
params.UserParameters.AddByValue("DIMB", flatExtentsWidth, "in")
Else
params.UserParameters.Item("DIMB").Value = flatExtentsWidth
End If

' Set parameters to be exposed as properties
params.Item("DIMA").ExposedAsProperty = True
params.Item("DIMB").ExposedAsProperty = True
End If
End If
Next

' Update the active assembly after parameter changes
ThisApplication.ActiveDocument.Update

0 Likes

THIS IS WHAT I HAVE SO FAR BUT ITS NOT WORKING:

' Rule to update flat extents parameters for sheet metal parts in an assembly

Dim flatExtentsLength As Double = SheetMetal.FlatExtentsLength * 2.54 ' Convert to inches
Dim flatExtentsWidth As Double = SheetMetal.FlatExtentsWidth * 2.54 ' Convert to inches

' Loop through all components in the active assembly
For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
' Check if the component occurrence is a part document and a sheet metal part
If comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim partDoc As PartDocument = comp.Definition.Document

If partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then ' Check if sheet metal part
' Get the parameters of the component definition
Dim params As Parameters = partDoc.ComponentDefinition.Parameters

' Check if parameters "DIMA" and "DIMB" exist, update or add them
If Not params.UserParameters.Item("DIMA").Exists Then
params.UserParameters.AddByValue("DIMA", flatExtentsLength, "in")
Else
params.UserParameters.Item("DIMA").Value = flatExtentsLength
End If

If Not params.UserParameters.Item("DIMB").Exists Then
params.UserParameters.AddByValue("DIMB", flatExtentsWidth, "in")
Else
params.UserParameters.Item("DIMB").Value = flatExtentsWidth
End If

' Set parameters to be exposed as properties
params.Item("DIMA").ExposedAsProperty = True
params.Item("DIMB").ExposedAsProperty = True
End If
End If
Next

' Update the active assembly after parameter changes
ThisApplication.ActiveDocument.Update

Message 3 of 18
act_mgoodwin
in reply to: _bmiller_

act_mgoodwin
Explorer
Explorer

It appears you are trying to get flat pattern Length and Width to populate a DIMA and DIMB parameter, did you know that sheet metal parts with flat patterns have a special self updating value?

If you set your DIMA to the expression of "=<Sheet Metal Length>" and DIMB "=<Sheet Metal Width>" they will automatically update every time the model changes using the units set in the document.

0 Likes

It appears you are trying to get flat pattern Length and Width to populate a DIMA and DIMB parameter, did you know that sheet metal parts with flat patterns have a special self updating value?

If you set your DIMA to the expression of "=<Sheet Metal Length>" and DIMB "=<Sheet Metal Width>" they will automatically update every time the model changes using the units set in the document.

Message 4 of 18
_bmiller_
in reply to: act_mgoodwin

_bmiller_
Contributor
Contributor

i tried it but it will not accept it in the params. i must be doing it wrong or misunderstanding.

0 Likes

i tried it but it will not accept it in the params. i must be doing it wrong or misunderstanding.

Message 5 of 18

Curtis_Waguespack
Consultant
Consultant

@_bmiller_ 

 

Do you truly need the parameters, or are they being created just to get the information into iProperties?

 

If you don't need them you can do something like this example:

 

' Loop through all components in the active assembly
For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
	' Check if the component occurrence is a part document and a sheet metal part
	If Not comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For
		
	Dim partDoc As PartDocument = comp.Definition.Document

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

	Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

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

	iProperties.Value(comp.Name, "Custom", "DIMA") = "=<Sheet Metal Length>"
	iProperties.Value(comp.Name, "Custom", "DIMB") = "=<Sheet Metal Width>"

Next

 

 

If you do need the parameters, you can do something like this example:

 

' Loop through all components in the active assembly
For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
	' Check if the component occurrence is a part document and a sheet metal part
	If Not comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For

	Dim partDoc As PartDocument = comp.Definition.Document

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

	Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

	If oCompDef.HasFlatPattern = False Then
		oCompDef.Unfold
		oCompDef.FlatPattern.ExitEdit
	End If
	
	oFlat_Length = oCompDef.FlatPattern.Length
	oFlat_Wdith = oCompDef.FlatPattern.Width

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

	' Check if parameters "DIMA" and "DIMB" exist, update or add them
	Try
		params.UserParameters.Item("DIMA").Value = oFlat_Length
	Catch
		params.UserParameters.AddByExpression("DIMA", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("DIMB").Expression = oFlat_Wdith
	Catch
		params.UserParameters.AddByExpression("DIMB", oFlat_Wdith, "in")
	End Try

	' Set parameters to be exposed as properties
	params.Item("DIMA").ExposedAsProperty = True
	params.Item("DIMB").ExposedAsProperty = True

	iProperties.Value(comp.Name, "Custom", "DIMA") = "=<Sheet Metal Length>"
	iProperties.Value(comp.Name, "Custom", "DIMB") = "=<Sheet Metal Width>"

Next

' Update the active assembly after Parameter changes
ThisApplication.ActiveDocument.Update

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

0 Likes

@_bmiller_ 

 

Do you truly need the parameters, or are they being created just to get the information into iProperties?

 

If you don't need them you can do something like this example:

 

' Loop through all components in the active assembly
For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
	' Check if the component occurrence is a part document and a sheet metal part
	If Not comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For
		
	Dim partDoc As PartDocument = comp.Definition.Document

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

	Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

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

	iProperties.Value(comp.Name, "Custom", "DIMA") = "=<Sheet Metal Length>"
	iProperties.Value(comp.Name, "Custom", "DIMB") = "=<Sheet Metal Width>"

Next

 

 

If you do need the parameters, you can do something like this example:

 

' Loop through all components in the active assembly
For Each comp As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
	' Check if the component occurrence is a part document and a sheet metal part
	If Not comp.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For

	Dim partDoc As PartDocument = comp.Definition.Document

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

	Dim oCompDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

	If oCompDef.HasFlatPattern = False Then
		oCompDef.Unfold
		oCompDef.FlatPattern.ExitEdit
	End If
	
	oFlat_Length = oCompDef.FlatPattern.Length
	oFlat_Wdith = oCompDef.FlatPattern.Width

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

	' Check if parameters "DIMA" and "DIMB" exist, update or add them
	Try
		params.UserParameters.Item("DIMA").Value = oFlat_Length
	Catch
		params.UserParameters.AddByExpression("DIMA", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("DIMB").Expression = oFlat_Wdith
	Catch
		params.UserParameters.AddByExpression("DIMB", oFlat_Wdith, "in")
	End Try

	' Set parameters to be exposed as properties
	params.Item("DIMA").ExposedAsProperty = True
	params.Item("DIMB").ExposedAsProperty = True

	iProperties.Value(comp.Name, "Custom", "DIMA") = "=<Sheet Metal Length>"
	iProperties.Value(comp.Name, "Custom", "DIMB") = "=<Sheet Metal Width>"

Next

' Update the active assembly after Parameter changes
ThisApplication.ActiveDocument.Update

 I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

Message 6 of 18

_bmiller_
Contributor
Contributor
I need them in parms for cut list on print and excel. also the get ref by other rules. second one works like i need but it seems to stop at first layer. it isn't catching parts in sub assemblys.
0 Likes

I need them in parms for cut list on print and excel. also the get ref by other rules. second one works like i need but it seems to stop at first layer. it isn't catching parts in sub assemblys.
Message 7 of 18

Curtis_Waguespack
Consultant
Consultant
Accepted solution

@_bmiller_ , give this a try ( I didn't test it well, but it should work) 

 

 

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

	oFlat_Length = oCompDef.FlatPattern.Length
	oFlat_Wdith = oCompDef.FlatPattern.Width

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

	' Check if parameters "DIMA" and "DIMB" exist, update or add them
	Try
		params.UserParameters.Item("DIMA").Value = oFlat_Length
	Catch
		params.UserParameters.AddByExpression("DIMA", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("DIMB").Expression = oFlat_Wdith
	Catch
		params.UserParameters.AddByExpression("DIMB", oFlat_Wdith, "in")
	End Try

	' Set parameters to be exposed as properties
	params.Item("DIMA").ExposedAsProperty = True
	params.Item("DIMB").ExposedAsProperty = True

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

End Sub

 

@_bmiller_ , give this a try ( I didn't test it well, but it should work) 

 

 

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

	oFlat_Length = oCompDef.FlatPattern.Length
	oFlat_Wdith = oCompDef.FlatPattern.Width

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

	' Check if parameters "DIMA" and "DIMB" exist, update or add them
	Try
		params.UserParameters.Item("DIMA").Value = oFlat_Length
	Catch
		params.UserParameters.AddByExpression("DIMA", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("DIMB").Expression = oFlat_Wdith
	Catch
		params.UserParameters.AddByExpression("DIMB", oFlat_Wdith, "in")
	End Try

	' Set parameters to be exposed as properties
	params.Item("DIMA").ExposedAsProperty = True
	params.Item("DIMB").ExposedAsProperty = True

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

End Sub

 

Message 8 of 18

_bmiller_
Contributor
Contributor
IT WORKS! THANK YOU

IT WORKS! THANK YOU
Message 9 of 18

_bmiller_
Contributor
Contributor
I did change one line for those whoe copy this
params.UserParameters.Item("DIMB").Expression = oFlat_Wdith
to
params.UserParameters.Item("DIMB").Value = oFlat_Wdith

I did change one line for those whoe copy this
params.UserParameters.Item("DIMB").Expression = oFlat_Wdith
to
params.UserParameters.Item("DIMB").Value = oFlat_Wdith
Message 10 of 18
harvey3ELEA
in reply to: _bmiller_

harvey3ELEA
Advocate
Advocate

Does anyone know a way to get this code to also pull in custom formatting of the generated values to show:

 

- Fractional format

- Units string box unchecked

- Leading Zeros box unchecked

- Trailing Zeros box unchecked

- Precision set to 1/16

- Apply to existing comparable parameters box checked

 

harvey3ELEA_0-1718828224069.png

 

Another nicety for consistency would be to always have the longer dimension be the "LENGTH", if possible.

 

 

Does anyone know a way to get this code to also pull in custom formatting of the generated values to show:

 

- Fractional format

- Units string box unchecked

- Leading Zeros box unchecked

- Trailing Zeros box unchecked

- Precision set to 1/16

- Apply to existing comparable parameters box checked

 

harvey3ELEA_0-1718828224069.png

 

Another nicety for consistency would be to always have the longer dimension be the "LENGTH", if possible.

 

 

Message 11 of 18

Curtis_Waguespack
Consultant
Consultant

 

@harvey3ELEA, using the previous example, see the additions in blue

 

I don't think the Apply to existing... checkbox is applicable here, as it we wouldn't do that via automation... or at least I don't know how to programmatically select that

 

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 "DIMA" and "DIMB" exist, update or add them
	Try
		params.UserParameters.Item("DIMA").Value = oFlat_Length
	Catch
		params.UserParameters.AddByValue("DIMA", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("DIMB").Value = oFlat_Wdith
	Catch
		params.UserParameters.AddByValue("DIMB", oFlat_Wdith, "in")
	End Try

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

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

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

End Sub

 

0 Likes

 

@harvey3ELEA, using the previous example, see the additions in blue

 

I don't think the Apply to existing... checkbox is applicable here, as it we wouldn't do that via automation... or at least I don't know how to programmatically select that

 

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 "DIMA" and "DIMB" exist, update or add them
	Try
		params.UserParameters.Item("DIMA").Value = oFlat_Length
	Catch
		params.UserParameters.AddByValue("DIMA", oFlat_Length, "in")
	End Try
	Try
		params.UserParameters.Item("DIMB").Value = oFlat_Wdith
	Catch
		params.UserParameters.AddByValue("DIMB", oFlat_Wdith, "in")
	End Try

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

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

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

End Sub

 

Message 12 of 18
harvey3ELEA
in reply to: _bmiller_

harvey3ELEA
Advocate
Advocate

Curtis, thanks as always.  I'll give this a run in the morning as I'm pretty worn out from the work day.

0 Likes

Curtis, thanks as always.  I'll give this a run in the morning as I'm pretty worn out from the work day.

Message 13 of 18
harvey3ELEA
in reply to: _bmiller_

harvey3ELEA
Advocate
Advocate

Curtis, works great!

 

If I may ask, is there a code snippet that can be added to include checkboxing the "Thickness" export parameter along with the DIMA & DIMB checkboxes?

Curtis, works great!

 

If I may ask, is there a code snippet that can be added to include checkboxing the "Thickness" export parameter along with the DIMA & DIMB checkboxes?

Message 14 of 18

Curtis_Waguespack
Consultant
Consultant

@harvey3ELEA 

 

In context of the previous examples it would be this:

 

	params.Item("Thickness").ExposedAsProperty = True

 

@harvey3ELEA 

 

In context of the previous examples it would be this:

 

	params.Item("Thickness").ExposedAsProperty = True

 

Message 15 of 18
harvey3ELEA
in reply to: _bmiller_

harvey3ELEA
Advocate
Advocate

Curtis, works great with that latest thickness adder and subsequent "blue" lines of code.  You efforts are truly appreciated!

 

I may try to see if this routine can be run at part level, and not assembly level, but for now, this code is a tremendous time saver as we were constantly adding flat pattern sketches to literally hundreds to thousands of parts to achieve the necessary FX table population.

0 Likes

Curtis, works great with that latest thickness adder and subsequent "blue" lines of code.  You efforts are truly appreciated!

 

I may try to see if this routine can be run at part level, and not assembly level, but for now, this code is a tremendous time saver as we were constantly adding flat pattern sketches to literally hundreds to thousands of parts to achieve the necessary FX table population.

Message 16 of 18

_dscholtes_
Advocate
Advocate

@Curtis_Waguespack 

I'm just wondering why you took the traversing occurrences way instead of going the AllReferencedDocuments way, as we're eventually dealing with (iproperties in) documents and multiple occurrences may share the same document (unnecessary work).

0 Likes

@Curtis_Waguespack 

I'm just wondering why you took the traversing occurrences way instead of going the AllReferencedDocuments way, as we're eventually dealing with (iproperties in) documents and multiple occurrences may share the same document (unnecessary work).

Message 17 of 18

deffysalfauzi
Enthusiast
Enthusiast

it works great!

But, I have problem, when a part (not sheet metal) included in assembly.

 

Screenshot 2024-06-27 091640.pngScreenshot 2024-06-27 091614.png

Screenshot 2024-06-27 092359.png

0 Likes

it works great!

But, I have problem, when a part (not sheet metal) included in assembly.

 

Screenshot 2024-06-27 091640.pngScreenshot 2024-06-27 091614.png

Screenshot 2024-06-27 092359.png

Message 18 of 18

act_mgoodwin
Explorer
Explorer

You'll need something unique about the part to handle this, for example if you have the part document description set to contain "PLATE" you can add a check before continuing the macro per part. add this after "partDoc" is defined.

 

Dim partDoc As PartDocument = oOcc.Definition.Document
If instr(1, partDoc.PropertySets("Design Tracking Properties").item("Description").Expression, "PLATE", vbTextCompare) = 0 Then Exit Sub

 

Change "PLATE" to what you use in your sheet metal descriptions.

0 Likes

You'll need something unique about the part to handle this, for example if you have the part document description set to contain "PLATE" you can add a check before continuing the macro per part. add this after "partDoc" is defined.

 

Dim partDoc As PartDocument = oOcc.Definition.Document
If instr(1, partDoc.PropertySets("Design Tracking Properties").item("Description").Expression, "PLATE", vbTextCompare) = 0 Then Exit Sub

 

Change "PLATE" to what you use in your sheet metal descriptions.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report