ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
ZLZhaoyi Liu05/09/2018

I've stuck on this for a long time.

We are using Macro to insert hundreds of electrodes on a head model. To do that we have two separate files, one importing points at the coordinates and another importing points based on the normal vector. The two files works perfectly, and I've made sure to AddtoDB so that the points don't snap into each other. In the Macro with problem, we add axis by mating the coordinates with the normal vector one by one. To select each normal vector in the loop SelectByID2 is used, but according to the debugger, the boolstatus returned is sometimes false, which results in total failure of the code. I've noticed that the normal vector points that cannot be selected are usually very small, with one component truncates to 0 due to SoildWorks' limitation (I suppose it's 8 digits after the decimal?). Is there any way I can fix this? I was told that for those extremely small values I can just manually change them to 0, I tried it but selectByID2 still doesn't work.

Below are the code from the problematic file:

' ******************************************************************************
' Function: automation electrode placement
' The line with a '*' should be changed if necessary
' There are some lines should never be changed
' ******************************************************************************
Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Dim SelMgr As Object
Dim Feature As Object
Dim filepath As String
Dim myMate As Object
Dim i As Integer
Dim instance As IPartDoc
Dim Xholder, Yholder, Zholder As Double
Dim aisname As String
Dim elename As String

Sub main()

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set SelMgr = Part.SelectionManager
Part.SketchManager.AddToDB True
Dim swSelData As SldWorks.SelectData

'*change the number according to how many electrode you have already insert. MAke sure the number of axis and electrode are the same
i = 1

'*add path and change name is necessary
'1--the position of the electrode
'2--the normal vector of the electrode
Open "E:\Louis Workspace\tDCS\419Coords.txt" For Input As #1
Open "E:\Louis Workspace\tDCS\419NorVecConverted.txt" For Input As #2
Do While Not EOF(1)

' *Change the name of Axis, it can be different according to the language
axisname = "Axis" + Trim(Str(i))

' *do remeber to change the assem name "@Assem6" can be different in different project
' * elename = "Point1@Origin@electrode_with_gel_on_z_4mm_diameter-" + Trim(Str(i)) + "@Assem419"
elename = "electrode_with_gel_on_z_4mm_diameter<" + Trim(Str(i)) + ">"

' Add electrode
Set Part = swApp.ActiveDoc
'*add the path and change name if necessary
boolstatus = Part.AddComponent("E:\Louis Workspace\tDCS\material\material\solidworks\electrode_with_gel_on_z_4mm_diameter.SLDPRT", 0#, 0#, 0#)

Input #2, X, Y, Z
'Add axis
boolstatus = Part.Extension.SelectByID2("", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
boolstatus = Part.InsertAxis2(True)

' set the direction
'change the axis name
boolstatus = Part.Extension.SelectByID2(axisname, "AXIS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "FACE", 0, 0, 0.002, True, 1, Nothing, 0)
Dim myMate As Object
'never change the following line
Set myMate = Part.AddMate4(2, -1, False, 8.38952298466863E-03, 0, 0, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, longstatus)
Part.ClearSelection2 True
Part.EditRebuild3

Input #1, X, Y, Z
' move to position
boolstatus = Part.Extension.SelectByID2(elename, "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("", "EXTSKETCHPOINT", X / 1000, Y / 1000, Z / 1000, True, 0, Nothing, 0)
'never change the following line
Set myMate = Part.AddMate4(0, -1, False, 0.114412498475579, 0, 0, 0.001, 0.001, 0.5235987755983, 0.5235987755983, 0.5235987755983, False, False, longstatus)
Part.ClearSelection2 True
Part.EditRebuild3

i = i + 1

Loop
Close #1
Close #2

End Sub

And here is one of the normal vector coordinates that cannot be selected:

0.00000069367179408369 0.00004339703586259640 0.00342425033616408000

(Since our model is in mm scale, these values are further divided by 1000 in the code when creating points. I've also tried to change the scale to mm in solidworks and got rid of the division, but again the selectByID2 doesn't recognize it)

Thank you so much for the help!