Am having trouble with the attached code related to the attached multibody part. What I am trying to do is run a process (veneering) on a part, which is one side of a piece of furniture. I have four parts, having made one part the only visible one, (hide others command), the code will detect that visible part and detect one of the largest faces to veneer.
The issue is, having cycled through the faces and found the largest face number, and stored that number, when that number is set as the face to veneer the process runs on a different (smaller ) face, despite the number being confirmed ( in a message box), as one of the largest faces. This happens only on parts 3 and 4 in the tree , but is ok on the other two.
I realise there are issues with face numbering, ie what starts off as say face 1, if the part is altered, the number of that face alters.
Hope some one can shed some light on this, as the part hasn’t been altered prior to the process happening.
Thanks in advance.
Solved! Go to Solution.
Solved by bshbsh. Go to Solution.
I would suggest to add attributes to your faces, because if you add faces, your query will indeed fail or result in unexpected behaviour.
After added attributes, you can easily query them!
http://modthemachine.typepad.com/my_weblog/attributes/
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.
Hi BshBsh. Many Thanks for this. Yes it sorted the problem out. I did build this up from various bits of code, samples and otherwise, and it appeared to work to a certain extent. I did realise that the part numbering was out of order, but didnt realise my debugging was giving ambiguous results. Will persevere. I was interested in the other reply to this post, and reading the link to Brian Ekins article in Mod the Machine. Kind Regards
Hi Frederick.. Thanks for your reply and the link to Brian Ekins article. At the outset it does seem like a positive way of referencing an item etc. I think what I am trying to do relates to his mention of finding circular faces and sketching on them. Kind Regards