Holes counting and reflecting the results to a drawing

Holes counting and reflecting the results to a drawing

Ahmed.shawkyXTZHN
Enthusiast Enthusiast
767 Views
6 Replies
Message 1 of 7

Holes counting and reflecting the results to a drawing

Ahmed.shawkyXTZHN
Enthusiast
Enthusiast

Hi all , I have the below code from Jhoel special thanks to  him , but I have two things pending ,

1- this code is counting holes and punches for assembly with part level , I wanted to be assembly with subassembly level then parts ,

2 - get the result's in table in the drawing.

 

"

 

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oHoles As New List(Of String)
Dim oPunches As New List(Of String)
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
For Each oOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences
	If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso oOcc.Definition.Document.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
		Dim oDoc As PartDocument = oOcc.Definition.Document

		Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
		If oCD.HasFlatPattern = False Then
			oCD.Unfold
			oCD.FlatPattern.ExitEdit
			oDoc.Close
			oAsm.Activate
		End If

		'Add all holes (diameters) to a list

		Dim oFace As Face = oCD.FlatPattern.TopFace
		For Each oLoop As EdgeLoop In oFace.EdgeLoops
			'Make sure the edgeloop is a circle
			If oLoop.IsOuterEdgeLoop = False AndAlso oLoop.Edges.Count = 1 _
				AndAlso oLoop.Edges(1).GeometryType = CurveTypeEnum.kCircleCurve
				oHoles.Add(UoM.GetStringFromValue(oLoop.Edges(1).Geometry.Radius * 2, UoM.LengthUnits))
			End If
		Next
		For Each oPunch As PunchToolFeature In oCD.Features.PunchToolFeatures
			For i = 1 To oPunch.PunchCenterPoints.Count
				oPunches.Add(System.IO.Path.GetFileNameWithoutExtension(oPunch.iFeatureTemplateDescriptor.LastKnownSourceFileName))
			Next
		Next
	End If
Next
For Each oProp As Inventor.Property In oAsm.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	If oProp.Name.StartsWith("Hole: ") Or oProp.Name.StartsWith("Punch: ") Then oProp.Delete
Next
'Sort the list
oHoles.Sort(Function(a, b) UoM.GetValueFromExpression(a, UoM.LengthUnits).CompareTo(UoM.GetValueFromExpression(b, UoM.LengthUnits)))
'Step through the list and create properties of diameter and count.
Dim currCount As Integer = 0
For i = 0 To oHoles.Count - 1
	If i > 0 AndAlso oHoles(i) <> oHoles(i - 1)
		iProperties.Value("Custom", "Hole: " & oHoles(i - 1)) = currCount
		MsgBox(oHoles(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oHoles.Count - 1
		MsgBox(oHoles(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Hole: " & oHoles(i)) = currCount
	End If
Next
oPunches.Sort(Function(a, b) a.CompareTo(b))
currCount = 0
For i = 0 To oPunches.Count - 1
	If i > 0 AndAlso oPunches(i) <> oPunches(i - 1)
		iProperties.Value("Custom", "Punch: " & oPunches(i - 1)) = currCount
		MsgBox(oPunches(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oPunches.Count - 1
		MsgBox(oPunches(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Punch: " & oPunches(i)) = currCount
	End If
Next
oAsm.Update





"

Thanks.

 

0 Likes
Accepted solutions (1)
768 Views
6 Replies
Replies (6)
Message 2 of 7

JhoelForshav
Mentor
Mentor

Hi @Ahmed.shawkyXTZHN 

You can use AllReferencedDocuments to dig through the subassemblies as well:

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oHoles As New List(Of String)
Dim oPunches As New List(Of String)
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso TypeOf (oDoc.ComponentDefinition) Is SheetMetalComponentDefinition
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			Dim occCount As Integer = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count
			Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
			If oCD.HasFlatPattern = False Then
				oCD.Unfold
				oCD.FlatPattern.ExitEdit
				oDoc.Close
				oAsm.Activate
			End If
			Dim oFace As Face = oCD.FlatPattern.TopFace
			For Each oLoop As EdgeLoop In oFace.EdgeLoops
				If oLoop.IsOuterEdgeLoop = False AndAlso oLoop.Edges.Count = 1 _
					AndAlso oLoop.Edges(1).GeometryType = CurveTypeEnum.kCircleCurve
					For j = 1 To occCount
					oHoles.Add(UoM.GetStringFromValue(oLoop.Edges(1).Geometry.Radius * 2, UoM.LengthUnits))
					Next
				End If
			Next
			For Each oPunch As PunchToolFeature In oCD.Features.PunchToolFeatures
				For j = 1 To occCount
				For i = 1 To oPunch.PunchCenterPoints.Count
					oPunches.Add(System.IO.Path.GetFileNameWithoutExtension(oPunch.iFeatureTemplateDescriptor.LastKnownSourceFileName))
				Next
				Next
			Next
		End If
	End If
Next
For Each oProp As Inventor.Property In oAsm.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	If oProp.Name.StartsWith("Hole: ") Or oProp.Name.StartsWith("Punch: ") Then oProp.Delete
Next
oHoles.Sort(Function(a, b) UoM.GetValueFromExpression(a, UoM.LengthUnits).CompareTo(UoM.GetValueFromExpression(b, UoM.LengthUnits)))
Dim currCount As Integer = 0
For i = 0 To oHoles.Count - 1
	If i > 0 AndAlso oHoles(i) <> oHoles(i - 1)
		iProperties.Value("Custom", "Hole: " & oHoles(i - 1)) = currCount
		MsgBox(oHoles(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oHoles.Count - 1
		MsgBox(oHoles(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Hole: " & oHoles(i)) = currCount
	End If
Next
oPunches.Sort(Function(a, b) a.CompareTo(b))
currCount = 0
For i = 0 To oPunches.Count - 1
	If i > 0 AndAlso oPunches(i) <> oPunches(i - 1)
		iProperties.Value("Custom", "Punch: " & oPunches(i - 1)) = currCount
		MsgBox(oPunches(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oPunches.Count - 1
		MsgBox(oPunches(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Punch: " & oPunches(i)) = currCount
	End If
Next
oAsm.Update
Message 3 of 7

Ahmed.shawkyXTZHN
Enthusiast
Enthusiast

@JhoelForshav  i tried the above code it gives me the below error 

 

"Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"

0 Likes
Message 4 of 7

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @Ahmed.shawkyXTZHN 

Maybe you have some bad sheet metal parts in your assembly that can't be unfolded?

Try this:

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oHoles As New List(Of String)
Dim oPunches As New List(Of String)
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso TypeOf (oDoc.ComponentDefinition) Is SheetMetalComponentDefinition
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			Dim occCount As Integer = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count
			Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
			Try
			If oCD.HasFlatPattern = False Then
				oCD.Unfold
				oCD.FlatPattern.ExitEdit
				oDoc.Close
				oAsm.Activate
			End If
			Catch
			MsgBox("cannot unfold " & oDoc.DisplayName)
			oDoc.Close
			Continue For
			End Try
			Dim oFace As Face = oCD.FlatPattern.TopFace
			For Each oLoop As EdgeLoop In oFace.EdgeLoops
				If oLoop.IsOuterEdgeLoop = False AndAlso oLoop.Edges.Count = 1 _
					AndAlso oLoop.Edges(1).GeometryType = CurveTypeEnum.kCircleCurve
					For j = 1 To occCount
					oHoles.Add(UoM.GetStringFromValue(oLoop.Edges(1).Geometry.Radius * 2, UoM.LengthUnits))
					Next
				End If
			Next
			For Each oPunch As PunchToolFeature In oCD.Features.PunchToolFeatures
				For j = 1 To occCount
				For i = 1 To oPunch.PunchCenterPoints.Count
					oPunches.Add(System.IO.Path.GetFileNameWithoutExtension(oPunch.iFeatureTemplateDescriptor.LastKnownSourceFileName))
				Next
				Next
			Next
		End If
	End If
Next
For Each oProp As Inventor.Property In oAsm.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	If oProp.Name.StartsWith("Hole: ") Or oProp.Name.StartsWith("Punch: ") Then oProp.Delete
Next
oHoles.Sort(Function(a, b) UoM.GetValueFromExpression(a, UoM.LengthUnits).CompareTo(UoM.GetValueFromExpression(b, UoM.LengthUnits)))
Dim currCount As Integer = 0
For i = 0 To oHoles.Count - 1
	If i > 0 AndAlso oHoles(i) <> oHoles(i - 1)
		iProperties.Value("Custom", "Hole: " & oHoles(i - 1)) = currCount
		MsgBox(oHoles(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oHoles.Count - 1
		MsgBox(oHoles(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Hole: " & oHoles(i)) = currCount
	End If
Next
oPunches.Sort(Function(a, b) a.CompareTo(b))
currCount = 0
For i = 0 To oPunches.Count - 1
	If i > 0 AndAlso oPunches(i) <> oPunches(i - 1)
		iProperties.Value("Custom", "Punch: " & oPunches(i - 1)) = currCount
		MsgBox(oPunches(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oPunches.Count - 1
		MsgBox(oPunches(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Punch: " & oPunches(i)) = currCount
	End If
Next
oAsm.Update
0 Likes
Message 5 of 7

Ahmed.shawkyXTZHN
Enthusiast
Enthusiast

@JhoelForshav  hello Jhoel , I have extra challenge  😊, can we get the result of the punch by punch ID not punch name

as i have one punch name but under this punch there is 10 punch ID with different hole sizes and properties , thanks.

0 Likes
Message 6 of 7

JhoelForshav
Mentor
Mentor

Hi @Ahmed.shawkyXTZHN 

I don't have any punches myself with different ID to try on so I haven't tried this code but it should work. If the punch doesn't have an ID it uses the name, otherwise it uses the ID.

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oHoles As New List(Of String)
Dim oPunches As New List(Of String)
Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
For Each oDoc As Document In oAsm.AllReferencedDocuments
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso TypeOf (oDoc.ComponentDefinition) Is SheetMetalComponentDefinition
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			Dim occCount As Integer = oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count
			Dim oCD As SheetMetalComponentDefinition = oDoc.ComponentDefinition
			Try
				If oCD.HasFlatPattern = False Then
					oCD.Unfold
					oCD.FlatPattern.ExitEdit
					oDoc.Close
					oAsm.Activate
				End If
			Catch
				MsgBox("cannot unfold " & oDoc.DisplayName)
				oDoc.Close
				Continue For
			End Try
			Dim oFace As Face = oCD.FlatPattern.TopFace
			For Each oLoop As EdgeLoop In oFace.EdgeLoops
				If oLoop.IsOuterEdgeLoop = False AndAlso oLoop.Edges.Count = 1 _
					AndAlso oLoop.Edges(1).GeometryType = CurveTypeEnum.kCircleCurve
					For j = 1 To occCount
						oHoles.Add(UoM.GetStringFromValue(oLoop.Edges(1).Geometry.Radius * 2, UoM.LengthUnits))
					Next
				End If
			Next
			For Each oPunch As PunchToolFeature In oCD.Features.PunchToolFeatures
				Dim PunchID As String = oPunch.iFeatureDefinition.PunchId
				For j = 1 To occCount
					For i = 1 To oPunch.PunchCenterPoints.Count
						If PunchID <> ""
							oPunches.Add(PunchID)
						Else
							oPunches.Add(System.IO.Path.GetFileNameWithoutExtension(oPunch.iFeatureTemplateDescriptor.LastKnownSourceFileName))
						End If
					Next
				Next
			Next
		End If
	End If
Next
For Each oProp As Inventor.Property In oAsm.PropertySets("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}")
	If oProp.Name.StartsWith("Hole: ") Or oProp.Name.StartsWith("Punch: ") Then oProp.Delete
Next
oHoles.Sort(Function(a, b) UoM.GetValueFromExpression(a, UoM.LengthUnits).CompareTo(UoM.GetValueFromExpression(b, UoM.LengthUnits)))
Dim currCount As Integer = 0
For i = 0 To oHoles.Count - 1
	If i > 0 AndAlso oHoles(i) <> oHoles(i - 1)
		iProperties.Value("Custom", "Hole: " & oHoles(i - 1)) = currCount
		MsgBox(oHoles(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oHoles.Count - 1
		MsgBox(oHoles(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Hole: " & oHoles(i)) = currCount
	End If
Next
oPunches.Sort(Function(a, b) a.CompareTo(b))
currCount = 0
For i = 0 To oPunches.Count - 1
	If i > 0 AndAlso oPunches(i) <> oPunches(i - 1)
		iProperties.Value("Custom", "Punch: " & oPunches(i - 1)) = currCount
		MsgBox(oPunches(i - 1) & vbCrLf & "Count = " & currCount)
		currCount = 1
	Else
		currCount += 1
	End If
	If i = oPunches.Count - 1
		MsgBox(oPunches(i) & vbCrLf & "Count = " & currCount)
		iProperties.Value("Custom", "Punch: " & oPunches(i)) = currCount
	End If
Next
oAsm.Update

An Idea could be to use a combination of name and ID instead, but as it is now it's only using the ID if it exists.

0 Likes
Message 7 of 7

Ahmed.shawkyXTZHN
Enthusiast
Enthusiast

Hi @JhoelForshav , regarding the above code for counting holes and punches , can we link it to custom property so we can get the punch and hole part wise in the cutting list , thanks.

0 Likes