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
Macro has an option to export coordinates in the sketch space (XY for 2D sketch) or in the model space (XYZ). Macro has an option to convert the points coordinates to system units (meters) or user units, currently assigned to the model.
Configure the macro by changing the constants below.
Const CONVERT_TO_USER_UNIT AsBoolean = True'True to use the current model units, False to use system units (meters)Const CONVERT_TO_MODEL_SPACE AsBoolean = True'For 2D Sketches, True to export coordinates in the sketch space, False to convert coordinates to the model spaceConst OUT_PATH AsString = "D:\points.csv"'Full path to the output file
Const CONVERT_TO_USER_UNIT AsBoolean = TrueConst CONVERT_TO_MODEL_SPACE AsBoolean = TrueConst OUT_PATH AsString = "D:\points.csv"Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
try_:
OnErrorGoTo catch_
Set swModel = swApp.ActiveDoc
If swModel IsNothingThen
Err.Raise vbError, "", "Please open model"EndIfDim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = swModel.SelectionManager
Dim swSketchFeat As SldWorks.Feature
Set swSketchFeat = swSelMgr.GetSelectedObject6(1, -1)
Dim swSketch As SldWorks.sketch
IfNot swSketchFeat IsNothingThenSet swSketch = swSketchFeat.GetSpecificFeature2
EndIfIf swSketch IsNothingThen
Err.Raise vbError, "", "Please select sketch"EndIfDim vPts AsVariant
vPts = ExtractPoints(swModel, swSketch, CONVERT_TO_MODEL_SPACE, CONVERT_TO_USER_UNIT)
WritePointsToCsvFile OUT_PATH, vPts
GoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
EndSubFunction ExtractPoints(model As SldWorks.ModelDoc2, sketch As SldWorks.sketch, convertCoordsToModelSpace AsBoolean, convertCoordsToUserUnits AsBoolean) AsVariantDim vSkPts AsVariant
vSkPts = sketch.GetSketchPoints2()
Dim i AsIntegerIf IsEmpty(vSkPts) Then
Err.Raise vbError, "", "Sketch contains no points"EndIfDim vPts() AsVariantReDim vPts(UBound(vSkPts))
For i = 0 To UBound(vSkPts)
Dim swSkPt As SldWorks.SketchPoint
Set swSkPt = vSkPts(i)
Dim dPt(2) AsDouble
dPt(0) = swSkPt.X: dPt(1) = swSkPt.Y: dPt(2) = swSkPt.Z
Dim vPt AsVariant
vPt = dPt
If convertCoordsToModelSpace Then
vPt = ConvertPointLocation(vPt, sketch.ModelToSketchTransform.Inverse())
EndIfIf convertCoordsToUserUnits Then
vPt = ConvertToUserUnits(vPt, model)
EndIf
vPts(i) = vPt
Next
ExtractPoints = vPts
EndFunctionFunction ConvertPointLocation(pt AsVariant, transform As SldWorks.MathTransform) AsVariantDim swMathUtils As SldWorks.MathUtility
Set swMathUtils = swApp.GetMathUtility
Dim swMathPt As SldWorks.MathPoint
Set swMathPt = swMathUtils.CreatePoint(pt)
Set swMathPt = swMathPt.MultiplyTransform(transform)
ConvertPointLocation = swMathPt.ArrayData
EndFunctionFunction ConvertToUserUnits(pt AsVariant, model As SldWorks.ModelDoc2) AsVariantDim swUserUnits As SldWorks.UserUnit
Set swUserUnits = model.GetUserUnit(swUserUnitsType_e.swLengthUnit)
Dim convFactor AsDouble
convFactor = swUserUnits.GetConversionFactor
Dim dPt(2) AsDouble
dPt(0) = pt(0) * convFactor
dPt(1) = pt(1) * convFactor
dPt(2) = pt(2) * convFactor
ConvertToUserUnits = dPt
EndFunctionSub WritePointsToCsvFile(filePath AsString, vPts AsVariant)
Dim fileNmb AsInteger
fileNmb = FreeFile
Open filePath For Output As #fileNmb
Dim i AsIntegerFor i = 0 To UBound(vPts)
Print #fileNmb, vPts(i)(0) & "," & vPts(i)(1) & "," & vPts(i)(2)
Next
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