You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Solidworks API中SelectByID2变量ID命名问题求助(VBA新手)

问题描述

我是VBA及SolidWorks API编程新手,想基于上传的点生成零件,每个点会被分配编号(比如Point32@Sketch1)。我希望通过循环创建从原点到每个点的参考轴,用SelectByID2(第一个参数是字符串格式的名称)来选择原点和目标点,但遇到了问题:虽然把sketchname初始化为字符串并拼接了其他内容,但SelectByID2好像把sketchname本身当成了固定字符串,没法在循环里切换目标点。

我的VBA代码如下:

Dim count As String
Dim sketchname1 As String
Dim sketchname2 As String
Dim sketchname As String

count = 1
sketchname1 = "Point"
sketchname2 = "@3DSketch1"

Do While Not EOF(1)

sketchname = """" & sketchname1 & count & sketchname2 & """"
Axisname = """" & Axis & count & """"
Planename = """"" & Plane & count & """""

'creates the reference axis for a point
boolstatus = Part.Extension.SelectByID2(sketchname, "EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0) 'selects the first point from our data
boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0) 'selects the origin
boolstatus = Part.InsertAxis2(True) 'inserts and axis going between the point and the origin

count = Format(Val(count) + 1) 'turns the count into a number, increase it by one, then back to a string

Loop
Close #1
解决方法

核心问题:多套了不必要的双引号

你给SelectByID2传的sketchname是带双引号的字符串(比如"Point1@3DSketch1"),但SolidWorks里的点名称本身是不带双引号的(实际名称是Point1@3DSketch1),所以API找不到对应的点。

具体修改步骤

  1. 修正sketchname的拼接方式
    去掉多余的双引号,直接拼接成点的真实名称:

    sketchname = sketchname1 & count & sketchname2
    

    这样循环时会生成Point1@3DSketch1、Point2@3DSketch1这类正确的名称。

  2. 优化count的变量类型
    没必要把count定义成字符串,用Integer类型更简单,省得来回转换:

    Dim count As Integer
    count = 1
    ' 循环里直接加1就行,不用Val和Format
    count = count + 1
    
  3. 修正Axisname和Planename的错误
    你写的Axis和Plane没加引号,而且Planename多了一个双引号,改成这样:

    Axisname = "Axis" & count
    Planename = "Plane" & count
    
  4. 每次循环前清空选择集
    避免上一次的选择残留影响当前操作,在选择点之前加一句:

    Part.ClearSelection2 True
    

修改后的完整代码

Dim count As Integer
Dim sketchname1 As String
Dim sketchname2 As String
Dim sketchname As String
Dim Axisname As String
Dim Planename As String
Dim boolstatus As Boolean

count = 1
sketchname1 = "Point"
sketchname2 = "@3DSketch1"

Do While Not EOF(1)
    ' 清空之前的选择
    Part.ClearSelection2 True
    
    ' 拼接正确的点名称(不带双引号)
    sketchname = sketchname1 & count & sketchname2
    Axisname = "Axis" & count
    Planename = "Plane" & count
    
    ' 选择目标点
    boolstatus = Part.Extension.SelectByID2(sketchname, "EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
    ' 选择原点
    boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
    ' 创建参考轴
    boolstatus = Part.InsertAxis2(True)
    
    ' 计数器加1
    count = count + 1
Loop
Close #1

额外提醒

  • 确保X/Y/Z变量在循环里能正确读取到对应点的坐标,不然SelectByID2的坐标参数可能干扰选择(不过如果名称正确,坐标参数可以传0,SolidWorks会优先按名称找)。
  • 如果运行时还是找不到点,先手动在SolidWorks里确认点的名称是不是PointN@3DSketch1格式,避免拼写错误。

内容的提问来源于stack exchange,提问作者brain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 07:47:32