Getting a bounding box from occurrence name.

Getting a bounding box from occurrence name.

SharkDesign
Mentor Mentor
828 Views
4 Replies
Message 1 of 5

Getting a bounding box from occurrence name.

SharkDesign
Mentor
Mentor

Is there a way to get the bounding box measurements from just an occurrence name, or do I have to open each part to get it?

 

I'm using this:

Model_X=Measure.ExtentsLength 'X-axis BB length
Model_Y=Measure.ExtentsWidth  'Y-axis BB length
Model_Z=Measure.ExtentsHeight 'Z-axis BB length
  Inventor Certified Professional
0 Likes
Accepted solutions (2)
829 Views
4 Replies
Replies (4)
Message 2 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @SharkDesign 

Are you looking for something like this? 🙂

Sub Main
	Dim oOccName As String = InputBox("Occurrence name:", "Get range box data", "Part1:1")
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oOcc As ComponentOccurrence = Nothing
	For Each oDoc As Document In oAsm.AllReferencedDocuments
		For Each sOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
			If sOcc.Name = oOccName
				oOcc = sOcc
				Exit For
			End If
		Next
		If oOcc IsNot Nothing Then Exit For
	Next
	If oOcc IsNot Nothing Then
		Dim oRB As Double() = GetRangeBoxData(oOcc, oAsm)
		MsgBox(oOccName & " rangebox:" & vbCrLf & _
		"X: " & oRB(0) & vbCrLf & _
		"Y: " & oRB(1) & vbCrLf & _
		"Z: " & oRB(2))
	Else
		MsgBox("No occurrence with name: " & oOccName & " in this assembly.")
	End If
End Sub

Function GetRangeBoxData(ByVal oOcc As ComponentOccurrence, oAsm As AssemblyDocument) As Double()
	Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
	Dim returnVal(2) As Double
	Dim oBox As Box = oOcc.RangeBox
	returnval(0) = UoM.ConvertUnits(Math.Abs(oBox.MaxPoint.X - oBox.MinPoint.X), UnitsTypeEnum.kDatabaseLengthUnits, UoM.LengthUnits)
	returnval(1) = UoM.ConvertUnits(Math.Abs(oBox.MaxPoint.Y - oBox.MinPoint.Y), UnitsTypeEnum.kDatabaseLengthUnits, UoM.LengthUnits)
	returnval(2) = UoM.ConvertUnits(Math.Abs(oBox.MaxPoint.Z - oBox.MinPoint.Z), UnitsTypeEnum.kDatabaseLengthUnits, UoM.LengthUnits)

	Return returnVal
End Function
Message 3 of 5

SharkDesign
Mentor
Mentor

Thanks! That's mint!

Knew it wouldn't be as simple a single line...

  Inventor Certified Professional
Message 4 of 5

SharkDesign
Mentor
Mentor

I've just realised that this takes the Bounding Box from the UCS of the assembly and not the part. So if the part is on a slight angle, the bounding box is wrong. 

Is there a way to get the BB at part level?

 

 

  Inventor Certified Professional
0 Likes
Message 5 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @SharkDesign 

That's correct. Try this instead to get the parts rangebox instead of the occurrences rangebox in the assembly environment 🙂

Sub Main
	Dim oOccName As String = InputBox("Occurrence name:", "Get range box data", "Part1:1")
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim oOcc As ComponentOccurrence = Nothing
	Dim UoM As UnitsOfMeasure = oAsm.UnitsOfMeasure
	For Each oDoc As Document In oAsm.AllReferencedDocuments
		For Each sOcc As ComponentOccurrence In oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc)
			If sOcc.Name = oOccName
				oOcc = sOcc
				Exit For
			End If
		Next
		If oOcc IsNot Nothing Then Exit For
	Next
	If oOcc IsNot Nothing Then
		Dim oBox As Box = oOcc.Definition.RangeBox
		MsgBox(oOccName & " rangebox:" & vbCrLf & _
		"X: " & UoM.ConvertUnits(Abs(oBox.MaxPoint.X - oBox.MinPoint.X), UnitsTypeEnum.kDatabaseLengthUnits, UoM.LengthUnits) & vbCrLf & _
		"Y: " & UoM.ConvertUnits(Abs(oBox.MaxPoint.Y - oBox.MinPoint.Y), UnitsTypeEnum.kDatabaseLengthUnits, UoM.LengthUnits) & vbCrLf & _
		"Z: " & UoM.ConvertUnits(Abs(oBox.MaxPoint.Z - oBox.MinPoint.Z), UnitsTypeEnum.kDatabaseLengthUnits, UoM.LengthUnits))
	Else
		MsgBox("No occurrence with name: " & oOccName & " in this assembly.")
	End If
End Sub