Solidworks API - 从文本文件中读取数据
Solidworks API - Reading data from a text file
我是 Solidworks API 和 VBA 的新手。我创建了一个文本文件,其中包含许多(如 200 个)多边形的顶点坐标,我想从这些数据中绘制简单的 3D 对象。我在 Solidworks 中为五边形录制了这个宏,并尝试在其中放置一个 for 循环以连续绘制我的 200 个五边形。
我知道我必须从我的文本文件中读取一列顶点并在
Part.SketchManager.CreateLine(x1, y1, z1, x2, y2, z2)
命令。但它不起作用。发生了许多错误,这意味着有很多我不知道 运行 这段代码的事情,正如我所期望的那样。
如果你认为我必须告诉更多细节,请告诉我完成问题!!!
' ******************************************************************************
' 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
我需要在 solidworks 中创建这样的东西...
首先,您不能总是直接使用录制的宏。它必须在某些时候进行清理和更正。宏记录器无法记录您在 solidworks 中使用的每个确切功能。
这是您录制的宏的清理版本:
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
如果您想自动创建 200 点的线,您必须用更多逻辑替换一些线。下一个示例使用您示例中的要点,仅用于演示。您必须用文本文件或类似文件的 200 个 xyz 点填充变量 vPoints。
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
我是 Solidworks API 和 VBA 的新手。我创建了一个文本文件,其中包含许多(如 200 个)多边形的顶点坐标,我想从这些数据中绘制简单的 3D 对象。我在 Solidworks 中为五边形录制了这个宏,并尝试在其中放置一个 for 循环以连续绘制我的 200 个五边形。
我知道我必须从我的文本文件中读取一列顶点并在
Part.SketchManager.CreateLine(x1, y1, z1, x2, y2, z2)
命令。但它不起作用。发生了许多错误,这意味着有很多我不知道 运行 这段代码的事情,正如我所期望的那样。
如果你认为我必须告诉更多细节,请告诉我完成问题!!!
' ******************************************************************************
' 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
我需要在 solidworks 中创建这样的东西...
首先,您不能总是直接使用录制的宏。它必须在某些时候进行清理和更正。宏记录器无法记录您在 solidworks 中使用的每个确切功能。
这是您录制的宏的清理版本:
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
如果您想自动创建 200 点的线,您必须用更多逻辑替换一些线。下一个示例使用您示例中的要点,仅用于演示。您必须用文本文件或类似文件的 200 个 xyz 点填充变量 vPoints。
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