Hi,
The biggest mistake is: you get n for the visible volume body:
For n = 1 To oCompDef.SurfaceBodies.Count
but then use the same n on extrudefeatures:
Set oFace1 = oCompDef.Features.ExtrudeFeatures.Item(n).Faces.Item(FaceNo)
they have nothing to do with each other. In fact, your "third" volume body is Volume4 (n=4), created by the extrusionfeature Extrusion3. So you look for the index of biggest face on the wrong volume. So you get the wrong FaceNoStore, which you later use for creating the sketch:
Set oFace2 = oCompDef.SurfaceBodies.Item(n).Faces.Item(FaceNoStore)
here you use the correct volume but the wrong faceno.
Public Sub FaceArea()
Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
Set oCompDef = oDoc.ComponentDefinition
Dim num As Long
Dim n As Long
num = oCompDef.SurfaceBodies.Count
For n = 1 To oCompDef.SurfaceBodies.Count
If oCompDef.SurfaceBodies.Item(n).Visible = True Then
MsgBox " Solid No " & n & " of " & num & " items. "
Dim FaceNo As Long
Dim AreaStore As Long
Dim FaceNoStore As Long
'FaceNo = 0 'useless, since the following For initializes it
AreaStore = 0
FaceNoStore = 0
'For FaceNo = 1 To 6 'I got an error at 6 because one of the ExtrusionFeatures only got 5 Faces.
For FaceNo = 1 To oCompDef.SurfaceBodies.Item(n).Faces.Count
Dim oFace1 As Face
Set oFace1 = oCompDef.SurfaceBodies.Item(n).Faces.Item(FaceNo)
MsgBox "Face area of " & FaceNo & " " & oFace1.Evaluator.Area & " cm^2"
If oFace1.Evaluator.Area > AreaStore Then
AreaStore = oFace1.Evaluator.Area
FaceNoStore = FaceNo 'Puts number of face in store -FaceNoStore
End If
Next FaceNo
MsgBox " biggest face is number " & FaceNoStore & " " & FaceNoStore
MsgBox "n = " & n
MsgBox "Face number is " & FaceNoStore
Dim oFace2 As Face
Set oFace2 = oCompDef.SurfaceBodies.Item(n).Faces.Item(FaceNoStore)
MsgBox "Face number is " & FaceNoStore
Dim oSketch1 As PlanarSketch
Set oSketch1 = oCompDef.Sketches.Add(oFace2, True)
Dim oProfile As Profile
Set oProfile = oSketch1.Profiles.AddForSolid
Dim oExtrude As ExtrudeFeature
Set oExtrude = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent(oProfile, 0.2, kNegativeExtentDirection, kCutOperation)
Dim oSideFace1 As Face
Set oSideFace1 = oExtrude.EndFaces.Item(1)
Dim oSketch3 As PlanarSketch
Set oSketch3 = oDoc.ComponentDefinition.Sketches.Add(oSideFace1, True)
Dim oProfile3 As Profile
Set oProfile3 = oSketch3.Profiles.AddForSolid
Set oExtrude = oDoc.ComponentDefinition.Features.ExtrudeFeatures.AddByDistanceExtent(oProfile3, 0.2, kPositiveExtentDirection, kNewBodyOperation)
End If
Next
End Sub
There are a few more things to tidy up.