Example 5 - 3dpoints.swb
' ***************************************************************
' 8/3/99 3DPoints.swb
' 8/9/99 Added prompt for data file name
' Connect points with lines
' Will select plane and start sketch now
' 1/19/00 Cleaned up the code some – added comments
'
' will start a 3D sketch and automate the creation of 3d points.
'
' Point data is in a TAB delimited file "3dpoints.txt"
' example:
'1.0 2.1 1
'1.2 2.0 -1
'... and so on
'
' Macro written by Joe Jones joe@nhcad.com
' New Hampshire CAD www.nhcad.com
'
' ***************************************************************
Dim swApp As Object
Dim modelDoc As Object
Dim retval As Object
Dim dataStr As String
Dim I As Integer
Dim Coords(1 To 6) As String
Dim Cnt As Integer
Dim Units As Integer
Dim Conversion As Double
Dim fileName As String
Sub main()
Set swApp = CreateObject ("SldWorks.Application")
Set modelDoc = swApp.ActiveDoc
'select plane & start 3d sketch
Set feat = modelDoc.FirstFeature
modelDoc.Insert3DSketch
' calculate conversion factor for meters --> current units
Units = modelDoc.LengthUnit
Select Case Units
Case 0 ' mm
Conversion = 1/1000
Case 1 ' cm
Conversion = 1/100
Case 2 ' m
Conversion = 1
Case 3 ' in
Conversion = 1/39.37
Case 4, 5 ' ft and ft-in
Conversion = 1/3.28
End Select
' read data file
fileName = "3dpoints.txt"
fileName = InputBox("Open Data File", ,fileName)
Open fileName for Input as #1
For I = 1 To 6
Coords(I) = ""
Next I
Do While Not EOF(1)
Cnt = 1
For I = 1 to 3
Coords(I+3)=Coords(I)
Coords(I) = ""
Next I
Line Input #1, dataStr
For I = 1 To Len(dataStr)
If Mid(dataStr,I,1) = Chr(9) Then
Cnt = Cnt + 1
Else
Coords(Cnt) = Coords(Cnt) + Mid(dataStr,I,1)
End If
Next I
set retval = ModelDoc.CreatePoint2 ( _
Val(Coords(1)) * Conversion, _
Val(Coords(2)) * Conversion, _
Val(Coords(3)) * Conversion)
' if Coords(4) is nothing then we have read
' the last point, do not try to create a line
If Coords(4) <> "" Then
set retval = ModelDoc.CreateLine2 ( _
Val(Coords(1)) * Conversion, _
Val(Coords(2)) * Conversion, _
Val(Coords(3)) * Conversion, _
Val(Coords(4)) * Conversion, _
Val(Coords(5)) * Conversion, _
Val(Coords(6)) * Conversion)
End If
Loop
End Sub