Macro Block Creation from 2D Sketch in a Part

Hello

I want to make a macro (SW2019) in order to automatically create blocks from the 2D sketches.
Sketches are a single layer in a dxf file.

So I open up the dxf and I end up with different 2D sketches.

Here is the code of the macro generated by recording with comments...:

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

'Selection of the "top support" sketch
boolstatus = Part.Extension.SelectByID2("top press", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Dim myBlockDefinition As Object
Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromSelected(Nothing) ' Creating the block

'Selection of the "bottom points" sketch
boolstatus = Part.Extension.SelectByID2("bottom tips", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromSelected(Nothing) ' Creating the block

'Selection of the sketch "wedges pm"
boolstatus = Part.Extension.SelectByID2("cales pm", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromSelected(Nothing) ' Creating the block

End Sub

The directory of registration is defined in the document options.
But no blocks are recorded...
Could someone please give me a lead? thank you in advance
Fred

Hello
It must be saved at the end of the procedure.
Save Method (ISketchBlockDefinition) - 2022 - SOLIDWORKS API Help

1 Like

Thank you very much!
I tried but not being a specialist I have trouble applying the method.
A little help would be welcome.

Re
At the end of the code add this:

bRet = swSketchBlockDef.Save(NomDeFichier+Chemin)

Right after:

Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromSelected(Nothing)

The file name must be xxx.sldblk

Sorry, I can't be good at it...

Here's my code:
Sub main()

Dim value As Boolean
Dim bRet As Boolean

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim Dir As String
Dir = Part.GetPathName()
dir = left(dir, InStrRev(dir, ""))

'Selection of the "top support" sketch
boolstatus = Part.Extension.SelectByID2("top press", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Dir = Dir & "support top.sldblk"

Dim myBlockDefinition As Object
Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromSelected(Nothing) ' Creating the block
bRet = myBlockDefinition.Save(Dir)

I get an error message "runtime error ‹ 91 ›:
object variable or block variable with not defined"

When the step by step, my "myBlockDefinition" is equal to "nothing" before saving. Is it because my block would be empty? yet I select my sketch well...

Thank you for your patience and responsiveness.

Instead of object to this line:

Dim myBlockDefinition as Object

Put:

Dim myBlockDefinition As SldWorks.SketchBlockDefinition
1 Like

Hello @fgauvreau
Indeed you make a mistake in the selection,
The makesketchfromselected() function, needs a sketch entity, not the sketch itself
Otherwise use mackesketchblockfromscketch()

1 Like

Awesome!! it works perfectly
Thank you very much for your help