Quoting in a macro

I would like to make a very simple macro with a line with the rating of it:

I make the recording, then when I launch the macro, it stops at the dimension where the floating window asks me to type a value; I would have liked the rating of 50 to set itself. But even when I enter a value, the program goes into debugging because it waits to be able to put the dimension of 50 that has already been set.

Anyone know how to proceed to dimension a sketch automatically?

1 Like

Hello

You must deactivate the option enter the dimension

1 Like

In the code, you have to add at the beginning: boolstatus = swApp.SetUserPreferenceToggle(swInputDimValOnCreate, False) then at the end of the code boolstatus = swApp.SetUserPreferenceToggle(swInputDimValOnCreate, True)
 

1 Like

@Cyril.f

I'm trying but I get an error message:

We would have to do two or three checks, on the one hand is swapp declared as Sldworks.Sldworks (it can solve the problem rather than declared as an object).

Then, are there not missing references in the references?

1 Like

Yes, swapp is declared SldWorks.sldworks

I'm drying, could you put all the code by inserting it with the option to insert code from the forum so that I can get it back to see.

Otherwise, at least check in the references (Tools> references) if the following references are enabled:

Solidworks 201X Extensibility Type Library

Sldworks 201X Type Library

Solidworks 201X Constant type library

Solidworks 201X Commands type library

For my part, I'm on 2016 and no problems.

Sorry, here's the initial code:

Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim skSegment As Object
Dim myDisplayDim As Object
Dim myDimension As Object

Sub main()

    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    
    Part.SketchManager.InsertSketch True
    boolstatus = Part.Extension.SelectByID2("Face Plan", "PLANE", -6.71223122593068E-02, 0.049524099883856, 0, False, 0, Nothing, 0)
       
    Set skSegment = Part.SketchManager.CreateLine(0#, 0#, 0#, 0.052237, 0#, 0#)
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 3.01706029555596E-02, 3.7613240418119E-04, 0, False, 0, Nothing, 0)
    
    Set myDisplayDim = Part.AddDimension2(2.91675832110764E-02, -1.54214285714286E-02, 0)
    Part.ClearSelection2 True
    
    Set myDimension = Part.Parameter("D1@Esquisse1")
    myDimension.SystemValue = 0.05
    Part.ClearSelection2 True
    Part.SketchManager.InsertSketch True
    
End Sub

It's on SolidWorks 2016

Here are the references:

Here is the second edited code:

Dim swApp As SldWorks.SldWorks
Dim Part As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim skSegment As Object
Dim myDisplayDim As Object
Dim myDimension As Object

Sub main()

    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    
    boolstatus = swApp.SetUserPreferenceToggle(swInputDimValOnCreate, False)
    
    Part.SketchManager.InsertSketch True
    boolstatus = Part.Extension.SelectByID2("Face Plan", "PLANE", -6.71223122593068E-02, 0.049524099883856, 0, False, 0, Nothing, 0)
    
    
    
    Set skSegment = Part.SketchManager.CreateLine(0#, 0#, 0#, 0.052237, 0#, 0#)
    
    boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 3.01706029555596E-02, 3.7613240418119E-04, 0, False, 0, Nothing, 0)
    
    Set myDisplayDim = Part.AddDimension2(2.91675832110764E-02, -1.54214285714286E-02, 0)
    
    
    Set myDimension = Part.Parameter("D1@Esquisse1")
    myDimension.SystemValue = 0.05
    
    Part.SketchManager.InsertSketch True
    
    boolstatus = swApp.SetUserPreferenceToggle(swInputDimValOnCreate, True)
    
End Sub

 

Hello

Replace "Dim swApp As SldWorks.SldWorks" with "Dim swApp As Object", this should work.

Kind regards

1 Like

And also, be careful, when you touch the SW parameters it is preferable to read the value of the parameter and, if you need to modify it for a treatment, to return it to its initial state at the end of the treatment:

Dim value As Boolean
value = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate)
    
If value = True Then
  boolstatus = swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate, False)
End If

###### Là tu mets ton traitement #######

boolstatus = swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate, value)

 

Kind regards

3 Likes

Indeed d.roger, I didn't specify since his sw was set to enter the odds.

In any case, a set to False and then True works even if the option is already checked or unchecked (I deliberately simplified).

For the swApp as Object, I tried with both (sldworks.sldworks and object) on 2016 it works for me. The advantage of the second is to have auto-completion when entering the code and an overview of what is expected by each function call without having to keep the help open all the time;)

Edit: Finally, works with Object, with SldWorks.SldWorks it generates a compilation error, I think yesterday it had kept the Object type in memory.

1 Like

there is no way not to do the dimension entry, just by an argument of the function, rather than undoing and then putting back the general parameters...??

 

Note: if the file already contains a sketch, the one created by the macro will not be "sketch1" but with another number... (??)

1 Like

@ Olivier42 : Hello, it is not possible to use an argument because it is a general parameter. There is nothing to my knowledge in the APIs that allows you to override these parameters via an argument.

This is confirmed by the SW API help: Before calling this method, you might want to call ISldWorks::SetUserPreferenceToggle with swUserPreferenceToggle_e.swInputDimValOnCreate to suppress the dialog box that allows the user to type the dimension value. 

1 Like

Hello

The general idea is to make complete sketches dimensioned by programming and then of course put volume functions on them. But it's important that there are sides in the sketch. Hence the test with a single line and its odds.

I'll try the modifications in a little while.

Thank you for your help!

Still not.

Here is the new code

Dim swApp As Object
Dim Part As Object

Dim boolstatus As Boolean
Dim value As Boolean

Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

value = swApp.GetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate)
    If value = True Then
      boolstatus = swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate, False)
    End If

boolstatus = Part.Extension.SelectByID2("Face Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateLine(0#, 0#, 0#, 0.051925, 0#, 0#)
boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 1.97416243507319E-02, -1.20689440993789E-03, 0, False, 0, Nothing, 0)
Dim myDisplayDim As Object
Set myDisplayDim = Part.AddDimension2(2.59101957793034E-02, -1.59578260869565E-02, 0)
Dim myDimension As Object
Set myDimension = Part.Parameter("D1@Esquisse1")
myDimension.SystemValue = 0.05
Part.SketchManager.InsertSketch True

boolstatus = swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate, value)

End Sub

It gets stuck in the same place

Here is the error message: