Macro to choose the position of a frame in a drawing

Hi all

I'm currently creating macro buttons to insert blocks into the background of a drawing.

So far nothing complicated, I have a code that works very well:

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
Part.EditTemplate
Part.EditSketch
Dim myBlockDefinition As Object
Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromFile(Nothing, "D:\_MAIA\1_MODELES\4_BLOCS-BUFFERS\Tol ISO2768 - EN22768 - mK.SLDBLK", False, 1, 0)
Part.EditSheet
Part.EditSketch
End Sub

 

My problem concerns the position of the block on the drawing, I would like to be able to predefine a position in my code (in X and Y).

Currently, when I launch the macro, the block fits into the bottom left corner of the drawing.

I tried a lot of things but without success.

If anyone knows anything about VBA I'm interested ;-)

Thanks in advance,

Kind regards.

 

1 Like

Hello

In fact, the first argument of your MakeSketchBlockFromFile method is the position!

In your example, you put "nothing" so I think by default, it fits at the anchor point of your drawing.

So you have two solutions: change the anchor point of your drawing templates, or modify this nothing to choose your point like this:

InsertPoint
Insertion point, which must be a 2D point with z = 0.0, for the block definition
 

The corresponding page: 

http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.interop.sldworks~SolidWorks.interop.sldworks.ISketchManager~MakeSketchBLockFromFile.html

3 Likes

First of all, thank you for your help.

I don't want to touch my models

However, I don't know how to transcribe this new line to my code.

 To what will it return? Where should I put it?

Are there coordinates in X and Y?

Thanks in advance

1 Like

So before inserting your block, you will have to create and name your insertion point that you will use instead of your "nothing", see this link to create the point:

http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMathPoint.html

More details to create the point:

 'Create the new MathPoint from the sketch point data.

    'MathP refers to the point location in the sketch coordinates

    Set MathP = MathUtil.CreatePoint(PointCoords)

    'Display the point coordinates in relation to the sketch origin

    SketchPoints = MathP.ArrayData

    MsgBox SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2)

http://help.solidworks.com/2012/English/api/sldworksapi/Transform_Sketch_to_Model_Example_VB.htm

 

2 Likes

I admit to being lost...

Here's what I did but without any results:

 

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc
Part.EditTemplate
Part.EditSketch

Set MathP = MathUtil.CreatePoint(PointCoords)
SketchPoints = MathP.ArrayData

    MsgBox SketchPoints(0) & "5.0" & SketchPoints(1) & "5.0" & SketchPoints(2)


Dim myBlockDefinition As Object
Set myBlockDefinition = Part.SketchManager.MakeSketchBlockFromFile(Nothing, "D:\_MAIA\1_MODELES\4_BLOCS-BUFFERS\Tol ISO2768 - EN22768 - mK.SLDBLK", False, 1, 0)
Part.EditSheet
Part.EditSketch
Part.ClearSelection2 True
End Sub

In your line:

Part.SketchManager.MakeSketchBlockFromFile(Nothing, "D:\_MAIA\1_MODELES\4_BLOCS-BUFFERS\Tol ISO2768 - EN22768 - mK.SLDBLK", False, 1, 0)

 

You still use Nothing

As I told you, we need to replace this with an insertion point!

And in your line:

SketchPoints = MathP.ArrayData

You didn't fill in the coordinates.

 

Do you know vba programming?

1 Like

There is perhaps a more telling example to help you here:

'NEW BLOCKS: Create block definition

    Set swSketchBlockDef = swSketchMgr.MakeSketchBlockFromSelected(Nothing)

    '

    ' Define an insertion point

    nPt(0) = 60# / 1000#

    nPt(1) = -60# / 1000#

    nPt(2) = 0#

    vPt = nPt

    Set swMathPoint = swMathUtil.CreatePoint(vPt)

    '

    ' Insert an instance of the block definition

    Set swBlockInst = swSketchMgr.InsertSketchBlockInstance(swSketchBlockDef, swMathPoint, 1, 0)

http://help.solidworks.com/2013/English/api/sldworksapi/Create_Block_Definition_and_Insert_Block_Instance_Example_VB.htm

1 Like

Finally I changed the anchor points of the blocks.

Merici  PL

1 Like

Hello Bec2

How did you change the anchoring of these systems?

Because I don't know too much about VBA and I have for the moment tinkered to get my block to come but it doesn't put itself in the right place.

Hello and welcome @sebastien_higaxo5742 .

Here is a Vba macro to insert a block by choosing its coordinate points:

Dim swApp As Object
Dim swModel As Object
Dim swDrawing As Object
Dim swSheet As Object
Dim swBlockDef As Object
Dim swBlockIns As Object

Sub InsertBlockAtPoint()

    ' Se connecter à l'application SolidWorks
    Set swApp = Application.SldWorks

    ' Obtenir le document actif (supposé être un dessin)
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then
        MsgBox "Veuillez ouvrir un document de dessin."
        Exit Sub
    End If

    ' Vérifier si le document actif est un dessin
    If swModel.GetType <> swDocumentTypes_e.swDocDRAWING Then
        MsgBox "Le document actif n'est pas un dessin."
        Exit Sub
    End If

    Set swDrawing = swModel

    ' Obtenir la feuille active
    Set swSheet = swDrawing.GetCurrentSheet

    ' Spécifier le chemin vers la définition du bloc
    Dim blockPath As String
    blockPath = "C:\Chemin\Vers\Votre\Bloc.sldblk" ' Changez ceci par le chemin de votre bloc

    ' Charger la définition du bloc
    Set swBlockDef = swDrawing.LoadBlockDefinition(blockPath)

    If swBlockDef Is Nothing Then
        MsgBox "Échec du chargement de la définition du bloc."
        Exit Sub
    End If

    ' Définir les coordonnées du point d'insertion
    Dim insertPoint(2) As Double
    insertPoint(0) = 0.1 ' Coordonnée X en mètres
    insertPoint(1) = 0.1 ' Coordonnée Y en mètres
    insertPoint(2) = 0    ' Coordonnée Z (généralement 0 pour les dessins 2D)

    ' Insérer le bloc au point spécifié
    Set swBlockIns = swSheet.InsertBlock(swBlockDef, insertPoint)

    If swBlockIns Is Nothing Then
        MsgBox "Échec de l'insertion du bloc."
    Else
        MsgBox "Bloc inséré avec succès."
    End If

End Sub

Then, a few remarks for your next requests:

Avoid digging up 2016 Resolute jobs in the future... especially if one of the contributors has not published recently.

image

1 Like