edited

edited

haphanthanhtam.work
Enthusiast Enthusiast
469 Views
2 Replies
Message 1 of 3

edited

haphanthanhtam.work
Enthusiast
Enthusiast

edit

0 Likes
Accepted solutions (1)
470 Views
2 Replies
Replies (2)
Message 2 of 3

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @haphanthanhtam.work . I also use the RangeBox function in my work, but I use OrientedBox. Its advantages are that it always shows the minimum size, but it only works for bodies, so it should not be used for assemblies or multibody parts. Try this code, it has a check if your component is a part and measures the OrientedBox for the first body in the part.

Public Sub Main() 
	' Get the active assembly. 
	Dim oAsmDoc As AssemblyDocument 
	oAsmDoc = ThisApplication.ActiveDocument
	
	Dim oUnit As UnitsOfMeasure = ThisApplication.UnitsOfMeasure
	
	' Get the assembly component definition. 
	Dim oAsmDef As AssemblyComponentDefinition 
	oAsmDef = oAsmDoc.ComponentDefinition 
	
	' Get all of the leaf occurrences of the assembly. 
	Dim oLeafOccs As ComponentOccurrencesEnumerator 
	oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences	
	
	' Iterate through all part occurrences in assembly. 
	Dim oOcc As ComponentOccurrence 
	For Each oOcc In oLeafOccs 
		If TypeOf oOcc.Definition.Document Is PartDocument Then
			Dim oBody As SurfaceBody = oOcc.Definition.SurfaceBodies.Item(1)
			Dim oBox As OrientedBox = oBody.OrientedMinimumRangeBox
			L = oUnit.ConvertUnits(oBox.DirectionOne.Length, "cm", "mm")
			W = oUnit.ConvertUnits(oBox.DirectionTwo.Length, "cm", "mm")
			H = oUnit.ConvertUnits(oBox.DirectionThree.Length, "cm", "mm")
			
			Dim oList As New ArrayList 
			oList.Add(L)
			oList.Add(W)
			oList.Add(H)
			
			Call oList.Sort()
			
			iProperties.Value(oOcc.Name,"Custom", "Length") =  Round(oList.Item(2),0)& " mm"
			iProperties.Value(oOcc.Name,"Custom", "Width") = Round(oList.Item(1),0)& " mm"
			iProperties.Value(oOcc.Name, "Custom", "BoxThickness") = Round(oList.Item(0),1) & " mm"	
		End If
	Next 
End Sub

In any case, if you need the dimensions of assemblies and multibody parts, you can combine your and my version of the code.

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 3

haphanthanhtam.work
Enthusiast
Enthusiast

Thank you!!!! @Andrii_Humeniuk 
It helps me get my work done quickly

0 Likes