Here is corrected code:
Sub CheckComponent2ComponentDistance()
lsbResult.Items.Clear()
Call MGCPCB_Connect()
Const epcbUnitMM As MGCPCB.EPcbUnit = 4
Const epcbSelectAll As MGCPCB.EPcbSelectionType = 0
Const epcbCompGeneral As MGCPCB.EPcbComponentType = 4
Const epcbCelltypePackage As MGCPCB.EPcbCelltype = 4
Const vbCrLf1 AsString = vbCrLf
Dim result1 AsString
result1 = ""
'Set the unit to be used
pcbDoc.CurrentUnit = epcbUnitMM
'Creat file to save check result
Dim overWriteInt AsInteger
overWriteInt = 1
' Create a FileSystemObject
Dim fileSysObj AsObject
fileSysObj = CreateObject("Scripting.FileSystemObject")
Dim txtStreamObj AsObject
txtStreamObj = fileSysObj.CreateTextFile(pcbDoc.Path & _
"LogFiles/Component and Component Report.txt", overWriteInt)
Dim i, j, num AsInteger
i = 0
j = 0
' Add a header
txtStreamObj.WriteLine()
txtStreamObj.WriteLine("component and component distance Report : ")
txtStreamObj.WriteLine()
'Collect component information
Dim comps As MGCPCB.Components
Dim comp1Obj, comp2Obj As MGCPCB.Component
comps = pcbDoc.Components(epcbSelectAll, epcbCompGeneral, epcbCelltypePackage, "*")
'check clearance between top component and component
ForEach comp1Obj In comps
If comp1Obj.RefDes = "" ThenGoTo next_comp1Obj
ForEach comp2Obj In comps
If comp2Obj.RefDes = "" ThenGoTo next_comp2Obj
If comp2Obj.RefDes = comp1Obj.RefDes ThenGoTo next_comp2Obj
IfNot comp2Obj.Side = comp1Obj.Side ThenGoTo next_comp2Obj
If pcbDoc.Clearance.GetActualMinClearance(comp1Obj, comp2Obj) < 0.254 Then
result1 = result1 & " " & comp1Obj.refdes & " and " & comp2Obj.refdes & " distance < 10mil" & vbCrLf
lsbResult.Items.Add(comp1Obj.refdes & " and " & comp2Obj.refdes & " distance < 10mil")
txtStreamObj.Write(comp1Obj.refdes & "and" & comp2Obj.refdes & " distance is " & pcbDoc.Clearance.GetActualMinClearance(comp1Obj, comp2Obj) & "mm" & "----" & "Spec is 10mil")
txtStreamObj.WriteLine()
EndIf
next_comp2Obj:
Next
next_comp1Obj:
Next
' Close the file
txtStreamObj.Close()
If result1 = "" Then
MsgBox("Don't need to check the distance between component and component.")
Else
MsgBox("Take care of: " & vbCrLf1 & result1)
EndIf
Call MGCPCB_Disconnect()
EndSub
I tested this code and it appears to work without any errors. I didn't look too deeply into other things going on in your project, but this function worked and produced information in the listbox, the messagebox, and output file.
A couple of tips:
1. I make a save_file subroutine that takes a string input with text to write in the file, and another string with the file path, and does all the file opening/creating, writing, and closing in one go. This reduces the amount of code in every sub that writes a text file, and it also keeps the file open for a lot less time. Plus in vb.net, there is the stringbuilder data type, which is not immutable like a normal string. For very big files, it's a huge timesaver because the text string does not have to be re-written from the beginning every time you add to it.
2. in all the cases where you define a constant with an integer value for the MGCPCB function arguments, you can just use the integer value in the function call. Like:
comps = pcbDoc.Components(0, 4, 4)
instead of
comps = pcbDoc.Components(epcbSelectAll, epcbCompGeneral, epcbCelltypePackage, "*")
note also the optional argument can be eliminated entirely. This saves a ton of variable declarations and typing in the code. You do have to remember what the integers mean, so sometimes you need to look them up in the help. I guess that's a style thing that I prefer
3. The Call statement is not needed when calling a subroutine.
4. In vb.net, use the debug.print("something") instead of msgbox to show messages in the debug window without interrupting program flow.
hope this helps