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
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
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.
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... (??)
@ 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.
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:

In my opinion it comes from your selection of the name of the rating. If you've tried several times, SW increments the sketch numbers, so if it's not called D1@Esquisse1 the content of mydimension is empty.
So try this instead:
Dim swApp As Object
Dim Part As SldWorks.ModelDoc2
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("Plan de face", "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 SldWorks.DisplayDimension
Set myDisplayDim = Part.AddDimension2(2.59101957793034E-02, -1.59578260869565E-02, 0)
Dim myDimension As SldWorks.Dimension
Set myDimension = myDisplayDim.GetDimension
myDimension.SystemValue = 0.05
Part.SketchManager.InsertSketch True
boolstatus = swApp.SetUserPreferenceToggle(swUserPreferenceToggle_e.swInputDimValOnCreate, value)
End Sub
If not, try macro in a new room!? the sketches and the names of the dimensions will be back to zero!?
Yes @Cyril.f, it seems to work!!
I'm analyzing the code, thank you!
It's okay, it's what I wanted.
Thank you all for your help!!