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
This macro exports the positions of components (X, Y, Z) from the active assembly to the comma separated values (CSV) file using SOLIDWORKS API. The file can be opened in Excel or any text editor.
The component position is a coordinate of the origin point (0, 0, 0) relative to the assembly origin.
Macro can export all components or only the instances of the selected component.
Specify the path to output file via OUT_FILE_PATH constant
Const OUT_FILE_PATH AsString = "D:\locations.csv"
Specify the conversion factor from meters for the coordinates
Const CONV_FACTOR AsDouble = 1000 'meters to mm
Optionally select the component to only export its instances (i.e. all of the components with the same file path and referenced configuration). Clear selection to export all components
As the result the CSV file is created which contains
Component file full path
Referenced configuration
Component name
X, Y, Z coordinate of the origin in the specified units
Const OUT_FILE_PATH AsString = "D:\locations.csv"Const CONV_FACTOR AsDouble = 1000 'meters to mmDim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Set swAssy = swApp.ActiveDoc
IfNot swAssy IsNothingThenDim swSeedComp As SldWorks.Component2
Set swSeedComp = swAssy.SelectionManager.GetSelectedObjectsComponent4(1, -1)
Dim table AsString
table = GetComponentsPositions(swAssy, swSeedComp, CONV_FACTOR)
WriteTextFile OUT_FILE_PATH, table
Else
MsgBox "Please open assembly"EndIfEndSubFunction GetComponentsPositions(assy As SldWorks.AssemblyDoc, seedComp As SldWorks.Component2, convFactor AsDouble) AsStringDim table AsString
table = "Path,Configuration,Name,X,Y,Z"Dim vComps AsVariant
vComps = assy.GetComponents(False)
Dim i AsIntegerFor i = 0 To UBound(vComps)
Dim swComp As SldWorks.Component2
Set swComp = vComps(i)
If swComp.GetSuppression() <> swComponentSuppressionState_e.swComponentSuppressed ThenDim includeComp AsBooleanIf seedComp IsNothingThen
includeComp = TrueElseIf LCase(seedComp.GetPathName()) = LCase(swComp.GetPathName()) And LCase(seedComp.ReferencedConfiguration) = LCase(swComp.ReferencedConfiguration) Then
includeComp = TrueElse
includeComp = FalseEndIfIf includeComp ThenDim vOrigin AsVariant
vOrigin = GetOrigin(swComp)
table = table & vbLf
table = table & swComp.GetPathName() & "," & swComp.ReferencedConfiguration & "," & swComp.Name2 & "," & vOrigin(0) * convFactor & "," & vOrigin(1) * convFactor & "," & vOrigin(2) * convFactor
EndIfEndIfNext
GetComponentsPositions = table
EndFunctionFunction GetOrigin(comp As SldWorks.Component2) AsVariantDim swXForm As SldWorks.MathTransform
Set swXForm = comp.Transform2
Dim swMathUtils As SldWorks.MathUtility
Set swMathUtils = swApp.GetMathUtility
Dim dPt(2) AsDouble
dPt(0) = 0: dPt(1) = 0: dPt(2) = 0
Dim swMathPt As SldWorks.MathPoint
Set swMathPt = swMathUtils.CreatePoint(dPt)
Set swMathPt = swMathPt.MultiplyTransform(swXForm)
GetOrigin = swMathPt.ArrayData
EndFunctionSub WriteTextFile(filePath AsString, content AsString)
Dim fileNmb AsInteger
fileNmb = FreeFile
Open filePath For Output As #fileNmb
Print #fileNmb, content
Close #fileNmb
EndSub
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