Would anybody be able to help with my code breaking flat patterns please?

Would anybody be able to help with my code breaking flat patterns please?

sean.boyle77X79
Contributor Contributor
252 Views
5 Replies
Message 1 of 6

Would anybody be able to help with my code breaking flat patterns please?

sean.boyle77X79
Contributor
Contributor

Hello, please would someone be able to explain why my below code would break a flat pattern? My code opens the sheet metal and creates a flat pattern, and measure the thickness to see if the flat pattern has unfolded correctly. However once executing the flat pattern breaks (flanges are folded in the flat pattern view etc.), and the only fix is to delete the flat pattern and make a new one, where the code would then break it if ran again.

 

Here is my code:

If TypeOf oCompDocDef Is SheetMetalComponentDefinition Then
				Dim oSheetMetal As SheetMetalComponentDefinition = oCompDocDef
				
				oCompDoc.Update
				oSheetMetal.Unfold
				oSheetMetal.FlatPattern.Edit
				
								'Check flat pattern has unfolded correctly
								Dim flatPatternThickness As Double = ((oCompDocDef.Parameters.ModelParameters.Item("Thickness").Value) * 10)
								Dim rangeThickness As Double = (oSheetMetal.RangeBox.MaxPoint.Z - oSheetMetal.RangeBox.MinPoint.Z) * 10
														
								If Round(flatPatternThickness) <> Round(rangeThickness) Then
'								MessageBox.Show("Thickness parameter = " & flatPatternThickness & vbCrLf & "Thickness measured in model = " & rangeThickness)
								errorMessage = oCompFileName & " - DXF not exported as the flat pattern did not unfold correctly"
																																oSheetMetal.FlatPattern.ExitEdit
								oCompDoc.Close
								GoTo dxfAbortion
								End If
				
				'Export DXF
				Dim dxfFormat As String = "FLAT PATTERN DXF?AcadVersion=2000" & "&OuterProfileLayer=IV_OUTER_PROFILE&OuterProfileLayerLineType=37633&OuterProfileLayerLineWeight=0,0500&OuterProfileLayerColor=0;0;0" & "&InteriorProfilesLayer=IV_INTERIOR_PROFILES&InteriorProfilesLayerLineType=37633&InteriorProfilesLayerLineWeight=0,0500&InteriorProfilesLayerColor=0;0;0" & "&InvisibleLayers=IV_TANGENT;IV_BEND;IV_Bend_Down;IV_Bend_Up;IV_ARC_CENTERS"
				exportFullPath = exportLocation & "\" & oCompFileNameWithoutExtension & " [" & oCompRevisionNumber.Value & "].dxf"
				oSheetMetal.DataIO.WriteDataToFile(dxfFormat, exportFullPath)
				oSheetMetal.FlatPattern.ExitEdit
				GoTo dxfRepair
				End If
0 Likes
253 Views
5 Replies
Replies (5)
Message 2 of 6

jwingateRJECD
Enthusiast
Enthusiast

So I can't really say what isn't working with your code, but what I can do is offer some different code that was built by someone else here on the forum that has worked well for me that I think should do what you're looking for. Give this a shot and see if it works for you. 

 

 

'get the document this rule is ran from
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oBOM As BOM = oADoc.ComponentDefinition.BOM
'disable row merge for dupiclate part numbers
oBOM.SetPartNumberMergeSettings(False)
'ignore suppressed components so you dont have to open the files in the background
oBOM.HideSuppressedComponentsInBOM = True
oBOM.PartsOnlyViewEnabled = True
'use parts only BOM to get sheet metal parts
Dim oBOMView As BOMView = oBOM.BOMViews.Item("Parts Only")

'dxf format
sOut = "FLAT PATTERN DXF?AcadVersion=R12&OuterProfileLayer=0&InteriorProfilesLayer=0&InvisibleLayers=IV_ARC_CENTERS;IV_BEND_DOWN;IV_BEND;IV_FEATURE_PROFILES&TrimCenterlinesAtContour=True"

