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.
I would like to hide the Plane feature and the Curve feature of this macro after the spring has been generated. However, when I try to use Part.BlankRefGeom in conjunction with Part.EditDelete, SolidWorks says in a message in the part: “None of the selected entities could be deleted”.
If I take out the Part.EditDelete line, multiple springs will be generated in the files, for example, if I generate three springs, the first file will generate the first spring, and the first file will generate both the first and second springs, and the third file will generate all three when it should only generate one spring per file. Is there a way around this? Additionally, is there a simple way to hide the planes? As the BlankRefGeom feature only hides the curve.
Here is my code so far (I've also attached the macro and sample files):
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
'Declare Variables
Dim skPoint As Object
Dim File1 As String
Dim FolderPath As String
Dim files1 As Integer
Dim path As String
Dim swModel As Object
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
'Script for opening Window
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
'Opens all the found files
For j = 1 To UBound(FileName)
Open FileName(j) For Input As #1
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
radius = D / 2000
'Choose the right number Curve
curveNum = "Curve" + CStr(j)
'Choose the right number Sketch
sketchNum = "Sketch" + CStr(j)
'Creates end of wire Reference Plane and cross section
Dim myRefPlane As SldWorks.RefPlane
boolstatus = Part.InsertCurveFileEnd()
Dim svCurveFeat As SldWorks.Feature
Set svCurveFeat = swModel.Extension.GetLastFeatureAdded()
Dim swRefCurve As SldWorks.ReferenceCurve
Set swRefCurve = svCurveFeat.GetSpecificFeature2
Set swEdge = swRefCurve.GetFirstSegment()
Dim swVert As SldWorks.Vertex
Set swVert = swEdge.GetEndVertex
Dim swEnt As SldWorks.Entity
Set swEnt = swEdge
swEnt.SelectByMark False, 0
Set swEnt = swVert
swEnt.SelectByMark True, 1
'Makes plane
Set myRefPlane = Part.FeatureManager.InsertRefPlane(2, 0, 4, 0, 0, 0)
myRefPlane.Select2 False, 0
swModel.InsertSketch2 True
swModel.SketchManager.AddToDB = True
Set swSketchSegment = swModel.SketchManager.CreateCircleByRadius(0, 0, 0, radius)
swModel.SketchManager.AddToDB = True
swModel.InsertSketch2 True
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2(curveNum, "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2(sketchNum, "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2(curveNum, "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.InsertProtrusionSwept3(False, False, 0, False, False, 0, 0, False, 0, 0, 0, 0, True, True, True, 0, True)
boolstatus = Part.Extension.SelectByID2(sketchNum, "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
boolstatus = Part.Extension.SelectByID2(curveNum, "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0)
'Saves as whatever filetype you want
swModel.SaveAs3 Replace(FileName(j), ".txt", ".SLDPRT"), 0, 0
swModel.SaveAs3 Replace(FileName(j), ".txt", ".step"), 0, 0
swModel.SaveAs3 Replace(FileName(j), ".txt", ".igs"), 0, 0
'Deletes everything that is selected
'Part.BlankRefGeom
Part.EditDelete
Next j
End Sub
Thanks,
Thomas