Reputation: 125
I'm new to Solidworks API and VBA. I have created a text file which contains coordinates of vertices of many (like 200) polygons and I want to draw simple 3D objects from this data. I recorded this macro in Solidworks for a pentagon and I tried to put a for-loop in it to draw my 200 pentagon in a row.
I know I have to read a column of vertices from my text file and use it in the
Part.SketchManager.CreateLine(x1, y1, z1, x2, y2, z2)
commands. but it doesn't work. Many errors occurs which means there are many thing that I don't know to run this code as I expect.
if you think i have to tell more details, Please tell me to complete the question!!!
' ******************************************************************************
' C:\Users\Abbas\AppData\Local\Temp\swx7040\Macro1.swb - macro recorded on 02/11/20 by Abbas
' ******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = _
Application.SldWorks
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2016\templates\Part.prtdot", 0, 0, 0)
swApp.ActivateDoc2 "Part4", False, longstatus
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.ClearSelection2 True
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateLine(0#, 0#, 0#, 0.043745, 0#, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.043745, 0#, 0#, 0.06038, 0.030036, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.06038, 0.030036, 0#, 0.031422, 0.064231, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.031422, 0.064231, 0#, -0.016327, 0.049752, 0#)
Set skSegment = Part.SketchManager.CreateLine(-0.016327, 0.049752, 0#, 0#, 0#, 0#)
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Line5", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.001, 0.001, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
longstatus = Part.SaveAs3("C:\Users\Abbas\Desktop\Part4.SLDPRT", 0, 2)
Part.ClearSelection2 True
Set Part = Nothing
swApp.CloseDoc "Part4.SLDPRT"
End Sub
I need to create sth like this in solidworks...
Upvotes: 1
Views: 2117
Reputation: 395
First, you can't always use a recorded macro directly. It has to be cleaned up and corrected at some points. The macro recorder is not able to record every exact function you use in solidworks.
This is a cleaned version of your recorded macro:
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Dim Part As ModelDoc2
Set Part = swApp.NewPart ' open new part document (with standard template file defined in system-options)
Set Part = swApp.ActiveDoc
' select front plane
boolstatus = Part.Extension.SelectByID2("Ebene vorne", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
' create new sketch on front plane
Part.SketchManager.InsertSketch (True)
Dim swActiveSketch As Sketch
Set swActiveSketch = Part.SketchManager.ActiveSketch
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateLine(0#, 0#, 0#, 0.043745, 0#, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.043745, 0#, 0#, 0.06038, 0.030036, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.06038, 0.030036, 0#, 0.031422, 0.064231, 0#)
Set skSegment = Part.SketchManager.CreateLine(0.031422, 0.064231, 0#, -0.016327, 0.049752, 0#)
Set skSegment = Part.SketchManager.CreateLine(-0.016327, 0.049752, 0#, 0#, 0#, 0#)
' close active sketch
Part.SketchManager.InsertSketch (True)
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True
boolstatus = swActiveSketch.Select2(False, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.001, 0.001, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
longstatus = Part.SaveAs3("C:\Users\Abbas\Desktop\Part4.SLDPRT", 0, 2)
Part.ClearSelection2 True
swApp.CloseDoc Part.GetTitle
Set Part = Nothing
End Sub
If you want to automatically create lines with 200 points you have to replace some lines with more logic. The next example is using the points from your example which is only for demonstration. You have to fill the variable vPoints with 200 xyz points of a text file or similar.
Dim p0(2) As Variant
Dim p1(2) As Variant
Dim p2(2) As Variant
Dim p3(2) As Variant
Dim p4(2) As Variant
p0(0) = 0#
p0(1) = 0#
p0(2) = 0#
p1(0) = 0.043745
p1(1) = 0#
p1(2) = 0#
p2(0) = 0.06038
p2(1) = 0.030036
p2(2) = 0#
p3(0) = 0.031422
p3(1) = 0.064231
p3(2) = 0#
p4(0) = -0.016327
p4(1) = 0.049752
p4(2) = 0#
Dim vPoints(4) As Variant
vPoints(0) = p0
vPoints(1) = p1
vPoints(2) = p2
vPoints(3) = p3
vPoints(4) = p4
'vPoints = FillVariantWithPointsFromTextFile() ' or a collection or what else you want to use
Dim i As Integer
For i = 0 To UBound(vPoints)
Dim vPoint As Variant
vPoint = vPoints(i)
If (i + 1 <= UBound(vPoints)) Then
Dim vNextPoint As Variant
vNextPoint = vPoints(i + 1)
' draw a line between current point and next point
Set skSegment = Part.SketchManager.CreateLine(vPoint(0), vPoint(1), vPoint(2), vNextPoint(0), vNextPoint(1), vNextPoint(2))
Else
Dim vFirstPoint As Variant
vFirstPoint = vPoints(0)
' draw a line between current point and first point to close contour
Set skSegment = Part.SketchManager.CreateLine(vPoint(0), vPoint(1), vPoint(2), vFirstPoint(0), vFirstPoint(1), vFirstPoint(2))
End If
Next i
Upvotes: 3