ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ¤ñ¸û¤@°ï¸ê®Æ¤¤³W«h°ÝÃD¡A¯à§_§PŪ

¦^´_ 1# boblovejoyce
¯ó¯ó§¹¦¨¦p¤Uµ{¦¡¡A
¦]¬°VBªº¥¿«hªí¹F¦¡¥\¯à®z¤FÂI
©Ò¥H¤S¦Û¤v¼g¤F FindCloseBracket §ä¥X¤Ç°tªº¥k¬A¸¹¦ì¸m
  1. Sub Test()
  2.     Dim fn As Integer, allText As String, indexClose As Long
  3.     Dim fname
  4.     fname = Application.GetOpenFilename()
  5.     If TypeName(fname) = "Boolean" Then Exit Sub
  6.     fn = FreeFile()
  7.     Open fname For Input As #fn
  8.     allText = Input$(LOF(1), 1)
  9.     Close #fn
  10.    
  11.     Sheets.Add
  12.     Cells(1, 1).Resize(, 3) = Array("net", "check", "portRef")
  13.     Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
  14.     Dim oMch As Object, oMch2 As Object, x, i As Long, j As Long, endindex As Long
  15.     Dim hasVDD As Boolean, hasGND As Boolean
  16.     With oRegex
  17.         .Global = True
  18.         .Pattern = "\(net\s+([^\s()]*)"     '(net ¶}ÀY¡A®·®»¤U¤@­Ó«DªÅ¥Õ©Î()ªº¦r²Õ
  19.         Set oMch = .Execute(allText)
  20.         
  21.         .Pattern = "\(portRef\s+([^\s()]*)"     '(portRef ¶}ÀY¡A®·®»¤U¤@­Ó«DªÅ¥Õ©Î()ªº¦r²Õ
  22.         i = 2
  23.         For Each x In oMch
  24.             endindex = FindCloseBracket(allText, x.firstIndex)
  25.             Set oMch2 = .Execute(Mid(allText, x.firstIndex, endindex - x.firstIndex + 1))
  26.             Cells(i, 1) = x.subMatches(0)
  27.             j = 3
  28.             hasVDD = False: hasGND = False
  29.             For Each y In oMch2
  30.                 If InStr(1, y.subMatches(0), "VDD", vbTextCompare) Then hasVDD = True
  31.                 If InStr(1, y.subMatches(0), "GND", vbTextCompare) Then hasGND = True
  32.                 Cells(i, j) = y.subMatches(0)
  33.                 j = j + 1
  34.             Next
  35.             Cells(i, 2) = IIf(oMch2.Count < 2 Or (hasVDD And hasGND), "X", "O")
  36.             
  37.             i = i + 1
  38.         Next
  39.     End With
  40. End Sub

  41. '   s: input string
  42. '   i: index of begin bracket "("
  43. Function FindCloseBracket(ByRef s As String, ByVal i As Long) As Long
  44.     Dim isInString As Boolean   'ex : ¹w¨¾Âù¤Þ¸¹¦r¦ê¤ºªº () »~§P
  45.     i = i + 1   '±q¤U¤@­Ó¦r¤¸¶}©l
  46.     Do While i <= Len(s)
  47.         Select Case Mid(s, i, 1)
  48.         Case "("
  49.             If Not isInString Then
  50.                 i = FindCloseBracket(s, i)
  51.                 If i = 0 Then Exit Do
  52.             End If
  53.         Case ")"
  54.             If Not isInString Then
  55.                 FindCloseBracket = i
  56.                 Exit Function
  57.             End If
  58.         Case """"
  59.             isInString = Not isInString
  60.         Case Else
  61.         End Select
  62.         
  63.         i = i + 1
  64.         DoEvents
  65.     Loop
  66.     FindCloseBracket = 0
  67. End Function
½Æ»s¥N½X
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

¦^´_ 8# boblovejoyce
¸ê®Æ¨º»ò¦hµ§­n¥ýÁ¿¡A¤W¶ÇªºÀɮפ~¨âµ§´N¨S¦³Àu¤Æ¤F
  1. Sub Test()   
  2.     '== Read File ==
  3.     Dim fn As Integer, allText As String
  4.     Dim fname
  5.     fname = Application.GetOpenFilename()
  6.     If TypeName(fname) = "Boolean" Then Exit Sub
  7.     fn = FreeFile()
  8.     Open fname For Input As #fn
  9.     allText = Input$(LOF(1), 1)
  10.     Close #fn
  11.         
  12.     '== Parse Element ==
  13.     Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
  14.     Dim oDic As Object: Set oDic = CreateObject("scripting.dictionary")
  15.     Dim oMch As Object, oMch2 As Object, x, i As Long, j As Long, endIndex As Long
  16.     Dim hasVDD As Boolean, hasGND As Boolean
  17.     With oRegex
  18.         .Global = True
  19.         .Pattern = "\(net\s+([^\s()]*)"     '(net ¶}ÀY¡A®·®»¤U¤@­Ó«DªÅ¥Õ©Î()ªº¦r²Õ
  20.         Set oMch = .Execute(allText)
  21.         
  22.         .Pattern = "\(portRef\s+([^\s()]*)"     '(portRef ¶}ÀY¡A®·®»¤U¤@­Ó«DªÅ¥Õ©Î()ªº¦r²Õ
  23.         i = 1
  24.         For Each x In oMch
  25.             endIndex = FindCloseBracket(allText, x.firstIndex)
  26.             If endIndex = 0 Then MsgBox "Fail to parse: " & vbNewLine & Mid(allText, x.firstIndex, 50) & "...": Exit Sub
  27.             Set oMch2 = .Execute(Mid(allText, x.firstIndex, endIndex - x.firstIndex + 1))
  28.             
  29.             hasVDD = False: hasGND = False
  30.             For Each y In oMch2
  31.                 If InStr(1, y.subMatches(0), "VDD", vbTextCompare) Then hasVDD = True
  32.                 If InStr(1, y.subMatches(0), "GND", vbTextCompare) Then hasGND = True
  33.             Next
  34.             
  35.             If oMch2.Count < 2 Or (hasVDD And hasGND) Then  'portRef§C©ó¨â­Ó©Î¦P®É§tVDD¤ÎGND¦r¦ê
  36.                 oDic.Add i, x.subMatches(0)
  37.                 i = i + 1
  38.             End If
  39.         Next
  40.     End With
  41.    
  42.     '== Output result ==
  43.     Dim ar
  44.     If oDic.Count = 0 Then MsgBox "All pass": Exit Sub
  45.     ar = oDic.items
  46.     With Sheets.Add
  47.         Application.ScreenUpdating = False
  48.         .[a1].Resize(UBound(ar) - LBound(ar) + 1) = OneDtoTwoD(ar)
  49.         Application.ScreenUpdating = True
  50.     End With
  51. End Sub

  52. '   s: input string
  53. '   i: index of begin bracket "("
  54. Function FindCloseBracket(ByRef s As String, ByVal i As Long) As Long
  55.     Dim isInString As Boolean   'ex : ¹w¨¾Âù¤Þ¸¹¦r¦ê¤ºªº () »~§P
  56.     i = i + 1   '±q¤U¤@­Ó¦r¤¸¶}©l
  57.     Do While i <= Len(s)
  58.         Select Case Mid(s, i, 1)
  59.         Case "("
  60.             If Not isInString Then
  61.                 i = FindCloseBracket(s, i)
  62.                 If i = 0 Then Exit Do
  63.             End If
  64.         Case ")"
  65.             If Not isInString Then
  66.                 FindCloseBracket = i
  67.                 Exit Function
  68.             End If
  69.         Case """"
  70.             isInString = Not isInString
  71.         Case Else
  72.         End Select
  73.         i = i + 1
  74.     Loop
  75.     FindCloseBracket = 0
  76. End Function

  77. Function OneDtoTwoD(ar, Optional base As Integer) As Variant    'transepose array of elements > 65535
  78.     Dim i, retn()
  79.     ReDim retn(base To base + UBound(ar) - LBound(ar), base To base)  ' transe to base
  80.     For i = LBound(ar) To UBound(ar)
  81.         retn(i - LBound(ar) + base, base) = ar(i)
  82.     Next
  83.     OneDtoTwoD = retn
  84. End Function
