I threw this together but it will need tweaks to match your situation. This base idea is to create a zone to keep (your inside rectangle) and remove anything that is outside that. Use your parameters to set the keep zone and pass those into the TargetAreaMax/Min (I think those are defaulting to cm right now so do math to convert your zone to cm)
Note: I also use the center of the part to find it's place in the assembly. The part may enter or exit the zone... I'm only looking at the center of the part (that might make it feel like its not working at first... just keep playing with is)
Sub VisByLoc()
Dim app As Application
Dim Doc As AssemblyDocument
Dim CompDef As ComponentDefinition
Dim tg As TransientGeometry
Set app = ThisApplication
Set Doc = app.ActiveDocument
Set CompDef = Doc.ComponentDefinition
Set tg = app.TransientGeometry
Dim CenterPoint As Point
Dim TargetAreaMax As Point
Dim TargetAreaMin As Point
Set TargetAreaMax = tg.CreatePoint(100, 150, 100) 'find way to set this yourself
Set TargetAreaMin = tg.CreatePoint(-100, -100, -100) 'find way to set this yourself
For Each occ In CompDef.Occurrences
Set CenterPoint = FindCenterPoint(occ.RangeBox.MaxPoint, occ.RangeBox.MinPoint)
If OutsideArea(CenterPoint, TargetAreaMax, TargetAreaMin) = True Then
Debug.Print (occ.Name & " = Show")
occ.Visible = True
'occ.Excluded = False 'play with this too it may work nice with your BOM and your drawing
Else
Debug.Print (occ.Name & " = Hide")
occ.Visible = False
'occ.Excluded = True 'play with this too
End If
'Debug.Print (occ.Name & " - Length: " & occ.Transformation.Translation.Length & " - X: " & occ.Transformation.Translation.x & " - Y: " & occ.Transformation.Translation.y & " - Z: " & occ.Transformation.Translation.Z)
Next
End Sub
Function OutsideArea(CenterPoint As Point, MaxPoint As Point, MinPoint As Point) As Boolean
Dim OutsideX As Boolean
Dim OutsideY As Boolean
Dim OutsideZ As Boolean
If CenterPoint.x > MaxPoint.x Or CenterPoint.x < MinPoint.x Then
OutsideX = True
End If
If CenterPoint.y > MaxPoint.y Or CenterPoint.y < MinPoint.y Then
OutsideY = True
End If
If CenterPoint.Z > MaxPoint.Z Or CenterPoint.Z < MinPoint.Z Then
OutsideZ = True
End If
If OutsideX + OutsideY + OutsideZ = 0 Then OutsideArea = True Else OutsideArea = False
End Function
Function FindCenterPoint(MaxPoint As Point, MinPoint As Point) As Point
Dim CenterX As Double
Dim CenterY As Double
Dim CenterZ As Double
CenterX = ((MaxPoint.x - MinPoint.x) / 2) + MinPoint.x
CenterY = ((MaxPoint.y - MinPoint.y) / 2) + MinPoint.y
CenterZ = ((MaxPoint.Z - MinPoint.Z) / 2) + MinPoint.Z
Set tg = ThisApplication.TransientGeometry
Set FindCenterPoint = tg.CreatePoint(CenterX, CenterY, CenterZ)
End Function FYI this is VBA code (as is, I don't believe this will run in iLogic)