I have Sheetmetal flat pattern drawing and I am trying to automate to add the dimensions as shown below using Macro -
Below code i got from ChatGPT but it does not add any dimensions. looking for guidance on how to fix it or if anyone has the similar macro and can share here. Thanks
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swDraw As DrawingDoc
Dim swView As View
Dim swPart As ModelDoc2
Dim swFeat As Feature
Sub main()
On Error GoTo ErrorHandler
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Or swModel.GetType <> swDocDRAWING Then
swApp.SendMsgToUser2 "Open a drawing with a flat pattern view.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
Set swDraw = swModel
Set swView = swDraw.GetFirstView ' Sheet view
Do
Set swView = swView.GetNextView
If swView Is Nothing Then Exit Do
If InStr(UCase(swView.ReferencedConfiguration), "FLAT-PATTERN") > 0 Then Exit Do
Loop
If swView Is Nothing Or InStr(UCase(swView.ReferencedConfiguration), "FLAT-PATTERN") = 0 Then
swApp.SendMsgToUser2 "Flat pattern view not found.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
Set swPart = swView.ReferencedDocument
If swPart Is Nothing Then
swApp.SendMsgToUser2 "Unable to access referenced part.", swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
Exit Sub
End If
swModel.ClearSelection2 True
AddBoundingBoxDimensions swModel, swView
AddBendLineDimensions swModel, swPart, swView
swApp.SendMsgToUser2 "Dimensions added successfully.", swMessageBoxIcon_e.swMbInformation, swMessageBoxBtn_e.swMbOk
Exit Sub
ErrorHandler:
swApp.SendMsgToUser2 "An error occurred: " & Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
End Sub
Function SafeGetVisibleEdges(swView As View) As Variant
On Error Resume Next
SafeGetVisibleEdges = swView.GetVisibleEntities2(0)
If Err.Number <> 0 Then
SafeGetVisibleEdges = Array() ' Return empty array on error
End If
On Error GoTo 0
End Function
Sub AddBoundingBoxDimensions(swModel As ModelDoc2, swView As View)
Dim edges As Variant
Dim swEdge As Edge, swCurve As Curve
Dim i As Long, pts As Variant
Dim minX As Double, maxX As Double, minY As Double, maxY As Double
Dim x1 As Double, y1 As Double, x2 As Double, y2 As Double
minX = 1000000000#
maxX = -1000000000#
minY = 1000000000#
maxY = -1000000000#
edges = SafeGetVisibleEdges(swView)
If IsEmpty(edges) Or Not IsArray(edges) Then Exit Sub
For i = 0 To UBound(edges)
If TypeOf edges(i) Is Edge Then
Set swEdge = edges(i)
Set swCurve = swEdge.GetCurve
If swCurve.IsLine Then
pts = swCurve.LineParams
x1 = pts(0): y1 = pts(1)
x2 = pts(3): y2 = pts(4)
minX = Min(minX, x1, x2)
maxX = Max(maxX, x1, x2)
minY = Min(minY, y1, y2)
maxY = Max(maxY, y1, y2)
End If
End If
Next i
swModel.ClearSelection2 True
If SelectEdgeAtY(swView, minY) And SelectEdgeAtY(swView, maxY) Then
' Dimension horizontal distance between edges at minY and maxY
swModel.CreateSmartDimension2 (minX + maxX) / 2, maxY + 0.02, 0#
End If
swModel.ClearSelection2 True
If SelectEdgeAtX(swView, minX) And SelectEdgeAtX(swView, maxX) Then
' Dimension vertical distance between edges at minX and maxX
swModel.CreateSmartDimension2 maxX + 0.02, (minY + maxY) / 2, 0#
End If
End Sub
Function SelectEdgeAtY(swView As View, targetY As Double) As Boolean
Dim edges As Variant, i As Long
Dim swEdge As Edge, swCurve As Curve, pts As Variant
edges = SafeGetVisibleEdges(swView)
For i = 0 To UBound(edges)
If TypeOf edges(i) Is Edge Then
Set swEdge = edges(i)
Set swCurve = swEdge.GetCurve
If swCurve.IsLine Then
pts = swCurve.LineParams
If Abs(((pts(1) + pts(4)) / 2) - targetY) < 0.001 Then
SelectEdgeAtY = swEdge.Select4(True, Nothing)
Exit Function
End If
End If
End If
Next i
SelectEdgeAtY = False
End Function
Function SelectEdgeAtX(swView As View, targetX As Double) As Boolean
Dim edges As Variant, i As Long
Dim swEdge As Edge, swCurve As Curve, pts As Variant
edges = SafeGetVisibleEdges(swView)
For i = 0 To UBound(edges)
If TypeOf edges(i) Is Edge Then
Set swEdge = edges(i)
Set swCurve = swEdge.GetCurve
If swCurve.IsLine Then
pts = swCurve.LineParams
If Abs(((pts(0) + pts(3)) / 2) - targetX) < 0.001 Then
SelectEdgeAtX = swEdge.Select4(True, Nothing)
Exit Function
End If
End If
End If
Next i
SelectEdgeAtX = False
End Function
Sub AddBendLineDimensions(swModel As ModelDoc2, swPart As ModelDoc2, swView As View)
Dim swFlat As Object, swFeat As Feature
Dim bends As Variant, swBend As Object
Dim swSeg As SketchSegment
Dim i As Long
Set swFeat = swPart.FirstFeature
Do While Not swFeat Is Nothing
If swFeat.GetTypeName2 = "FlatPattern" Then
Set swFlat = swFeat.GetSpecificFeature2
Exit Do
End If
Set swFeat = swFeat.GetNextFeature
Loop
If swFlat Is Nothing Then Exit Sub
bends = swFlat.GetBendLines2
If IsEmpty(bends) Then Exit Sub
For i = 0 To UBound(bends)
Set swBend = bends(i)
Set swSeg = swBend.GetSketchSegment
If Not swSeg Is Nothing Then
Dim bendMidX As Double, bendMidY As Double
bendMidX = (swSeg.GetStartPoint2(0) + swSeg.GetEndPoint2(0)) / 2
bendMidY = (swSeg.GetStartPoint2(1) + swSeg.GetEndPoint2(1)) / 2
Dim closestEdge As Edge
Set closestEdge = FindNearestParallelEdge(swView, swSeg)
If Not closestEdge Is Nothing Then
swModel.ClearSelection2 True
If swSeg.Select4(True, swView) And closestEdge.Select4(True, Nothing) Then
swModel.CreateSmartDimension2 bendMidX + 0.01, bendMidY + 0.01, 0#
End If
End If
End If
Next i
End Sub
Function FindNearestParallelEdge(swView As View, swSeg As SketchSegment) As Edge
Dim edges As Variant, i As Long
Dim dx1 As Double, dy1 As Double, dx2 As Double, dy2 As Double
Dim angleDiff As Double, bestDist As Double, dist As Double
Dim swEdge As Edge, swCurve As Curve, pts As Variant, bestEdge As Edge
dx1 = swSeg.GetEndPoint2(0) - swSeg.GetStartPoint2(0)
dy1 = swSeg.GetEndPoint2(1) - swSeg.GetStartPoint2(1)
bestDist = 1000000000#
edges = SafeGetVisibleEdges(swView)
If IsEmpty(edges) Then Set FindNearestParallelEdge = Nothing: Exit Function
For i = 0 To UBound(edges)
If TypeOf edges(i) Is Edge Then
Set swEdge = edges(i)
Set swCurve = swEdge.GetCurve
If swCurve.IsLine Then
pts = swCurve.LineParams
dx2 = pts(3) - pts(0)
dy2 = pts(4) - pts(1)
angleDiff = Abs(AngleBetween(dx1, dy1, dx2, dy2))
If angleDiff < 0.01 Then
dist = Abs(pts(0) - swSeg.GetStartPoint2(0)) + Abs(pts(1) - swSeg.GetStartPoint2(1))
If dist < bestDist Then
bestDist = dist
Set bestEdge = swEdge
End If
End If
End If
End If
Next i
Set FindNearestParallelEdge = bestEdge
End Function
Function AngleBetween(dx1 As Double, dy1 As Double, dx2 As Double, dy2 As Double) As Double
Dim dot As Double, mag1 As Double, mag2 As Double
dot = dx1 * dx2 + dy1 * dy2
mag1 = Sqr(dx1 ^ 2 + dy1 ^ 2)
mag2 = Sqr(dx2 ^ 2 + dy2 ^ 2)
If mag1 = 0 Or mag2 = 0 Then
AngleBetween = 1
Else
AngleBetween = 1 - (dot / (mag1 * mag2))
End If
End Function
Function Min(a As Double, b As Double, Optional c As Double = 1000000000#) As Double
Min = a
If b < Min Then Min = b
If c < Min Then Min = c
End Function
Function Max(a As Double, b As Double, Optional c As Double = -1000000000#) As Double
Max = a
If b > Max Then Max = b
If c > Max Then Max = c
End Function