½Æ»s¥N½X
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

¥»©«³Ì«á¥Ñ stillfish00 ©ó 2016-3-11 19:42 ½s¿è

¦^´_ 12# boblovejoyce
®¦¡AfirstIndex ³oÃä§Ú¿ù¤F¡Amatch¨ì²Ä¤@­Ó¦r®É firstIndex = 0¡A©MVBA¤£¤@¼Ë¡AÃø©Ç¶]¨º»ò¤[¡A­×§ï¦p¤U

µM«á¦³¨Çrenameªº§Ú¤£ª¾­n¨ú­þ­Ó¦WºÙ¡A³£¼g¨ì²Ä¤@Äæ
  1. Sub Test()
  2.     '== Read File ==
  3.     Dim fn As Integer, allText As String
  4.     Dim fname
  5.     fname = Application.GetOpenFilename()
  6.     If TypeName(fname) = "Boolean" Then Exit Sub
  7.     fn = FreeFile()
  8.     Open fname For Input As #fn
  9.     allText = Input$(LOF(1), 1)
  10.     Close #fn
  11.         
  12.     '== Parse Element ==
  13.     Dim oRegex As Object: Set oRegex = CreateObject("vbscript.regexp")
  14.     Dim oDic As Object: Set oDic = CreateObject("scripting.dictionary")
  15.     Dim omch As Object, oMch2 As Object, x, i As Long, j As Long, endIndex As Long
  16.     Dim hasVDD As Boolean, hasGND As Boolean
  17.     With oRegex
  18.         .Global = True
  19.         .Pattern = "\(net\s+(\(rename.*\)|[^\s()]*)"     '(net ¶}ÀY¡A®·®»¤U¤@­Ó«DªÅ¥Õ©Î()ªº¦r²Õ©Îrename...
  20.         Set omch = .Execute(allText)
  21.         
  22.         .Pattern = "\(portRef\s+([^\s()]*)"     '(portRef ¶}ÀY¡A®·®»¤U¤@­Ó«DªÅ¥Õ©Î()ªº¦r²Õ
  23.         i = 1
  24.         For Each x In omch
  25.             endIndex = FindCloseBracket(allText, x.firstIndex + 1)
  26.             If endIndex = 0 Then MsgBox "Fail to parse: " & vbNewLine & Mid(allText, (x.firstIndex + 1), 50) & "...": Exit Sub
  27.             Set oMch2 = .Execute(Mid(allText, x.firstIndex + 1, endIndex - (x.firstIndex + 1) + 1))
  28.             
  29.             hasVDD = False: hasGND = False
  30.             For Each y In oMch2
  31.                 If InStr(1, y.subMatches(0), "VDD", vbTextCompare) Then hasVDD = True
  32.                 If InStr(1, y.subMatches(0), "GND", vbTextCompare) Then hasGND = True
  33.             Next
  34.             
  35.             If oMch2.Count < 2 Or (hasVDD And hasGND) Then  'portRef§C©ó¨â­Ó©Î¦P®É§tVDD¤ÎGND¦r¦ê
  36.                 oDic.Add i, x.subMatches(0)
  37.                 i = i + 1
  38.             End If
  39.         Next
  40.     End With
  41.    
  42.     '== Output result ==
  43.     Dim ar
  44.     If oDic.Count = 0 Then MsgBox "All pass": Exit Sub
  45.     ar = oDic.items
  46.     With Sheets.Add
  47.         Application.ScreenUpdating = False
  48.         .[a1].Resize(UBound(ar) - LBound(ar) + 1) = OneDtoTwoD(ar)
  49.         Application.ScreenUpdating = True
  50.     End With
  51. End Sub

  52. '   s: input string
  53. '   i: index of begin bracket "("
  54. Function FindCloseBracket(ByRef s As String, ByVal i As Long) As Long
  55.     Dim isInString As Boolean   'ex : ¹w¨¾Âù¤Þ¸¹¦r¦ê¤ºªº () »~§P
  56.     i = i + 1   '±q¤U¤@­Ó¦r¤¸¶}©l
  57.     Do While i <= Len(s)
  58.         Select Case Mid(s, i, 1)
  59.         Case "("
  60.             If Not isInString Then
  61.                 i = FindCloseBracket(s, i)
  62.                 If i = 0 Then Exit Do
  63.             End If
  64.         Case ")"
  65.             If Not isInString Then
  66.                 FindCloseBracket = i
  67.                 Exit Function
  68.             End If
  69.         Case """"
  70.             isInString = Not isInString
  71.         Case Else
  72.         End Select
  73.         i = i + 1
  74.     Loop
  75.     FindCloseBracket = 0
  76. End Function

  77. Function OneDtoTwoD(ar, Optional base As Integer) As Variant    'transepose array of elements > 65535
  78.     Dim i, retn()
  79.     ReDim retn(base To base + UBound(ar) - LBound(ar), base To base)  ' transe to base
  80.     For i = LBound(ar) To UBound(ar)
  81.         retn(i - LBound(ar) + base, base) = ar(i)
  82.     Next
  83.     OneDtoTwoD = retn
  84. End Function
½Æ»s¥N½X
ªí¹F¤£²M¡BÃD·N¤£©ú½T¡B¨SªþÀɮ׮榡¡B¨S¦³°Q½×°ÝÃDªººA«×~~~~~~¥H¤W·R²ö¯à§U¡C

TOP

        ÀR«ä¦Û¦b : ¡i®É¶¡µLªk¾B¾×¡j©È®É¶¡®ø³u¡Aªá¤F³\¦h¤ß¦å¡A·QºÉ¦U¦¡¤èªk­n¾B¾×®É¶¡¡Aµ²ªG¬O¡G®ö¶O¤F§ó¦h®É¶¡¡A¥B¤@µL©Ò¦¨¡I
ªð¦^¦Cªí ¤W¤@¥DÃD