13 Replies Latest reply on Oct 3, 2014 1:59 PM by Wes Cox

    Re-naming Sketches & Bodies using API

    Nick Hayman

      Afternoon All,

       

      I would like to re-name sketches and bodies within a part.

       

      I do not want to select the sketch and then rename it because i would have to know the name of the sketch in the first place which i do not know. Also, I may not know how many bodies are within the part. I would like to work my way down from the model tree renaming (in a pattern) as i go untill all skecthes and bodies have been renamed.

       

      Bodies (See parts attached):

      The first body will be called A1. The 3rd body will be called A2 and the fifth body will be called A3.

      This will leave the second body renamed as B1 and the fourth as B2 and so on.

       

      I would like this to happen untill all bodies within the part are renamed.

       

      Sketches (See parts attached):

      The skecth within the body will need to be renamed.

      So the sketch in the first body will be called AA1, The third AA2, and the fifth AA3.

      The second as BB1 and fourth as BB2.

       

      ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

       

      Ive attached an example before and after to show what i am looking for.

      I suppose the first thing to ask is can SW rename bodies and skecthes in this way? Rather than selecting by name.

       

       

      Many Thanks in advance.

      Nick

        • Re: Re-naming Sketches & Bodies using API
          Deepak Gupta

          Yes this should be possible.

            • Re: Re-naming Sketches & Bodies using API
              Nick Hayman

              Any ideas how i go about this Deepak?

                  • Re: Re-naming Sketches & Bodies using API
                    Nick Hayman

                    Thanks Deepak,

                     

                    A few issues:

                    1.     It renames everything in the model tree. I want just the Sketches and extrudes to be renamed. Not the folders and planes & origin.

                    2.     It doesnt rename the sketches in the tree.

                    3.     It hasnt renamed the first extrude.

                    4.     Its renamed the bodies as ICE yet i cannot see thisin the code. Have i gone blind?

                     

                    Can you help me with this?

                     

                    Many Thanks

                    Nick

                      • Re: Re-naming Sketches & Bodies using API
                        Deepak Gupta

                        I believe that is something internal. Scroll down in the post and you would see some more updated macros from him which you can try/use.

                          • Re: Re-naming Sketches & Bodies using API
                            Nick Hayman

                            The latest macro sorts out the issues with the default planes and material but not the Sensors or annotations.

                             

                            It also does not rename the sketches within the extrudes on my example part (Before).

                             

                            So we are half way there! But not all the way

                             

                            This is a bit beyond my skills... although i am learning... sloowwwwly.

                             

                            Anything you can do?

                              • Re: Re-naming Sketches & Bodies using API
                                Nick Hayman

                                I only want to rename bodies and sketches. Nothing else.

                                  • Re: Re-naming Sketches & Bodies using API
                                    Ivana Kolin

                                    Dim swApp As SldWorks.SldWorks

                                    Dim swPart As SldWorks.PartDoc

                                    Dim swFeat As SldWorks.Feature

                                    Dim tempFeat As SldWorks.Feature

                                    Dim swFeature As SldWorks.Feature

                                    Dim newName As String

                                    Dim newNameCount As Integer

                                    Dim dicFeatsCount As Object

                                    Dim parents As Variant

                                     

                                     

                                     

                                     

                                    Sub main()

                                     

                                     

                                        Set dicFeatsCount = CreateObject("Scripting.Dictionary")

                                        Set collFeatsNonIncr = New Collection

                                        Set dicBaseNames = CreateObject("Scripting.Dictionary")

                                        isRefGeom = False

                                        '- - - - - - - - - - - - - - - - - - - -

                                        'Add the list of predefined base names

                                        '- - - - - - - - - - - - - - - - - - - -

                                     

                                     

                                        dicBaseNames.Add "Extrusion", "A"

                                        dicBaseNames.Add "Boss", "B"

                                        '- - - - - - - - - - - - - - - - - - - -

                                        Set swApp = Application.SldWorks

                                        Set swPart = swApp.ActiveDoc

                                        Set swFeat = swPart.FirstFeature

                                        While Not swFeat Is Nothing

                                            If dicFeatsCount.exists(swFeat.GetTypeName()) Then

                                                dicFeatsCount.Item(swFeat.GetTypeName()) = dicFeatsCount.Item(swFeat.GetTypeName()) + 1

                                            Else

                                                dicFeatsCount.Add swFeat.GetTypeName(), 1

                                            End If

                                           

                                            If dicBaseNames.exists(swFeat.GetTypeName()) Then

                                             

                                                newName = dicBaseNames.Item(swFeat.GetTypeName())

                                                newNameCount = dicFeatsCount.Item(swFeat.GetTypeName())

                                               

                                                Set tempFeat = swPart.FeatureByName(newName & newNameCount)

                                                If Not tempFeat Is Nothing Then

                                                 tempFeat.Name = swFeat.Name & newName & newNameCount

                                                End If

                                               

                                                swFeat.Name = newName & newNameCount

                                                parents = swFeat.GetParents

                                             

                                                For Each varobj In parents

                                                    Set swFeature = varobj

                                                    If swFeature.GetTypeName = "ProfileFeature" Then

                                                        swFeature.Name = newName & newName & newNameCount

                                                    End If

                                                Next varobj

                                            End If

                                            Set swFeat = swFeat.GetNextFeature

                                        Wend

                                       

                                    End Sub

                                      • Re: Re-naming Sketches & Bodies using API
                                        Nick Hayman

                                        Hi Ivana,

                                         

                                        Thankyou for your reply.

                                        It doesnt quite work. Close but not quite.

                                         

                                        It has renamed the first body as A1 and the sketch within as AA1. It renames the second body as B1 and the sketch within as BB1. This is correct.

                                         

                                        After that it renames the body and sketches as B2 & BB2, B3 & BB3, B4 & BB4 which is incorrect.

                                         

                                        It should be:

                                        A1, AA1

                                        B1, BB1

                                        A2, AA2

                                        B2, BB2

                                        A3, AA3

                                        B3, BB3

                                         

                                        Etc...

                                         

                                        Very close! Can you help further?

                                          • Re: Re-naming Sketches & Bodies using API
                                            Nick Hayman

                                            Id also like the option to change the letters within the macro.

                                             

                                            This is an example i might require::

                                            F1, FL1

                                            HL1, WHL1

                                            F2, FL2

                                            HL2, WHL2

                                            F3, FL3

                                            HL3, WHL3

                                             

                                            Is this achieveable?

                                              • Re: Re-naming Sketches & Bodies using API
                                                Ivana Kolin

                                                Option Explicit

                                                Dim swApp As SldWorks.SldWorks

                                                Dim swPart As SldWorks.PartDoc

                                                Dim swFeat As SldWorks.Feature

                                                Dim tempFeat As SldWorks.Feature

                                                Dim swFeature As SldWorks.Feature

                                                Dim newName As String

                                                Dim newNameCount As Integer

                                                Dim newSketchName As String

                                                Dim FLCount As Integer

                                                Dim HLCount As Integer

                                                Dim varobj As Variant

                                                Dim parents As Variant

                                                 

                                                 

                                                 

                                                 

                                                Sub main()

                                                 

                                                 

                                                    Set swApp = Application.SldWorks

                                                    Set swPart = swApp.ActiveDoc

                                                    Set swFeat = swPart.FirstFeature

                                                   

                                                    FLCount = 1

                                                    HLCount = 1

                                                   

                                                    While Not swFeat Is Nothing

                                                 

                                                 

                                                        Select Case swFeat.GetTypeName

                                                        Case "Extrusion", "Boss"

                                                           

                                                            If FLCount = HLCount Then

                                                                newName = "F"

                                                                newSketchName = "FL"

                                                                newNameCount = FLCount

                                                                FLCount = FLCount + 1

                                                            Else

                                                                newName = "HL"

                                                                newSketchName = "WHL"

                                                                newNameCount = HLCount

                                                                HLCount = FLCount

                                                            End If

                                                           

                                                            Set tempFeat = swPart.FeatureByName(newName & newNameCount)

                                                           

                                                            If Not tempFeat Is Nothing Then

                                                             tempFeat.Name = swFeat.Name & "<renamed>"

                                                            End If

                                                           

                                                            swFeat.Name = newName & newNameCount

                                                            parents = swFeat.GetParents

                                                 

                                                 

                                                            For Each varobj In parents

                                                                Set swFeature = varobj

                                                                If swFeature.GetTypeName = "ProfileFeature" Then

                                                                    swFeature.Name = newSketchName & newNameCount

                                                                End If

                                                            Next varobj

                                                 

                                                 

                                                        Case Else

                                                            Debug.Print swFeat.GetTypeName

                                                        End Select

                                                        Set swFeat = swFeat.GetNextFeature

                                                       

                                                    

                                                    Wend

                                                   

                                                End Sub

                            • Re: Re-naming Sketches & Bodies using API
                              Nick Hayman

                              Thanks to all.

                               

                              Perfect!

                                • Re: Re-naming Sketches & Bodies using API
                                  Wes Cox

                                  For some reason when I try to run this (Correct answer from Ivana), nothing happens-- am I missing something painfully obvious? Yes, got it. I've tried creating this several times with no run. SW2010. Do I need some other references or something else enabled? Does anyone have a copy of this macro working that they'd be willing to share? Thank you!

                                   

                                  I expected this macro to rename bodies, but from what I'm seeing it only renames features. I was running it on an imported part that only had imported bodies in the tree, and the macro was having no effect. Once I tried it on a part that contained features, I saw the macro was running fine.

                                   

                                  I'm trying to rename all bodies of an imported part, without altering the name of the folders, planes etc at top of tree.

                                   

                                  This macro is close, except that it renames the default folders etc. http://eng-tips.com/viewthread.cfm?qid=250003

                                   

                                   

                                   

                                  Boom! Got it-- excited, this is one of the more difficult edits I've made yet, it just didn't make sense to me for a while:

                                   

                                  ------

                                   

                                   

                                  Dim swApp As SldWorks.SldWorks

                                  Dim swPart As SldWorks.PartDoc

                                  Dim swFeat As SldWorks.Feature

                                  Dim newName As String

                                   

                                   

                                  Dim dicFeatsCount As Object

                                   

                                   

                                  Dim collFeatsNonIncr As Collection

                                  Dim dicBaseNames As Object

                                   

                                   

                                  Const FrontPlane = "Front Plane"

                                  Const TopPlane = "Top Plane"

                                  Const RigthPlane = "Right Plane"

                                  Const Origin = "Origin"

                                  Const DetailCabinet = "Annotations"

                                  Dim isRefGeom As Boolean

                                   

                                   

                                  Sub main()

                                   

                                   

                                      Set dicFeatsCount = CreateObject("Scripting.Dictionary")

                                     

                                      Set collFeatsNonIncr = New Collection

                                     

                                      Set dicBaseNames = CreateObject("Scripting.Dictionary")

                                     

                                      isRefGeom = False

                                     

                                      'Add the list of features which shouldn't be incremented

                                      '- - - - - - - - - - - - - - - - - - - -

                                      collFeatsNonIncr.Add "SensorFolder"

                                      collFeatsNonIncr.Add "DocsFolder"

                                      collFeatsNonIncr.Add "DetailCabinet"

                                      collFeatsNonIncr.Add "MaterialFolder"

                                      collFeatsNonIncr.Add "OriginProfileFeature"

                                      collFeatsNonIncr.Add "EqnFolder"

                                      collFeatsNonIncr.Add "EnvFolder"

                                     

                                      '- - - - - - - - - - - - - - - - - - - -

                                     

                                      'Add the list of predefined base names

                                      '- - - - - - - - - - - - - - - - - - - -

                                      dicBaseNames.Add "MaterialFolder", "Material <not specified>"

                                      dicBaseNames.Add "OriginProfileFeature", "Origin"

                                      dicBaseNames.Add "ProfileFeature", "Sketch"

                                      dicBaseNames.Add "Extrusion", "Extrude"

                                      dicBaseNames.Add "RefPlane", "Plane"

                                      dicBaseNames.Add "DetailCabinet", "Annotations"

                                      dicBaseNames.Add "EqnFolder", "Equations"

                                      dicBaseNames.Add "EnvFolder", "Lights, Cameras & Scenes"

                                      '- - - - - - - - - - - - - - - - - - - -

                                   

                                   

                                      Set swApp = Application.SldWorks

                                     

                                      Set swPart = swApp.ActiveDoc

                                     

                                      Set swFeat = swPart.FirstFeature

                                     

                                      While Not swFeat Is Nothing

                                         

                                          If dicFeatsCount.exists(swFeat.GetTypeName2()) Then

                                              dicFeatsCount.Item(swFeat.GetTypeName2()) = dicFeatsCount.Item(swFeat.GetTypeName2()) + 1

                                          Else

                                              dicFeatsCount.Add swFeat.GetTypeName2(), 1

                                          End If

                                         

                                          If dicBaseNames.exists(swFeat.GetTypeName2()) Then

                                              newName = dicBaseNames.Item(swFeat.GetTypeName2())

                                          Else

                                              newName = swFeat.GetTypeName2()

                                          End If

                                         

                                          Dim i As Integer

                                         

                                          Dim isIncremented As Boolean

                                          isIncremented = True

                                          For i = 1 To collFeatsNonIncr.Count

                                              If collFeatsNonIncr(i) = swFeat.GetTypeName2() Then

                                                  isIncremented = False

                                                  Exit For

                                              End If

                                          Next

                                         

                                          If isIncremented Then

                                              newName = newName & dicFeatsCount.Item(swFeat.GetTypeName2())

                                          End If

                                         

                                          If swFeat.GetTypeName2 = "MaterialFolder" Then

                                             

                                              isRefGeom = True

                                             

                                              Dim sMatName As String

                                             

                                              sMatName = swPart.GetMaterialPropertyName2("", "")

                                             

                                              If sMatName <> "" Then

                                                  newName = sMatName

                                              End If

                                             

                                          End If

                                         

                                          swFeat.Name = newName

                                         

                                          Set swFeat = swFeat.GetNextFeature

                                         

                                          If isRefGeom Then

                                             

                                              swFeat.Name = FrontPlane

                                             

                                              Set swFeat = swFeat.GetNextFeature

                                              swFeat.Name = TopPlane

                                             

                                              Set swFeat = swFeat.GetNextFeature

                                              swFeat.Name = RigthPlane

                                             

                                              Set swFeat = swFeat.GetNextFeature

                                              swFeat.Name = Origin

                                                 

                                              Set swFeat = swFeat.GetNextFeature

                                              isRefGeom = False

                                          End If

                                         

                                      Wend

                                     

                                  End Sub