Instructions
Gets all dimension names, locations, and values on the current sheet.
- Preconditions
- drawing is open
- Result
- CSV containing dimension data is saved to drawing folder. Position values are in mm.
Files
Get name, owner, type, position, and value of all dimensions on a sheet
Code
Module: mMain
'Gets all dimension names, locations, and values on the current sheet.
'Preconditions: drawing is open
'Results: CSV containing dimension data is saved to drawing folder. Position values are in mm.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDraw As SldWorks.DrawingDoc
Dim swSheet As SldWorks.Sheet
Dim swView As SldWorks.View
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swAnn As SldWorks.Annotation
Dim vViews As Variant
Dim vDispDims As Variant
Dim vPos As Variant
Dim vVals As Variant
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim dimData As New Collection
Dim strRowText As String
Dim strDimVals As String
Dim strFilePath As String
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
Set swSheet = swDraw.GetCurrentSheet
'make sure drawing is saved, get the path name
strFilePath = swModel.GetPathName
If strFilePath = Empty Then
swApp.SendMsgToUser "Please save drawing before running macro."
Exit Sub
End If
'clear collection
While dimData.Count <> 0
dimData.Remove 1
Wend
'add header
dimData.Add "Name|Owner|Type|X(mm)|Y(mm)|Z(mm)|Value"
vViews = swSheet.GetViews
If IsEmpty(vViews) = False Then
For i = 0 To UBound(vViews)
Set swView = vViews(i)
vDispDims = swView.GetDisplayDimensions
If IsEmpty(vDispDims) = False Then
For j = 0 To UBound(vDispDims)
Set swDispDim = vDispDims(j)
'name
strRowText = swDispDim.GetNameForSelection
'owner
strRowText = strRowText & "|" & swView.Name
'type
strRowText = strRowText & "|" & GetDimType(swDispDim.Type2)
'position
Set swAnn = swDispDim.GetAnnotation
vPos = swAnn.GetPosition
strRowText = strRowText & "|" & Round(vPos(0), 4) * 1000 & "|" & Round(vPos(1), 4) * 1000 & "|" & Round(vPos(2), 4) * 1000
'value
Set swDim = swDispDim.GetDimension2(Empty)
vVals = swDim.GetValue3(swThisConfiguration, Empty)
strDimVals = Empty
If IsEmpty(vVals) = False Then
For k = 0 To UBound(vVals)
strDimVals = "|" & vVals(k)
Next k
strRowText = strRowText & strDimVals
End If
dimData.Add strRowText
Next j
End If
Next i
End If
strFilePath = Left(strFilePath, InStrRev(strFilePath, "\")) & swModel.GetTitle
If Right(UCase(strFilePath), 6) = "SLDDRW" Then strFilePath = Replace(UCase(strFilePath), ".SLDDRW", Empty)
strFilePath = strFilePath & ".csv"
swApp.SendMsgToUser CreateCSV(dimData, strFilePath)
End Sub
Function GetDimType(enumVal As Long) As String
Select Case enumVal
Case swDimensionTypeUnknown
GetDimType = "Unknown"
Case swOrdinateDimension
GetDimType = "Ordinate"
Case swLinearDimension
GetDimType = "Linear"
Case swAngularDimension
GetDimType = "Angular"
Case swArcLengthDimension
GetDimType = "Arc Length"
Case swRadialDimension
GetDimType = "Radial"
Case swDiameterDimension
GetDimType = "Diameter"
Case swHorOrdinateDimension
GetDimType = "Horizontal ordinate"
Case swVertOrdinateDimension
GetDimType = "Vertical ordinate"
Case swZAxisDimension
GetDimType = "Z Axis"
Case swChamferDimension
GetDimType = "Chamfer"
Case swHorLinearDimension
GetDimType = "Horizontal linear"
Case swVertLinearDimension
GetDimType = "Vertical linear"
Case swScalarDimension
GetDimType = "Scalar"
Case Else
GetDimType = "Unrecognized"
End Select
End Function
Module: mUtility
Public Function CreateCSV(rows As Collection, path As String) As String
On Error GoTo Errorhandler
'This code requires a reference (In the VBE Tools > References) to Microsoft Scripting Runtime
Dim FSO As FileSystemObject
Dim FSOFile As TextStream
Dim i As Long
If Right(UCase(path), 4) <> ".CSV" Then
create = "File must have .csv extension."
Exit Function
End If
Set FSO = New FileSystemObject
Set FSOFile = FSO.CreateTextFile(path, True, True)
FSOFile.WriteLine "sep=|"
For i = 1 To rows.Count
FSOFile.WriteLine rows.Item(i)
Next i
FSOFile.Close
If FSO.FileExists(path) Then
CreateCSV = "File successfully saved to " & path
Else
CreateCSV = "File failed to save."
End If
Exit Function
Errorhandler:
If Err.Number = 70 Then
CreateCSV = "CSV file is open. Please close and then save."
Else
CreateCSV = "Save failed. Error number " & Err.Number & ". " & Err.Description
End If
End Function