The below code is (to the best of my limited ability) supposed to cycle through all bodies of a part, and rename the faces of each body if they have not yet been renamed. the code works great when I open a part and run it, all faces on all bodies (works if part is a single body or multibody) get renamed.
the issue arises when I go and add a new body to the part (unmerged) and run the code/macro again. It does not cycle through the new body and its faces. please advise, and help is appreciated. see code below:
Dim SW_App As SldWorks.SldWorks Dim SW_Model As ModelDoc2 Dim Slctn_Mngr As SelectionMgr Dim Slctn_Data As SldWorks.SelectData Dim Slctd_Face As Face2 Dim Slctd_Face_Entity As Entity Dim Slctd_Face_Name As String Dim Crrnt_Cmpnt As SldWorks.PartDoc Dim vSW_Body As Variant Dim Bool_Rtrn As Boolean Dim Body_Vrnt As Variant Dim Cmpnt_Name_S1 As String Dim Cmpnt_Name_S2 As String Dim vBody_Arr As Variant Dim Bodies_Cnt As Integer Dim Face_Cnt As Integer Dim instnc As Long Sub Rename_All_Faces_Main() Set SW_App = Application.SldWorks Set SW_Model = SW_App.ActiveDoc '// test code for renaming all faces. 'Clear selections. SW_Model.ClearSelection2 True 'Get all Bodies Set Crrnt_Cmpnt = SW_Model 'Clear selections. SW_Model.ClearSelection2 True vBody_Arr = Crrnt_Cmpnt.GetBodies2(-1, False) Iterate_Through_Bodies_And_Faces SW_Model, vBody_Arr End Sub Sub Iterate_Through_Bodies_And_Faces(SW_Model As SldWorks.ModelDoc2, vBody_Arr As Variant) Bodies_Cnt = 1 Face_Cnt = 1 For Each vSW_Body In vBody_Arr 'Get count of all faces on body. Set Slctn_Mngr = SW_Model.SelectionManager Set Slctn_Data = Slctn_Mngr.CreateSelectData 'Body_Vrnt = Crrnt_Cmpnt.GetBodies2(swAllBodies, True) 'Set vSW_Body = Body_Vrnt(0) Set Slctd_Face = vSW_Body.GetFirstFace Cmpnt_Name_S1 = Crrnt_Cmpnt.GetTitle Cmpnt_Name_S2 = Replace(Cmpnt_Name_S1, ".SLDPRT", "") 'Debug.Print "Component Name S2 (No Doc Extnsn) = " & Cmpnt_Name_S2 'Clear selections. SW_Model.ClearSelection2 True 'Loop for renaming faces. Do While Not Slctd_Face Is Nothing Set Slctd_Face_Entity = Slctd_Face 'Select using IEntity. Bool_Rtrn = Slctd_Face_Entity.Select4(True, Slctn_Data) 'Check if face already named. Slctd_Face_Name = Crrnt_Cmpnt.GetEntityName(Slctd_Face_Entity) 'Check if face not yet named, skip if already renamed. If Not Slctd_Face_Name = "" Then Face_Cnt = Face_Cnt + 1 Else Bool_Rtrn = SW_Model.SelectedFaceProperties(0, 0, 0, 0, 0, 0, 0, True, Cmpnt_Name_S2 & " - FACE" & Face_Cnt) Face_Cnt = Face_Cnt + 1 End If Set Slctd_Face = Slctd_Face.GetNextFace Loop Bodies_Cnt = Bodies_Cnt + 1 Next vSW_Body End Sub