I am doing a project to explore an automatic method to build the multi-electrode CAD model using SolidWorks. I try to use macro to insert more than one thousand electrode models to a head model. I have already calculated all the coordinates and normal vectors of each electrode. But when I insert the points, some points are missed. I have tried a lot of methods, it still doesn't work. How to write the macro?
Here is my codes:
' ******************************************************************************
' 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
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 = 2
'*add path and change name is necessary
'1--the position of the electrode
'2--the normal vector of the electrode
Open "10-10systembyJWL.txt" For Input As #1
Open "normalv10-10sysbyJWL.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)) + "@Assem6"
' Add electrode
Set Part = swApp.ActiveDoc
'*add the path and change name if necessary
boolstatus = Part.AddComponent("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
The points have already insert. The problem is that some points are missed, and it is a huge disaster.
Explore an automatic method to build the multi-electrode CAD model using SolidWorks.
SolidworksApi macros