15 Replies Latest reply on Jan 3, 2017 11:39 AM by Deepak Gupta

    How do you get the name of a sketch?

    Frank Sandoval

      I need to write a macro that will get the name of the current sketch and append it to some text which will then be used as a value for a custom property. I've seen one posting on this and tried it but it doesn't work. Can anyone help?

       

      Thanks

        • Re: How do you get the name of a sketch?
          Solid Air

          Since API calls can change from version to version, what version of SolidWorks are you using?

           

          Edit:  From SW2017

           

          Dim swApp As SldWorks.SldWorks

          Dim swDoc As SldWorks.ModelDoc2

          Dim swSketchMgr As SldWorks.SketchManager

          Dim swActiveSketch As SldWorks.Sketch

           

           

          Sub main()

           

              Set swApp = Application.SldWorks

              Set swDoc = swApp.ActiveDoc

              Set swSketchMgr = swDoc.SketchManager

              Set swActiveSketch = swSketchMgr.ActiveSketch

             

              MsgBox swActiveSketch.Name

             

          End Sub

          • Re: How do you get the name of a sketch?
            Solid Air

            I like this code better (mainly because Feature.Name is documented whereas swSketch.Name is not {as least I could not find it}):

             

            Dim swApp As SldWorks.SldWorks

            Dim swDoc As SldWorks.ModelDoc2

            Dim swActiveSketch As SldWorks.Sketch

            Dim swFeat As SldWorks.Feature

             

            Sub main()

             

                Set swApp = Application.SldWorks

                Set swDoc = swApp.ActiveDoc

                Set swActiveSketch = swDoc.GetActiveSketch2

                Set swFeat = swActiveSketch

               

                MsgBox swFeat.Name

               

            End Sub

              • Re: How do you get the name of a sketch?
                Frank Sandoval

                Thanks for the response. I'm running SW2016. I've tried both versions posted and while they both work in a message box, they don't work within the program I've written. What I've done is made a custom property called OD1 and am trying to use a text box to enter the dimension name (D!,D2, etc)and appending the @ symbol with the dimension name that's entered into the text box  The error I keep getting is "Object variable or with block not set", happening at the line in bold text. Code as follows:

                 

                Private Sub btnok_Click()

                Dim swApp As SldWorks.SldWorks

                Dim swDoc As SldWorks.ModelDoc2

                Dim swSketchMgr As SldWorks.SKETCH

                Dim swfeat As SldWorks.feature

                    Set swApp = Application.SldWorks

                    Set swDoc = swApp.ActiveDoc

                    Set swActiveSketch = swDoc.GetActiveSketch2

                    Set swfeat = swActiveSketch

                 

                Set swModel = swApp.ActiveDoc

                Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

                Set Part = swApp.ActiveDoc

                 

                swCustPropMgr.Set "SERIES", txtseries.Text

                swCustPropMgr.Set "POINT", txtpoint.Text

                swCustPropMgr.Set "DUCTDIA", txtductdia.Text

                swCustPropMgr.Set "DUCTBC", txtductbc.Text

                swCustPropMgr.Set "OD1", txtod1.Text + "@" + swfeat.Name

                 

                Set swApp = Nothing

                Unload Me

                End

                 

                End Sub

                 

                Thanks in advance.