Solidworks VBA边缘选择无法工作

时间:2017-07-07 16:02:11

标签: vba solidworks

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

有问题的代码部分是最后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

1 个答案:

答案 0 :(得分:0)

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