3 Replies Latest reply on Sep 12, 2014 11:17 AM by Jesse Seger

    Applying name to all faces when revolve contains split sketch

    Suraj Khosla

      I am trying to assign a name to all faces on parts, I found an example which does exactly this:

       

      Option Explicit

      Dim swApp As SldWorks.SldWorks

      Dim Part As SldWorks.ModelDoc2

      Dim swPart As SldWorks.PartDoc

      Dim boolstatus As Boolean

       

      Sub setFaceNames()

      Dim swFace As SldWorks.Face2

      Dim currface As Integer

      Dim retval As Variant

      Dim i As Integer

      Dim obj As Object

       

          Set swApp = Application.SldWorks

          If Not swApp Is Nothing Then

                  Set Part = swApp.ActiveDoc

              If Not Part Is Nothing Then

                  Set swPart = Part

                  If Not swPart Is Nothing Then

                      retval = swPart.GetBodies2(SwConst.swSolidBody, True)

                      For i = 0 To UBound(retval)

                          Set swFace = retval(i).GetFirstFace

                          currface = 1

                          Do While Not swFace Is Nothing

                              swFace.Select False

                              Set obj = Part.SelectionManager.GetSelectedObject5(1)

                              boolstatus = swPart.SetEntityName(swFace, currface)

                              Debug.Print swPart.GetEntityName(swFace)

                              Set swFace = swFace.GetNextFace

                              currface = currface + 1

                          Loop

                      Next i

                  End If

              End If

          End If

          Set swApp = Nothing

      End Sub

       

      This has worked fine for most of the parts however, there is one component which is revolved as two separate closed sketches as shown below:

      swimage.png

      This causes problems as the faces on the left section of the revolved body do not seem to get names applied to them, only the faces on for the section sketched on the right do. I think this may have something to do with the split between two sketches resulting in SW thinking these are two separate bodies?? In which case, how could all faces be named?

          • Re: Applying name to all faces when revolve contains split sketch
            Suraj Khosla

            Please see attached file, macro has been ran on this and can be seen only one half of the model has names applied to the faces.

              • Re: Applying name to all faces when revolve contains split sketch
                Jesse Seger

                Ok I found the issue.  Found this in the help file.

                 

                "If the entity already has a name, then this method does not change the name and returns false.

                This behavior is intended to prevent a program from accidentally renaming an entity that is referenced in some other location. For example, if an assembly contains a mate to a face on a part, then a name is automatically assigned to that face. If you were to change that name, then there is no guarantee that the mate is still valid. Therefore, when using entity names, you should first check to see if the entity is already named, and, if so, use the existing name. If no name exists for the entity, then you can name the entity.

                You can explicitly delete an entity name using the IPartDoc::DeleteEntityName or IPartDoc::IDeleteEntityName method. You then have the option of renaming the item or using that name elsewhere. The method was provided because the action is already available in the core SolidWorks product. However, you should recognize the possibility of reference failures as described."

                 

                The reason it wasn't working on your two body part was that your currface = 1 was in the body loop thus resetting it to 1 after it looped through the first body.  So the entity name was already taken.  I added a line to delete the name.  See the comments below.

                 

                Here is the updated macro.

                 

                Option Explicit

                Dim swApp As SldWorks.SldWorks

                Dim Part As SldWorks.ModelDoc2

                Dim swPart As SldWorks.PartDoc

                Dim boolstatus As Boolean

                 

                Sub setFaceNames()

                    Dim swFace As SldWorks.Face2

                    Dim currface As Integer

                    Dim retval As Variant

                    Dim i As Integer

                    Dim obj As Object

                 

                    Set swApp = Application.SldWorks

                    If Not swApp Is Nothing Then

                            Set Part = swApp.ActiveDoc

                        If Not Part Is Nothing Then

                            Set swPart = Part

                            currface = 1

                            If Not swPart Is Nothing Then

                                retval = swPart.GetBodies2(SwConst.swSolidBody, True)

                                For i = 0 To UBound(retval)

                                    Dim swBody As Body2

                                    Set swBody = retval(i)

                                    Debug.Print swBody.Name

                                    Set swFace = swBody.GetFirstFace

                                 

                                    Do While Not swFace Is Nothing

                                        swFace.Select False

                                        Set obj = Part.SelectionManager.GetSelectedObject5(1)

                                        boolstatus = swPart.DeleteEntityName(swFace)   '************************  remove or comment out if you don't want to accidentally rename your faces in the future.

                                        boolstatus = swPart.SetEntityName(swFace, currface)

                                        Debug.Print boolstatus & " " & swPart.GetEntityName(swFace)

                                        Set swFace = swFace.GetNextFace

                                        currface = currface + 1

                                    Loop

                                Next i

                            End If

                        End If

                    End If

                    Set swApp = Nothing

                End Sub