MakeSketchBlockFromFile method doesn't define block

Hi

 

I am running Solidworks 2018.

I tried to insert a revision block to the drawing. And I would change the attribute of it after. However, when I used MakeSketchBlockFromFile method like this

 

                Set swSktBlkDef = swSktManager.MakeSketchBlockFromFile(swMathPoint, FileName, True, scl, angle)

 

I could insert the block successfully but the could not do anything after. During debugging, when I moved cursor over swSktBlkDef, it always showed “nothing”. Like nothing was assigned to swSktBlkDef. I couldn’t get instance from this block definition as it is empty.  

 

Can anyone help?

SolidworksApi/macros
Answers 5
AT
AT 2021-04-08 Best Answer

What is the extension of your file, is it sldblk? Can you insert this block from ui?

ST
ST 2021-04-08

What you have written is correct. However, rather than check the filename, I would check the name of the feature. I think that's the thing that must not be duplicate:

If swSkBlkDef.GetFeature.Name = "Revision" Then

...

XC
XC 2021-04-08

Hi Simon

 

Thanks.  You are right. I already had the block defined in the drawing by template. So if I tried to define another block from the same file, Solidworks would not create the instance for it. Now I can insert the another block instance. However I found it is a bit odd, that I had to cycle through all blocks in the sheets to get the block definition. Is there a better method to get block definition directly by “Name”

 

For example, if there is a block named “Revision” in the sheet. I have to use following code to cycle through all the block definition to get the one I want. Is there a better method?

 

    vBlockDef = swSktManager.GetSketchBlockDefinitions

    For i = 0 To UBound(vBlockDef)

        Set swSktBlkDef = vBlockDef(i)

        If Dir(swSktBlkDef.FileName) = "Revision.SLDBLK" Then

            Set swBlockInst = swSktManager.InsertSketchBlockInstance(swSktBlkDef, swMathPoint, 1, 0)

        End If

Next

ST
ST 2021-04-08

If a block of the same name alreadys exists in the Blocks folder in the drawing, then trying to insert it will fail.

Call GetSketchBlockDefinitions first. If the block definition already exists, you can use that to create a new instance.

XC
XC 2021-04-08

Thanks for the reply, It is a sldblk file and it can be inserted from the menu. When I did a search on Api help file, I only declaration in vb.net but not in vba. So I doubt if it is the problem.