Solidworks VBA 边选择不起作用

Solidworks VBA Edge Selection Wont Work

我在下面附上了我的宏目前的样子。到目前为止,宏将制作一个带有 2 个孔的矩形。然后需要对四个边角进行倒角。但出于某种原因,当我尝试 select 那些角落时,它不起作用。我实际上会在不同的尝试中得到不同的结果。

有问题的代码部分是最后 7 行左右。如果我 运行 这些行与宏完全分开,我就会得到我想要的结果。会不会只是一些语法问题?

关于 Solidworks,我是 VBA 的新手。我在 excel 中使用过它。因此,如果您发现任何不良习惯,我们将不胜感激。

    Option Explicit
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelDocExt As SldWorks.ModelDocExtension
    Dim swSketchMgr As SldWorks.SketchManager
    Dim swFeature As SldWorks.Feature
    Dim swFeatureMgr As SldWorks.FeatureManager
    Dim vSkLines As Variant
    Dim boolstatus As Boolean
    Dim longstatus As Long
    Dim circ As SldWorks.SketchSegment
    Dim cx1 As Double
    Dim cx2 As Double
    Dim cy1 As Double
    Dim cy2 As Double
    Dim d1 As Double
    Dim d2 As Double
    Dim b1 As Double
    Dim w1 As Double
    Dim t1 As Double
    Dim in2mmconv As Double
    Dim m2mmconv As Double

    Sub main()
    in2mmconv = 0.0254
    m2mmconv = 1 / 1000
    b1 = 5.5 * in2mmconv
    w1 = 3.5 * in2mmconv
    t1 = 0.75 * in2mmconv
    cy1 = b1 - 1.75 * in2mmconv
    cx1 = w1 / 2
    d1 = 66.779 * m2mmconv + 0.0002 * in2mmconv
    cx2 = cx1
    cy2 = cy1 - 79.3 * m2mmconv
    d2 = 0.5007 * in2mmconv

    Set swApp = Application.SldWorks

    ' Reset the counts for untitled documents for this macro

    Set swModel = swApp.ActiveDoc

    ' Select the Front plane

    Set swModelDocExt = swModel.Extension

    boolstatus = swModelDocExt.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

    ' Open a sketch and sketch a rectangle

    Set swSketchMgr = swModel.SketchManager

    swSketchMgr.InsertSketch True

    swModel.ClearSelection2 True

    vSkLines = swSketchMgr.CreateCornerRectangle(0, b1, 0, w1, 0, 0)

    ' Change view orientation and clear all selections

    swModel.ShowNamedView2 "*Trimetric", 8

    swModel.ClearSelection2 True

    ' Select the sketch entities to extrude

    Set swModelDocExt = swModel.Extension

    boolstatus = swModelDocExt.SelectByID2("Line2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("Line4", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("Line3", "SKETCHSEGMENT", 0, 0, 0, True, 0, Nothing, 0)

    ' Create the extrude feature

    Set swFeatureMgr = swModel.FeatureManager

    Set swFeature = swFeatureMgr.FeatureExtrusion2(True, False, True, 0, 0, t1, 0.381, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, True, True, True, 0, 0, False)

    ' Fit the model in the graphics area

    swModel.ViewZoomtofit2

    ' Select the face on the extrude feature

    ' and sketch the entities to pattern

    swModel.ShowNamedView2 "*Front", 1

    boolstatus = swModelDocExt.SelectByID2("", "FACE", -w1, 0, -t1, False, 0, Nothing, 0)

    Set circ = swSketchMgr.CreateCircle(cx1, cy1, 0, cx1 - d1 / 2, cy1, 0)

    Set circ = swSketchMgr.CreateCircle(cx2, cy2, 0, cx2 - d2 / 2, cy2, 0)

    boolstatus = swModelDocExt.SelectByID2("Arc1", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("Arc2", "SKETCHSEGMENT", 0, 0, 0, False, 0, Nothing, 0)

    swModel.ClearSelection2 True

    Set swFeature = swFeatureMgr.FeatureCut3(True, False, False, swEndCondThroughAll, swEndCondBlind, 0.01, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, False, True, True, False, False, False, swStartSketchPlane, 0, False)

    swModel.ClearSelection2 True

    boolstatus = swModelDocExt.SelectByID2("", "EDGE", 0, 0, -t1 / 2, True, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("", "EDGE", 0, b1, 0, True, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("", "EDGE", w1, b1, -t1 / 2, True, 0, Nothing, 0)

    boolstatus = swModelDocExt.SelectByID2("", "EDGE", w1, 0, -t1 / 2, True, 0, Nothing, 0)

    Set swFeature = swFeatureMgr.InsertFeatureChamfer(4, 1, 0.00254, 0.78539816339745, 0, 0, 0, 0)

    End Sub

我最近遇到了同样的问题。经过一些 google 搜索,我发现 selectbyID2 方法在某些情况下可能会起作用。其中之一是有一个边缘超出了您所需的边缘。他们有很多。那么,我在代码中做了什么?我计划旋转模型,以便在正常视图中选择所需的对象。就我而言,此过程完全解决了我的问题。尝试一下。 要旋转模型,请在使用 selectbyID2 之前使用 showNamedView2 方法。