AnsweredAssumed Answered

Insert Block As Floating Selection - Macro

Question asked by Primoz Semprimoznik on May 14, 2020
Latest reply on Sep 22, 2020 by Hansjörg Wolff


I want to make a macro that works on sw part and it does the following:

-Creates a sketch on a selected face

-Insert a sketch Block in that sketch (As floating selection)


Macro partially works. The issue is that it inserts the block at 0,0 coordinates. I would like to have the block attached to a cursor just like it is when you manually open the block.


Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swSktManager As SldWorks.SketchManager
    Dim swModel As SldWorks.ModelDoc2
    Dim swMathUtility As SldWorks.MathUtility
    Dim swMathPoint As SldWorks.MathPoint
    Dim PointCoords(2) As Double
    Dim swSktBlkDef As SldWorks.SketchBlockDefinition
    Dim scl As Double
    Dim angle As Double
  Dim swExt As SldWorks.ModelDocExtension
  Dim Distance

  'Refer to the current model
  Set swApp = Application.SldWorks
  Set swModel = swApp.ActiveDoc
  Set swExt = swModel.Extension
  'Get the last feature
  'Dim swFeature As SldWorks.Feature
  'Set swFeature = swExt.GetLastFeatureAdded
  'Select it
  'swFeature.Select2 False, -1
  'Insert a sketch
  swModel.SketchManager.InsertSketch True


    ' Change scale and angle here
    scl = 1
    angle = 0


    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSktManager = swModel.SketchManager
    Set swMathUtility = swApp.GetMathUtility


    ' Change the insert point here
    PointCoords(0) = 0 ' X
    PointCoords(1) = 0 ' Y
    PointCoords(2) = 0 ' Z


    Set swMathPoint = swMathUtility.CreatePoint(PointCoords)
    Set swSktBlkDef = swSktManager.MakeSketchBlockFromFile(swMathPoint, "C:\Users\primoz\Dat-Con\003_DOC\KI Code.SLDBLK", False, scl, angle)
End Sub