2 Replies Latest reply on Jun 3, 2015 7:51 AM by zmg2007

    How to use "item" property in VB.NET

    zmg2007

      Hi:

      I have a VBS code which can run in expedition.but  i write the code using VB.NET. there are error about "item" property.

      can you help me? thanks

       

      vbs code:

       

      dim result1:result1=""

      'Set the unit to be used

      pcbDocObj.CurrentUnit = epcbUnitMM

       

       

      'Creat file to save check result

      Dim overWriteInt: overWriteInt = 1

      ' Create a FileSystemObject

      Dim fileSysObj

      Set fileSysObj = CreateObject("Scripting.FileSystemObject")

      Dim txtStreamObj

      Set txtStreamObj = fileSysObj.CreateTextFile(pcbDocObj.Path & _

                               "LogFiles/Component and Component Report.txt", overWriteInt)

       

       

      dim i,j

       

       

      ' Add a header

      txtStreamObj.WriteLine()

      txtStreamObj.WriteLine("component and component distance Report : " &Date&"  "&time)

      txtStreamObj.WriteLine()

       

       

      'Collect component information

      Dim cmpsTopColl,cmpTopObj

      Set cmpsTopColl = pcbDocObj.Components(epcbSelectAll,epcbCompGeneral,epcbCelltypePackage,"*")

       

       

       

       

      cmpsTopColl.Sort

      'check clearance between top component and component

      for i = 1 to (cmpsTopColl.count -1)

          for j = (i+1)  to (cmpsTopColl.count) 

              if cmpsTopColl.item(i).Side = cmpsTopColl.item(j).Side then

                  'clearance between component and component  PlaceMinBot10mils

                  if pcbdocobj.clearance.getactualminclearance(cmpsTopColl.item(i),cmpsTopColl.item(j))<0.254 then

                    result1 = result1 & "  " & cmpsTopColl.item(i).refdes & " and "&cmpsTopColl.item(j).refdes &" distance < 10mil"& vbCrLf

                    txtStreamObj.Write(cmpsTopColl.item(i).refdes & "and"&cmpsTopColl.item(j).refdes &" distance is "&pcbdocobj.clearance.getactualminclearance(cmpsTopColl.item(i),cmpsTopColl.item(j))&"mm"&"----"&"Spec is 10mil")

                   txtStreamObj.WriteLine()

                   end if

              End if

          Next

      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: "& vbCrLf&result1)

      end if

       

      VB.net Code (VS2010):

      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 As String = vbCrLf

       

       

              Dim result1 As String

              result1 = ""

              'Set the unit to be used

              pcbDoc.CurrentUnit = epcbUnitMM

       

       

              'Creat file to save check result

              Dim overWriteInt As Integer

              overWriteInt = 1

              ' Create a FileSystemObject

              Dim fileSysObj As Object

              fileSysObj = CreateObject("Scripting.FileSystemObject")

              Dim txtStreamObj As Object

              txtStreamObj = fileSysObj.CreateTextFile(pcbDoc.Path & _

                                       "LogFiles/Component and Component Report.txt", overWriteInt)

       

       

              Dim i, j, num As Integer

              i = 0

              j = 0

              ' Add a header

              txtStreamObj.WriteLine()

              txtStreamObj.WriteLine("component and component distance Report : ")

              txtStreamObj.WriteLine()

       

       

              'Collect component information

              Dim cmpsColl, comp1Obj, comp2Obj As Object

              cmpsColl = pcbDoc.Components(epcbSelectAll, epcbCompGeneral, epcbCelltypePackage, "*")

              'cmpsColl.Sort()

              'cmpsColl.Highlighted = True

              'num = cmpsColl.Count

       

       

              'check clearance between top component and component

              For i = cmpsColl.Count To 2 Step -1

                  comp1Obj = cmpsColl.Item(i)

                  MsgBox(comp1Obj.Side)

                  For j = (i - 1) To 1 Step -1

                      comp2Obj = cmpsColl.Item(j)

                      If cmpsColl.Item(i) Is Nothing Or comp2Obj Is Nothing Then

                          'MsgBox("t")

                      Else

                          If comp1Obj.Side = comp2Obj.Side Then

                              'clearance between component and component  PlaceMinBot10mils

                              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()

                              End If

                          End If

                      End If

                  Next

              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)

              End If

              Call MGCPCB_Disconnect()

          End Sub

        • 1. Re: How to use "item" property in VB.NET
          Patrick.Cashman

          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 As String = vbCrLf

                  

                  

           

                   Dim result1 As String

                   result1 = ""

                  

                   'Set the unit to be used

                   pcbDoc.CurrentUnit = epcbUnitMM

           

                   'Creat file to save check result

                   Dim overWriteInt As Integer

                   overWriteInt = 1

                  

                   ' Create a FileSystemObject

                   Dim fileSysObj As Object

                   fileSysObj = CreateObject("Scripting.FileSystemObject")

                   Dim txtStreamObj As Object

                   txtStreamObj = fileSysObj.CreateTextFile(pcbDoc.Path & _

                                            "LogFiles/Component and Component Report.txt", overWriteInt)

           

                   Dim i, j, num As Integer

                   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

                   For Each comp1Obj In comps

                       If comp1Obj.RefDes = "" Then GoTo next_comp1Obj

                       For Each comp2Obj In comps

                           If comp2Obj.RefDes = "" Then GoTo next_comp2Obj

                           If comp2Obj.RefDes = comp1Obj.RefDes Then GoTo next_comp2Obj

                           If Not comp2Obj.Side = comp1Obj.Side Then GoTo 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()

                           End If

                           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)

                   End If

                   Call MGCPCB_Disconnect()

          End Sub

           

           

           

          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

          1 of 1 people found this helpful
          • 2. Re: How to use "item" property in VB.NET
            zmg2007

            Patrick.Cashman,thanks you very much for you reply!   your suggestion is very helpful , thanks you!

            1 of 1 people found this helpful