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