I'm trying to make a macro behave similarly to the regular features so I can hit enter or ok(the shortcut ok) for completion. The macro brings up a window of options, that is usually left unaltered, and executes once the OK button in the window is clicked. Anyone knows how to make it react like this?
Option Explicit
Private Sub Swap(ByRef a As Double, ByRef b As Double)
Dim temp As Double
temp = a
a = b
b = temp
End Sub
Function BuildMacroFeature(swApp As SldWorks.SldWorks, swModel As ModelDoc2, swFeature As Feature) As Variant
Dim swModeler As Modeler
Dim swMacroFeatData As SldWorks.MacroFeatureData
Dim selObjects As Variant
Dim selTypes As Variant
Dim selMarks As Variant
Dim paramCount As Integer
Dim paramNames As Variant
Dim paramTypes As Variant
Dim paramValues As Variant
Dim sliceHeight As Double
Dim totalHeight As Double
Dim i As Integer
Dim firstID As Long
firstID = 1
Dim resultBodies As Collection
Set resultBodies = New Collection
Set swModeler = swApp.GetModeler
Set swMacroFeatData = swFeature.GetDefinition
swMacroFeatData.AccessSelections swModel, Nothing
swMacroFeatData.GetSelections3 selObjects, selTypes, selMarks, Nothing, Nothing
paramCount = swMacroFeatData.GetParameterCount
If paramCount > 0 Then
swMacroFeatData.GetParameters paramNames, paramTypes, paramValues
For i = 0 To paramCount - 1
If paramNames(i) = "Slice_Height" Then
sliceHeight = CDbl(paramValues(i))
End If
Next i
End If
If Not IsEmpty(selObjects) Then
If selTypes(0) = swConst.swSelectType_e.swSelFACES Then
Dim swFace As Face2
Dim swSurface As Surface
Set swFace = selObjects(0)
Set swSurface = swFace.GetSurface
Dim swBody As Body2
Set swBody = swFace.GetBody
Dim swBox As Variant
swBox = swBody.GetBodyBox
Dim boxDims(8) As Double
boxDims(0) = swBox(0) + ((swBox(3) - swBox(0)) / 2)
boxDims(1) = swBox(1) + ((swBox(4) - swBox(1)) / 2)
boxDims(2) = swBox(2)
boxDims(3) = 0
boxDims(4) = 0
boxDims(5) = 1
boxDims(6) = swBox(3) - swBox(0)
boxDims(7) = swBox(4) - swBox(1)
boxDims(8) = swBox(5) - swBox(2)
Dim swBoxBody As Body2
Set swBoxBody = swModeler.CreateBodyFromBox3(boxDims)
Dim swCopyBody As Body2
Set swCopyBody = swBody
Dim maxPoint(2) As Double
Dim minPoint(2) As Double
Dim normal As Variant
normal = swFace.normal
swCopyBody.GetExtremePoint normal(0), normal(1), normal(2), minPoint(0), minPoint(1), minPoint(2)
swCopyBody.GetExtremePoint -normal(0), -normal(1), -normal(2), maxPoint(0), maxPoint(1), maxPoint(2)
resultBodies.Add swCopyBody
totalHeight = Sqr((maxPoint(0) - minPoint(0)) ^ 2 + (maxPoint(1) - minPoint(1)) ^ 2 + (maxPoint(2) - minPoint(2)) ^ 2)
Dim largestHalfDim As Double
largestHalfDim = 10 ' Optional: You could calculate this from the bounding box
Dim numSlices As Integer
numSlices = totalHeight / sliceHeight
Dim directionMultiplier As Integer
For directionMultiplier = -1 To 1 Step 2 ' -1 and +1
For i = 1 To numSlices
Dim offsetDist As Double
offsetDist = sliceHeight * i * directionMultiplier
Dim tempSurface As Surface
Set tempSurface = swModeler.CreateOffsetSurface(swSurface, offsetDist)
If tempSurface Is Nothing Then Exit For
Dim uvLow As Variant
Dim uvHigh As Variant
uvLow = tempSurface.GetClosestPointOn(swBox(0), swBox(1), swBox(2))
uvHigh = tempSurface.GetClosestPointOn(swBox(3), swBox(4), swBox(5))
Dim uv(3) As Double
uv(0) = uvLow(3) - largestHalfDim
uv(1) = uvHigh(3) + largestHalfDim
If uv(0) > uv(1) Then Swap uv(0), uv(1)
uv(2) = uvLow(4) - largestHalfDim
uv(3) = uvHigh(4) + largestHalfDim
If uv(2) > uv(3) Then Swap uv(2), uv(3)
Dim tempSheet As Body2
Set tempSheet = swModeler.CreateSheetFromSurface(tempSurface, uv)
If tempSheet Is Nothing Then Exit For
Dim k As Integer
Dim newResultBodies As Collection
Set newResultBodies = New Collection
For k = 1 To resultBodies.Count
Dim tempBody As Body2
Set tempBody = resultBodies(k)
Dim vNewBodies As Variant
Dim errorCode As Long
Dim intersectionCount As Long
intersectionCount = tempBody.IGetIntersectionEdgeCount(tempSheet)
If intersectionCount > 0 Then
vNewBodies = tempBody.Operations2(swConst.swBodyOperationType_e.SWBODYCUT, tempSheet, errorCode)
If Not IsEmpty(vNewBodies) Then
Dim n As Integer
For n = 0 To UBound(vNewBodies)
Dim newBody As Body2
Set newBody = vNewBodies(n)
newResultBodies.Add newBody
Next n
End If
Else
newResultBodies.Add tempBody
End If
Next k
Set resultBodies = newResultBodies
Next i
Next directionMultiplier
' Assign user IDs to resulting faces and edges
For i = 1 To resultBodies.Count
Dim tempNewBody As Body2
Set tempNewBody = resultBodies(i)
Dim vFaces As Variant
Dim vEdges As Variant
vFaces = tempNewBody.GetFaces
vEdges = tempNewBody.GetEdges
Dim j As Integer
For j = 0 To UBound(vEdges)
swMacroFeatData.SetEdgeUserId vEdges(j), j, 0
Next j
For j = 0 To UBound(vFaces)
swMacroFeatData.SetFaceUserId vFaces(j), j, 0
Next j
Next i
Dim resultBodyArray() As Body2
If resultBodies.Count <> 0 Then
ReDim resultBodyArray(resultBodies.Count - 1)
For i = 0 To UBound(resultBodyArray)
Set resultBodyArray(i) = resultBodies(i + 1)
Next i
End If
BuildMacroFeature = resultBodyArray
End If
End If
swMacroFeatData.ReleaseSelectionAccess
End Function