For Each oBOMRow As BOMRow In oBOMView.BOMRows
	'sheet metal document sub type
	If Not oBOMRow.ComponentDefinitions(1).Document.SubType.ToString = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For

	Dim qnty As Integer = oBOMRow.ItemQuantity

	'this takes the first document file for the row (should only be 1 unless duplicate part numbers)
	Dim oDoc As PartDocument = oBOMRow.ComponentDefinitions(1).Document

	'try to create flat pattern
	Dim oSMCD As SheetMetalComponentDefinition
	oSMCD = oDoc.ComponentDefinition
	
	If oSMCD.HasFlatPattern = False
		Try
			oSMCD.Unfold
			oSMCD.FlatPattern.ExitEdit
		Catch : Continue For : End Try
	End If
	
	Dim thickness As String = oSMCD.Thickness.Expression
	Dim material As String = oDoc.PropertySets.Item(3).Item("Material").Value
	
	'create file name
	Dim dxfname As String = Left(oDoc.FullFileName, oDoc.FullFileName.Length - 4) & " - " & qnty & " - " & material & " - " & thickness & ".dxf"
	'create dxf
	oSMCD.DataIO.WriteDataToFile(sOut, dxfname)
Next
0 Likes
Message 3 of 6

marcin_otręba
Advisor
Advisor

hi,

 

i would add some check if your doc do not have flatpattern already:

	If Not oSheetMetal.HasFlatPattern Then	oSheetMetal.Unfold

and you can't acces model parameters when you are in flatpattern enviroment  like:

Dim flatPatternThickness As Double = ((oCompDocDef.Parameters.ModelParameters.Item("Thickness").Value) * 10)

so change it to :

Dim flatPatternThickness As Double = ((oCompDocDef.Parameters.ReferenceParameters.Item("Thickness").Value) * 10)

 

i do not know if you set all your variables correctly, but you should get some error message - could you share it ?

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 4 of 6

WCrihfield
Mentor
Mentor

Another option for measuring the thickness of the FlatPattern, which does not rely on what X, Y, or Z axis orientation the resulting flat pattern it in (because that can vary) is to measure between its 'TopFace' and its 'BottomFace', like this example rule below does.  It is converting the default 'database units' (centimeters) to inches, but you can change that to (* 10) instead for converting it to millimeters, if needed.

Sub Main
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then Return
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	If oSMDef.HasFlatPattern Then
		Dim oFP As FlatPattern = oSMDef.FlatPattern
		Dim dMeasuredThickness As Double = 0.0
		Dim oTopPlane As Inventor.Plane = oFP.TopFace.Geometry
		Dim oBottomPlane As Inventor.Plane = oFP.BottomFace.Geometry
		dMeasuredThickness = oTopPlane.DistanceTo(oBottomPlane.RootPoint)
		'converting database units (centimeters) to inches
		dMeasuredThickness = (dMeasuredThickness / 2.54)
		MsgBox("Distance from FlatPattern's 'TopFace' to its 'BottomFace':" & _
		vbCrLf & dMeasuredThickness.ToString(), vbInformation, "FlatPattern Thickness Measurement")
	End If
End Sub

There is also the FlatPattern.OrientedMinimumRangeBox (new in 2024) or the FlatPattern.Body.OrientedMinimumRangeBox (new in 2021), both of which ignore origin axis alignment when measuring its size, usually resulting in more accurate results.  To use those, we can get the OrientedBox's three 'direction lengths' into a List(Of Double), then Sort it, then the smallest value (usually the thickness) will be the first value in the list (at index position of zero, not one).

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 6

marcin_otręba
Advisor
Advisor

Hi,

 

It will work until you do not have any problems in flatpattern.. like:

 

marcin_otrba_0-1750233163725.png

 

then it is better to use flange pattern rangebox in Z direction wchich give you actual size of flatpattern:

 

dMeasuredThickness = oFP.RangeBox.MaxPoint.Z-oFP.RangeBox.MinPoint.Z

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Good point.  Since I design a lot of sheet metal parts, but usually do not need to create flat patterns for them, I pretty much never encounter situations where there is something really odd going on with its flat pattern like that.  Since it seems like the original 'intent' of this discussion was to detect when something is not 'as expected' in the flat pattern, I now see that measuring between those two faces would not identify that issue.  So, using either the regular RangeBox property (as you and the original poster are), or one of the 'oriented' ones, would definitely be a better fit here.

It may be worth pointing out also that in your last example, you are starting from the FlatPattern object itself, while the code posted by the original poster contains a very similar line of code, but that line is using the SheetMetalComponentDefinition object, instead of the FlatPattern object, which may make all the difference.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes