0

Note I am very new at VBA and coding in Solidworks API in general. Explaining things very simply would be very helpful.

I am trying to generate a part based off a set of points I've uploaded. Each of the points, when uploaded, is assigned a number (i.e. "Point32@Sketch1", etc.). I would like to, as part of a loop, create a Reference Axis going from the Origin to each of the points. Since the point where I am drawing the axis will change each loop, I use SelectbyID2 (whose first input is the name in the form of a string) to pick the origin and the point in question.

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

My issue is that while I initialize the variable sketchname as a string, and concatenate with other strings, the SelectbyID2 is reading sketchname itself as a string, and not seeing the string inside the variable. I want the string inside sketchname to change each loop, but SelectbyID2 is just reading it as sketchname each time.

brain
  • 1
  • 1
    Try `sketchname = sketchname1 & count & sketchname2` without the extra quotes. – Tim Williams Jul 05 '23 at 18:22
  • Tried it. SelectbyID2 need the quotes because that's how the reference is stored in SolidWorks. I tested the concatenation by having a Msgbox() display sketchname, and you need the extra quotes for it to display as "Point32@Sketch1". – brain Jul 05 '23 at 19:03
  • If you hard-code it as `Part.Extension.SelectByID2("Point1@3DSketch1","EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)` does it work? The first parameter of that function is typed as `String`, and when passing a string you don't typically include surrounding quotes. Quotes are only needed if you use a *string literal* in the call instead of a string variable. – Tim Williams Jul 05 '23 at 19:08
  • Yes, it works if I hard code it. – brain Jul 05 '23 at 19:43
  • If it works when hardcoded then you don't need the extra quotes. If it's not working when using a variable, then something is off about that variable, maybe in the code you've not shown us. Try using `Debug.Print sketchname` inside the loop and make sure the value is what you expect. FYI you can (eg) `Dim count as Long` and it will auto-convert to a string when using `&` to concatenate it with another string. – Tim Williams Jul 05 '23 at 20:31

0 Answers0