0% found this document useful (0 votes)
404 views4 pages

CountAllHoles SRC

This macro counts the number of holes in a SolidWorks part or assembly by diameter and outputs the results to a CSV file. It processes each body, identifies faces that are full cylinders on the interior, and counts them by diameter. The counts are stored in a dictionary and written to a CSV file with the diameter and count.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
404 views4 pages

CountAllHoles SRC

This macro counts the number of holes in a SolidWorks part or assembly by diameter and outputs the results to a CSV file. It processes each body, identifies faces that are full cylinders on the interior, and counts them by diameter. The counts are stored in a dictionary and written to a CSV file with the diameter and count.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

'----------------------------------------------------------------------------------'Created by Artem Taturevych (IC3D, Australia)

'http://ic3d.com.au/
'----------------------------------------------------------------------------------'Disclaimer: The API examples are provided as is and should be used as reference
only.
'You may redistribute it and/or modify it on the condition that this header is r
etained.
'In no event shall IC3D be liable for any types of damages whatsoever
'(including without limitation, damages from the loss of use, data, profits, or
business)
'arising out of the uses of this information, applications, or services.
'----------------------------------------------------------------------------------'Macro counts all holes in active part or assembly and outputs the result to CSV
file
Const DIAM_ROUND_DIGITS As Integer = 5 ' number of digits after decimal
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swHolesDic As Object
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Please open SolidWorks Part or Assembly"
End
End If
Set swHolesDic = CreateObject("Scripting.Dictionary")
swHolesDic.CompareMode = vbTextCompare 'dicitonary works better with string
keys
Dim vBodies As Variant
If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
Dim swAssy As SldWorks.AssemblyDoc
Set swAssy = swModel
Dim vComps As Variant
vComps = swAssy.GetComponents(False)
Dim i As Integer
For i = 0 To UBound(vComps)
Dim swComp As SldWorks.Component2
Set swComp = vComps(i)

If UCase(Right(swComp.GetPathName, 6)) = "SLDPRT" Then


vBodies = swComp.GetBodies3(swBodyType_e.swSolidBody, Empty)
ProcessBodies vBodies
End If
Next
ElseIf swModel.GetType = swDocumentTypes_e.swDocPART Then
Dim swPart As SldWorks.PartDoc
Set swPart = swModel
vBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, True)
ProcessBodies vBodies
Else
MsgBox "Macro doesn't run on drawings"
End If
Dim fileName As String
fileName = OutputResultsToCsv
MsgBox "Completed. Holes Table is saved to " & fileName
End Sub
Function OutputResultsToCsv() As String
Dim fileNmb As Long
fileNmb = FreeFile
Dim fileName As String
fileName = swApp.GetCurrentMacroPathFolder() & "\holes.csv"
Open fileName For Output As #fileNmb
Print #fileNmb, "Diameter (meters), Count"
Dim vKeys As Variant
Dim vValues As Variant
vKeys = swHolesDic.keys
vValues = swHolesDic.Items
For i = 0 To swHolesDic.count - 1
Dim diam As Double
Dim count As Integer
diam = vKeys(i)
count = vValues(i)
Print #fileNmb, diam & ", " & count
Next
Close #fileNmb
OutputResultsToCsv = fileName
End Function

Sub ProcessBodies(vBodies As Variant)


If Not IsEmpty(vBodies) Then
Dim i As Integer
For i = 0 To UBound(vBodies)
Dim swBody As SldWorks.Body2
Set swBody = vBodies(i)
ProcessBody swBody
Next
End If
End Sub
Sub ProcessBody(swBody As SldWorks.Body2)
Dim vFaces As Variant
vFaces = swBody.GetFaces
Dim i As Integer
For i = 0 To UBound(vFaces)
Dim swFace As SldWorks.Face2
Set swFace = vFaces(i)
If True = IsFullCylinder(swFace) Then
If True = IsInternalFace(swFace) Then
Dim swSurf As SldWorks.Surface
Set swSurf = swFace.GetSurface
Dim vParams As Variant
vParams = swSurf.CylinderParams
Dim diam As Double
diam = CDbl(vParams(6) * 2)
Dim diamStr As String
diamStr = CStr(Round(diam, DIAM_ROUND_DIGITS))
If swHolesDic.Exists(diamStr) Then
swHolesDic.Item(diamStr) = swHolesDic.Item(diamStr) + 1
Else
swHolesDic.Add diamStr, 1
End If
End If
End If
Next
End Sub

Function IsFullCylinder(swFace As SldWorks.Face2) As Boolean


IsFullCylinder = False
Dim swSurf As SldWorks.Surface
Set swSurf = swFace.GetSurface
If swSurf.IsCylinder Then
Dim vBounds As Variant
vBounds = swFace.GetUVBounds()
Const PI As Double = 3.14
IsFullCylinder = CDbl(vBounds(0)) = 0 _
And CDbl(vBounds(1)) >= PI * 2
End If
End Function
Function IsInternalFace(swFace As SldWorks.Face2) As Boolean
Dim vLoops As Variant
vLoops = swFace.GetLoops
Dim i As Integer
IsInternalFace = False
'swFace.Select4 False, Nothing
For i = 0 To UBound(vLoops)
Dim swLoop As SldWorks.Loop2
Set swLoop = vLoops(i)
Dim vCoEdges As Variant
Dim swCoEdge As SldWorks.coEdge
Set swCoEdge = swLoop.GetFirstCoEdge
Set swCoEdge = swCoEdge.GetPartner
Set swLoop = swCoEdge.GetLoop
If False = swLoop.IsOuter() Then
IsInternalFace = True
Exit Function
End If
Next
End Function

You might also like