This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
Output file is saved into the same folder as original drawing and named [drawing name]-dimensions.csv
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
try_:
OnErrorGoTo catch_:
Dim swDraw As SldWorks.DrawingDoc
Set swDraw = swApp.ActiveDoc
If swDraw IsNothingThen
Err.Raise vbError, "", "Please open drawing"EndIf
ExportDrawingDimensions swDraw
GoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
EndSubSub ExportDrawingDimensions(draw As SldWorks.DrawingDoc)
Dim vSheets AsVariant
vSheets = draw.GetViews
Dim fileNmb AsInteger
fileNmb = FreeFile
Dim filePath AsString
filePath = draw.GetPathName
If filePath = ""Then
Err.Raise vbError, "", "Please save drawing document"EndIf
filePath = Left(filePath, InStrRev(filePath, ".") - 1) & "-dimensions.csv"
Open filePath For Output As #fileNmb
Dim header AsString
header = Join("Name", "Owner", "Type", "X", "Y", "Value", "Grid Ref", "Tolerance", "Min", "Max")
Print #fileNmb, header
Dim i AsIntegerFor i = 0 To UBound(vSheets)
Dim vViews AsVariant
vViews = vSheets(i)
Dim j AsIntegerFor j = 0 To UBound(vViews)
Dim swView As SldWorks.view
Set swView = vViews(j)
ExportViewDimensions swView, draw, fileNmb
NextNext
Close #fileNmb
EndSubSub ExportViewDimensions(view As SldWorks.view, draw As SldWorks.DrawingDoc, fileNmb AsInteger)
Dim swDispDim As SldWorks.DisplayDimension
Set swDispDim = view.GetFirstDisplayDimension5
Dim swSheet As SldWorks.Sheet
Set swSheet = view.Sheet
If swSheet IsNothingThenSet swSheet = draw.Sheet(view.name)
EndIfWhileNot swDispDim IsNothingDim swAnn As SldWorks.Annotation
Set swAnn = swDispDim.GetAnnotation
Dim vPos AsVariant
vPos = swAnn.GetPosition()
Dim swDim As SldWorks.dimension
Set swDim = swDispDim.GetDimension2(0)
Dim drwZone AsString
drwZone = swSheet.GetDrawingZone(vPos(0), vPos(1))
vPos = GetPositionInDrawingUnits(vPos, draw)
Dim tolType AsStringDim minVal AsDoubleDim maxVal AsDouble
GetDimensionTolerance draw, swDim, tolType, minVal, maxVal
OutputDimensionData fileNmb, swDim.FullName, view.name, GetDimensionType(swDispDim), CDbl(vPos(0)), CDbl(vPos(1)), _
CDbl(swDim.GetValue3(swInConfigurationOpts_e.swThisConfiguration, Empty)(0)), _
drwZone, tolType, minVal, maxVal
Set swDispDim = swDispDim.GetNext5
Wend
EndSubFunction GetPositionInDrawingUnits(pos AsVariant, draw As SldWorks.DrawingDoc) AsVariantDim dPt(1) AsDouble
dPt(0) = ConvertToUserUnits(draw, CDbl(pos(0)), swLengthUnit)
dPt(1) = ConvertToUserUnits(draw, CDbl(pos(1)), swLengthUnit)
GetPositionInDrawingUnits = dPt
EndFunctionFunction ConvertToUserUnits(model As SldWorks.ModelDoc2, val AsDouble, unitType As swUserUnitsType_e) AsDoubleDim swUserUnit As SldWorks.UserUnit
Set swUserUnit = model.GetUserUnit(unitType)
Dim convFactor AsDouble
convFactor = swUserUnit.GetConversionFactor()
ConvertToUserUnits = val * convFactor
EndFunctionFunction GetDimensionType(dispDim As SldWorks.DisplayDimension) AsStringSelectCase dispDim.Type2
Case swDimensionType_e.swAngularDimension
GetDimensionType = "Angular"Case swDimensionType_e.swArcLengthDimension
GetDimensionType = "ArcLength"Case swDimensionType_e.swChamferDimension
GetDimensionType = "Chamfer"Case swDimensionType_e.swDiameterDimension
GetDimensionType = "Diameter"Case swDimensionType_e.swDimensionTypeUnknown
GetDimensionType = "Unknown"Case swDimensionType_e.swHorLinearDimension
GetDimensionType = "HorLinear"Case swDimensionType_e.swHorOrdinateDimension
GetDimensionType = "HorOrdinate"Case swDimensionType_e.swLinearDimension
GetDimensionType = "Linear"Case swDimensionType_e.swOrdinateDimension
GetDimensionType = "Ordinate"Case swDimensionType_e.swRadialDimension
GetDimensionType = "Radial"Case swDimensionType_e.swScalarDimension
GetDimensionType = "Scalar"Case swDimensionType_e.swVertLinearDimension
GetDimensionType = "VertLinear"Case swDimensionType_e.swVertOrdinateDimension
GetDimensionType = "VertOrdinate"Case swDimensionType_e.swZAxisDimension
GetDimensionType = "ZAxis"EndSelectEndFunctionSub GetDimensionTolerance(draw As SldWorks.DrawingDoc, swDim As SldWorks.dimension, ByRef tolType AsString, ByRef minVal AsDouble, ByRef maxVal AsDouble)
Dim swTol As SldWorks.DimensionTolerance
Set swTol = swDim.Tolerance
SelectCase swTol.Type
Case swTolType_e.swTolBASIC
tolType = "Basic"Case swTolType_e.swTolBILAT
tolType = "Bilat"Case swTolType_e.swTolBLOCK
tolType = "Block"Case swTolType_e.swTolFIT
tolType = "Fit"Case swTolType_e.swTolFITTOLONLY
tolType = "FitTolOnly"Case swTolType_e.swTolFITWITHTOL
tolType = "FitWithTol"Case swTolType_e.swTolGeneral
tolType = "General"Case swTolType_e.swTolLIMIT
tolType = "Limit"Case swTolType_e.swTolMAX
tolType = "Max"Case swTolType_e.swTolMETRIC
tolType = "Metric"Case swTolType_e.swTolMIN
tolType = "Min"Case swTolType_e.swTolNONE
tolType = "None"Case swTolType_e.swTolSYMMETRIC
tolType = "Symmetric"EndSelect
swTol.GetMinValue2 minVal
swTol.GetMaxValue2 maxVal
Dim unitType As swUserUnitsType_e
If swDim.GetType() = swDimensionParamType_e.swDimensionParamTypeDoubleAngular Then
unitType = swUserUnitsType_e.swAngleUnit
Else
unitType = swUserUnitsType_e.swLengthUnit
EndIf
minVal = ConvertToUserUnits(draw, minVal, unitType)
maxVal = ConvertToUserUnits(draw, maxVal, unitType)
EndSubSub OutputDimensionData(fileNmb AsInteger, dimName AsString, owner AsString, dimType AsString, x AsDouble, y AsDouble, value AsDouble, gridRef AsString, tol AsString, minAsDouble, maxAsDouble)
Dim line AsString
line = Join(dimName, owner, dimType, x, y, value, gridRef, tol, min, max)
Print #fileNmb, line
EndSubFunctionJoin(ParamArray parts() AsVariant) AsStringDim res AsStringIfNot IsEmpty(parts) ThenDim i AsIntegerFor i = 0 To UBound(parts)
res = res & IIf(i = 0, "", ", ") & parts(i)
NextEndIfJoin = res
EndFunction
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more