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 imports the points read from the specified CSV (comma separated values) file into the active sketch using SOLIDWORKS API. Both 2D and 3D Sketches are supported.
Configuration
Macro has several configuration options which can be modified by changing the values of the constants at the beginning of the macro
FIRST_ROW_HEADER specifies if the if the first row of the CSV file is considered as a header and should be ignored ignored. If CSV file doesn't contain the header set the value of the constant to False.
USE_SYSTEM_UNITS indicates if the coordinate values in the CSV file are in system units (meters). If this option is set to False, macro will use the current document units instead.
Macro can also import points relative to coordinate system. Pre-select the target coordinate system before running the macro otherwise the points will be inserted relative to global coordinate system
Input CSV file can contain 3 coordinates (X, Y, Z) or 2 coordinates (X, Y)
Open the model and create 2D or 3D sketch (or edit existing sketch)
(Optional) Pre select coordinate system if points need to be imported relative to this system
Run the macro. Specify the full path to CSV file in the displayed file browse dialog
Click OK. Points are created in the active sketch
Const USE_SYSTEM_UNITS AsBoolean = TrueConst FIRST_ROW_HEADER AsBoolean = TrueDim swApp As SldWorks.SldWorks
Sub main()
try_:
OnErrorGoTo catch_
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
IfNot swModel IsNothingThenDim swSketch As SldWorks.Sketch
Set swSketch = swModel.SketchManager.ActiveSketch
IfNot swSketch IsNothingThenDim vPoints AsVariantDim inputFile AsString
inputFile = swApp.GetOpenFileName("Specify the full path to CSV file", "", "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt|All Files (*.*)|*.*|", -1, "", "")
If inputFile <> ""Then
vPoints = ReadCsvFile(inputFile, FIRST_ROW_HEADER)
vPoints = ConvertPointsLocations(vPoints, swModel, USE_SYSTEM_UNITS, GetSelectedCoordinateSystemTransform(swModel))
DrawPoints swModel, vPoints
EndIfElse
Err.Raise vbError, "", "Please open 2D or 3D Sketch"EndIfElse
Err.Raise vbError, "", "Please open the model"EndIfGoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
EndSubFunction GetSelectedCoordinateSystemTransform(model As SldWorks.ModelDoc2) As SldWorks.mathTransform
Dim swSelMgr As SldWorks.SelectionMgr
Set swSelMgr = model.SelectionManager
If swSelMgr.GetSelectedObjectType3(1, -1) = swSelectType_e.swSelCOORDSYS ThenDim swCoordSysFeat As SldWorks.Feature
Set swCoordSysFeat = swSelMgr.GetSelectedObject6(1, -1)
Set GetSelectedCoordinateSystemTransform = model.Extension.GetCoordinateSystemTransformByName(swCoordSysFeat.Name)
ElseSet GetSelectedCoordinateSystemTransform = NothingEndIfEndFunctionSub DrawPoints(model As SldWorks.ModelDoc2, vPoints AsVariant)
model.SketchManager.AddToDB = TrueDim i AsIntegerFor i = 0 To UBound(vPoints)
Dim swSkPt As SldWorks.SketchPoint
Dim vPt AsVariant
vPt = vPoints(i)
Dim x AsDoubleDim y AsDoubleDim z AsDouble
x = CDbl(vPt(0))
y = CDbl(vPt(1))
z = CDbl(vPt(2))
Set swSkPt = model.SketchManager.CreatePoint(x, y, z)
If swSkPt IsNothingThen
Err.Raise vbError, "", "Failed to create point at: " & x & "; " & y & "; " & z
EndIfNext
model.SketchManager.AddToDB = FalseEndSubFunction ConvertPointsLocations(points AsVariant, model As SldWorks.ModelDoc2, useSystemUnits AsBoolean, mathTransform As SldWorks.mathTransform) AsVariantDim swMathUtils As SldWorks.MathUtility
Set swMathUtils = swApp.GetMathUtility
Dim convFact AsDouble
convFact = 1
IfNot useSystemUnits ThenDim swUserUnit As SldWorks.UserUnit
Set swUserUnit = model.GetUserUnit(swUserUnitsType_e.swLengthUnit)
convFact = 1 / swUserUnit.GetConversionFactor()
EndIfDim i AsIntegerFor i = 0 To UBound(points)
Dim vPt AsVariant
vPt = points(i)
Dim dPt(2) AsDoubleIf UBound(vPt) >= 0 Then
dPt(0) = CDbl(vPt(0)) * convFact
Else
dPt(0) = 0
EndIfIf UBound(vPt) >= 1 Then
dPt(1) = CDbl(vPt(1)) * convFact
Else
dPt(1) = 0
EndIfIf UBound(vPt) >= 2 Then
dPt(2) = CDbl(vPt(2)) * convFact
Else
dPt(2) = 0
EndIfIfNot mathTransform IsNothingThenDim swMathPt As SldWorks.MathPoint
Set swMathPt = swMathUtils.CreatePoint(dPt)
Set swMathPt = swMathPt.MultiplyTransform(mathTransform)
vPt = swMathPt.ArrayData
Else
vPt = dPt
EndIf
points(i) = vPt
Next
ConvertPointsLocations = points
EndFunctionFunction ReadCsvFile(filePath AsString, firstRowHeader AsBoolean) AsVariant'rows x columnsDim vTable() AsVariantDim fileName AsStringDim tableRow AsStringDim fileNo AsInteger
fileNo = FreeFile
Open filePath For Input As #fileNo
Dim isFirstRow AsBooleanDim isTableInit AsBoolean
isFirstRow = True
isTableInit = FalseDoWhileNot EOF(fileNo)
Line Input #fileNo, tableRow
IfNot isFirstRow OrNot firstRowHeader ThenDim vCells AsVariant
vCells = Split(tableRow, ",")
Dim i AsIntegerDim dCells() AsDoubleReDim dCells(UBound(vCells))
For i = 0 To UBound(vCells)
dCells(i) = CDbl(vCells(i))
NextDim lastRowIndex AsIntegerIfNot isTableInit Then
lastRowIndex = 0
isTableInit = TrueReDimPreserve vTable(lastRowIndex)
Else
lastRowIndex = UBound(vTable, 1) + 1
ReDimPreserve vTable(lastRowIndex)
EndIf
vTable(lastRowIndex) = dCells
EndIfIf isFirstRow Then
isFirstRow = FalseEndIfLoop
Close #fileNo
ReadCsvFile = vTable
EndFunction
Notifications
Join session by SOLIDWORKS and PDM API expert Artem Taturevych at 3DEXPERIENCE World 2026 on Wednesday, Feb 4 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more