By Adam Nagy
If you want to get the extents of a ComponentOccurrence then you can use its RangeBox for that.
This will provide the information in the coordinate system of the top assembly:
Sub AddPoints1( _ oCD As AssemblyComponentDefinition, oOcc As ComponentOccurrence) Dim pt1 As Point Set pt1 = oOcc.RangeBox.MinPoint Dim pt2 As Point Set pt2 = oOcc.RangeBox.MaxPoint Call oCD.WorkPoints.AddFixed(pt1) Call oCD.WorkPoints.AddFixed(pt2) End Sub Sub ComponentExtents1() Dim oDoc As AssemblyDocument Set oDoc = ThisApplication.ActiveDocument Dim oCD As AssemblyComponentDefinition Set oCD = oDoc.ComponentDefinition Dim oOcc As ComponentOccurrence For Each oOcc In oCD.Occurrences.AllLeafOccurrences Call AddPoints1(oCD, oOcc) Next End Sub
If you want to get the extents in the part's or subassembly's coordinate system instead then you just have to drill down to the ComponentDefinition used by the occurrence, which will also have a RangeBox property. Then to show those points in the top assemblies coordinate system, you just have to transform them based on the occurrence's Transformation property:
Sub AddPoints2( _ oCD As AssemblyComponentDefinition, oOcc As ComponentOccurrence) Dim oRB As Box Set oRB = oOcc.Definition.RangeBox Dim pt1 As Point Set pt1 = oRB.MinPoint Call pt1.TransformBy(oOcc.Transformation) Dim pt2 As Point Set pt2 = oRB.MaxPoint Call pt2.TransformBy(oOcc.Transformation) Call oCD.WorkPoints.AddFixed(pt1) Call oCD.WorkPoints.AddFixed(pt2) End Sub Sub ComponentExtents2() Dim oDoc As AssemblyDocument Set oDoc = ThisApplication.ActiveDocument Dim oCD As AssemblyComponentDefinition Set oCD = oDoc.ComponentDefinition Dim oOcc As ComponentOccurrence For Each oOcc In oCD.Occurrences.AllLeafOccurrences Call AddPoints2(oCD, oOcc) Next End Sub
You could also get the extents in an arbitrary coordinate system, e.g. one defined by a UserCoordinateSystem object placed inside the part document. In this case we can use TransientBRep to transform the SurfaceBodies of the parts as also shown in this forum thread:
Sub GetRangePoints3( _ oOcc As ComponentOccurrence, pt1 As Point, pt2 As Point) Dim oUCS As UserCoordinateSystem On Error Resume Next Set oUCS = oOcc.Definition.UserCoordinateSystems("UCS") On Error GoTo 0 ' If the part does not have a UserCoordinateSystem object ' named "UCS" then we just use the part's coordinate system If oUCS Is Nothing Then Set pt1 = oOcc.Definition.RangeBox.MinPoint Set pt2 = oOcc.Definition.RangeBox.MaxPoint Exit Sub End If Dim oTB As TransientBRep Set oTB = ThisApplication.TransientBRep Dim oUT As Matrix Set oUT = oUCS.Transformation Call oUT.Invert Dim oSB As SurfaceBody Dim oTSB As SurfaceBody Dim oRB As Box For Each oSB In oOcc.Definition.SurfaceBodies Set oTSB = oTB.Copy(oSB) Call oTB.Transform(oTSB, oUT) If oRB Is Nothing Then Set oRB = oTSB.RangeBox Else Call oRB.Extend(oTSB.RangeBox.MinPoint) Call oRB.Extend(oTSB.RangeBox.MaxPoint) End If Next ' Transform points back to the part coordinate system Call oUT.Invert Set pt1 = oRB.MinPoint Call pt1.TransformBy(oUT) Set pt2 = oRB.MaxPoint Call pt2.TransformBy(oUT) End Sub Sub AddPoints3( _ oCD As AssemblyComponentDefinition, oOcc As ComponentOccurrence) Dim pt1 As Point Dim pt2 As Point Call GetRangePoints3(oOcc, pt1, pt2) Call pt1.TransformBy(oOcc.Transformation) Call pt2.TransformBy(oOcc.Transformation) Call oCD.WorkPoints.AddFixed(pt1) Call oCD.WorkPoints.AddFixed(pt2) End Sub Sub ComponentExtents3() Dim oDoc As AssemblyDocument Set oDoc = ThisApplication.ActiveDocument Dim oCD As AssemblyComponentDefinition Set oCD = oDoc.ComponentDefinition Dim oOcc As ComponentOccurrence For Each oOcc In oCD.Occurrences.AllLeafOccurrences Call AddPoints3(oCD, oOcc) Next End Sub
The following article might also come handy in understanding the component transformations:
http://adndevblog.typepad.com/manufacturing/2013/07/occurrences-contexts-definitions-proxies.html