4 Replies Latest reply on Sep 17, 2018 5:44 PM by David Mandl

    How to add a dimension to a Sketch?

    Diego Saenz

      I recorded this simple macro which creates 2 lines and adds a dimension to one of them, yet I cannot run it. Each time I try to run the macro I get "Run-time error '91': Object variable or With block variable not set".
      What's happening?

       

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = Application.SldWorks

       

      Set Part = swApp.ActiveDoc

      Dim COSMOSWORKSObj As Object

      Dim CWAddinCallBackObj As Object

      Set CWAddinCallBackObj = swApp.GetAddInObject("CosmosWorks.CosmosWorks")

      Set COSMOSWORKSObj = CWAddinCallBackObj.COSMOSWORKS

      Part.ClearSelection2 True

      Dim skSegment As Object

      Set skSegment = Part.SketchManager.CreateLine(-0.062071, 0.031491, 0#, 0.044724, 0.020114, 0#)

      Set skSegment = Part.SketchManager.CreateLine(0.044724, 0.020114, 0#, 0.072671, -0.053698, 0#)

      Part.SetPickMode

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", -1.37844698816069E-02, 1.06805172404046E-02, 1.76911028892737E-05, False, 0, Nothing, 0)

      Dim myDisplayDim As Object

      Set myDisplayDim = Part.AddDimension2(0, 0.083967766732826, 0.163377435133782)

      Part.ClearSelection2 True

      Dim myDimension As Object

      Set myDimension = Part.Parameter("D1@Sketch2")

      myDimension.SystemValue = 0.107

      Part.ClearSelection2 True

      Part.SketchManager.InsertSketch True

      StudyManagerObj = Nothing

      ActiveDocObj = Nothing

      Set CWAddinCallBackObj = Nothing

      Set COSMOSWORKSObj = Nothing

      End Sub

       

       

      The error appears in the line in red

        • Re: How to add a dimension to a Sketch?
          Artem Taturevych

          Make sure that you have dimension named D1 in Sketch2 feature (D1@Sketch2)

            • Re: How to add a dimension to a Sketch?
              Diego Saenz

              Well, I dont have it, but that's supposed to be the point of the macro, add a dimension.

              I recorded myself drawing two lines and then adding a dimension to one of them, the default name is D1. I dont get it how it is not working if it is a recording...

              Maybe I'm missing something, I'm new to solidworks in general.

                • Re: How to add a dimension to a Sketch?
                  John Alexander

                  Solidworks' macro recorder is effectively worthless for generating working code. It uses obsolete API calls, rarely captures the crucial state changing functions, etc. It's better to use it for discover, as a starting point, and then go spelunking in the API documentation to verify what is and isn't valid.

                   

                  Set myDimension = Part.Parameter("D1@Sketch2")

                  'this returns Nothing and assigns Nothing to myDimension when the part doesn't have a Dimensions named "D1@Sketch2".

                  myDimension.SystemValue = 0.107

                  'this throws an error because myDimensions equals Nothing

                   

                   

                  The line that actually creates the dimension is above where it throws. You need to use the object reference that it returns to get access to the underlying Dimension and set its parameter. If you don't want to change the dimension, there isn't really any reason to keep any of the code following the dimension creation.

                   

                  Here is the documentation page for those functions.

                   

                  2012 SOLIDWORKS API Help - AddDimension2 Method (IModelDoc2)

                  2012 SOLIDWORKS API Help - Parameter Method (IModelDoc2)

                    • Re: How to add a dimension to a Sketch?
                      David Mandl

                      Ran into this while trying to "play along" with the API Fundamentals eCourse on mySolidworks.

                       

                      I try recording a simple macro of sketching a circle and extruding it, and the stinking macro that I get from the recorder won't work, with a failure at the same line...

                       

                       

                      *roll eyes*

                       

                      Poor form, Macro Recorder, poor form.

                       

                      This seems like a pretty rudimentary problem to have with the macro recorder (spitting out code for one of the simplest operations that there is in SW).  Is there an SPR that we can start spamming to try to get this resolved, or would the SW devs be drinking from a fire hose trying to keep this stuff up to date?