I am automating a spring from multiple text files. After getting spring centerline data and wire diameter from a text file. Each row represents a point, and each column represents the X, Y, and Z coordinate of those points, respectively.
The spring has three features. First, the Curve feature takes the text file and makes a curve based on the points given. Second, the Plane feature is used to put a sketch normal to the spring centerline. This sketch is a circle, whose diameter will be the wire diameter. Third, the Sweep feature uses the sketch as a profile and the curve as a path to make the spring.
The purpose of creating a sketch plane is to make the orientation at a right angle with respect to the end of the wire. I would like to position the spring's plane with respect to the right angle at the end of the wire and center the circle on the sketch. Presently, the circle shows up but the plane does not, regardless if I use InsertRefPlane or 3D Sketch Plane.
I've also tried using this line:
- myModelView.RotateAboutCenter 0, (Y / 500)
But it makes the plane disappear.
I use it in conjunction with this line, where X, Y, and Z represent the spline text file data:
Status = swSketchManager.CreateSketchPlane(X / 1000, Y / 1000, Z / 1000)
Should I use RotateAboutCenter, or are angle, sin, cos functions, etc. needed here? Here is the code I have so far.
Dim swApp As Object
'Dim swApp As SldWorks.SldWorks
Dim Part As Object
'Dim Part As SldWorks.ModelDoc2
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swSketchManager As SldWorks.SketchManager
'Initiate the Sub Routine
Sub main()
'On Error Resume Next
Set swApp = _
- Application.SldWorks
Set Part = swApp.ActiveDoc
'swApp.ActiveDoc.ActiveView.FrameState = 1
Dim skPoint As Object
Dim File1 As String
Dim FolderPath As String
Dim files1 As Integer
Dim path As String
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Set swModel = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
Dim value As SketchSegment
Dim instance As ISketchManager
Dim radius As Double
Dim swFeature As SldWorks.Feature
Dim swFeatureManager As SldWorks.FeatureManager
Dim curveNum As String
Dim lineNum As String
Dim sketchNum, sketchNum2, sketchNum3 As String
Dim angle As Double
Set xlapp = CreateObject("Excel.Application")
FileName = xlapp.GetOpenFileName("Text Files (*.txt),*.txt*", , "Choose Spring Centerline Files...", , True)
Set xlapp = Nothing
If IsArray(FileName) = False Then
MsgBox "Please select a file."
Exit Sub
End If
'MsgBox "Number of files in directory: " & files1
'Opens all the found Deform Files
For j = 1 To UBound(FileName)
Open FileName(j) For Input As #1
' Skips 5 Header lines, but there's probably a better way to do it
For i = 1 To 5
Input #1, X
Next i 'End i For
' Makes your initial point
Input #1, X, Y, Z, D
'Loop that makes the spline centerline
- Part.InsertCurveFileBegin
boolstatus = Part.InsertCurveFilePoint(X / 1000, Y / 1000, Z / 1000)
Do While Not EOF(1)
Input #1, X, Y, Z, D
boolstatus = Part.InsertCurveFilePoint(X / 1000, Y / 1000, Z / 1000)
Loop
Close #1
'Close Spline
radius = D / 2000
'Choose the right number Curve
curveNum = "Curve" + CStr(j)
'Choose the right number Sketch
sketchNum = "Point" + CStr(j) + "@3DSketch" + CStr(j)
'Selects the spline you just made
boolstatus = Part.Extension.SelectByID2(curveNum, "REFERENCECURVES", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
'Find some way to select the point that is made line 33
boolstatus = Part.Extension.SelectByID2(sketchNum, "EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
Set swSketchManager = swModel.SketchManager
swSketchManager.Insert3DSketch True
Set swSketchSegment = swSketchManager.CreateCenterLine(0, 0, 0#, 0, 0, 0#)
Set swSketch = swSketchManager.ActiveSketch
Status = swSketch.SetWorkingPlaneOrientation(0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0)
Set swSketchSegment = swSketchManager.CreateCenterLine(0, 0, 0#, 0, 0, 0)
Set swSketch = swSketchManager.ActiveSketch
Status = swSketch.SetWorkingPlaneOrientation(0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0)
'You now have 2 things selected. This should make a plane at the point normal to the curve
Dim myRefPlane As SldWorks.RefPlane
- Part.ClearSelection2 True
'Insert 2D sketch of a circle
swModel.ActivateSelectedFeature
swModel.ClearSelection2 True
swSketchManager.InsertSketch True
Set swModelDocExt = swModel.Extension
Status = swModelDocExt.SelectByID2("Front Plane", "PLANE", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
swModel.ClearSelection2 True
Set swSketchSegment = swSketchManager.CreateCircleByRadius(X / 1000, Y / 1000, Z / 1000, radius)
swModel.ClearSelection2 True
swSketchManager.InsertSketch True
swModel.ClearSelection2 True
boolstatus = Part.InsertCurveFileEnd()
'Choose the right number Curve
lineNum = "Line" + CStr(j)
'Choose the right number Sketch
sketchNum2 = "Point" + CStr(j) + "@Sketch" + CStr(j)
'Choose the right number Sketch
sketchNum3 = "Plane" + CStr(j)
'Insert a 3D sketch plane
swSketchManager.Insert3DSketch True
Status = swModelDocExt.SelectByID2("Line1@3DSketch1", "EXTSKETCHSEGMENT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
Status = swModelDocExt.SelectByID2("Point2@Sketch1", "EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
Status = swSketchManager.CreateSketchPlane(X / 1000, Y / 1000, Z / 1000)
myModelView.RotateAboutCenter 0, (Y / 500)
Status = swModelDocExt.SelectByID2("Plane1", "SKETCHSURFACES", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
'Set swXform = swMathUtil.CreateTransformRotateAxis(swOriginPt, swX_Axis, 1# * RadPerDeg)
swModel.ActivateSelectedFeature
swModel.ClearSelection2 True
swSketchManager.InsertSketch True
Next j 'End j For
End Sub
Note: I’ve tried using the InsertRefPlane function and can’t get that to show up at all when generating a spline from the data, despite the fact that that’s what appears when recording a macro that creates the spline.
The spring should look like this:
No problem, try the attached macro.
Andreas.