| 
积分2244明经币 个注册时间2022-4-4在线时间 小时威望 金钱 个贡献 激情  
 | 
 
| 复制代码import math
import typing as typ
class TextBound:
    def __init__(self, text: DBText):
        self.DbText = text
        self.Bounds = self.DbText.GeometricExtents
        self.Xmax = self.Bounds.MaxPoint.X
        self.Xmin = self.Bounds.MinPoint.X
        self.Ymax = self.Bounds.MaxPoint.Y
        self.Ymin = self.Bounds.MinPoint.Y
        self.Center = Point2d(0.5 * (self.Xmax + self.Xmin), 0.5 * (self.Ymax + self.Ymin))
        self.Width = self.Xmax - self.Xmin
        self.Height = self.Ymax - self.Ymin
        self.Row = 0
        self.Column = 0
    def __str__(self) -> str:
        return self.DbText.TextString
    def AreSameRow(self, other: "TextBound", compare_Y=None):
        if compare_Y is None:
            compare_Y = (self.Height + other.Height) / 2
        if abs(self.Center.Y - other.Center.Y) < compare_Y:
            return True
        else:
            return False
    def AreSameCol(self, other: "TextBound", compare_X=None):
        if compare_X is None:
            compare_X = self.Width + other.Width
        if abs(self.Center.X - other.Center.X) <= compare_X:
            return True
        else:
            return False
@Command()
def GroupText(doc):
    with dbtrans(doc) as tr:
        res = ssget(":A", TV(0, "*text"))
        if not res.ok:
            return
        ids = tuple(res)
        texts: typ.List[DBText] = [tr.getobject(id) for id in ids]
        textBounds = [TextBound(text) for text in texts]
        textBounds.sort(key=lambda bound: (-bound.Center.Y, bound.Center.X))
        base0 = textBounds[0]
        groupedTexts = [[base0]]
        row = 0
        for i in range(1, len(textBounds)):
            addedToGroup = False
            for j in range(row + 1):
                if textBounds[i].AreSameRow(groupedTexts[j][-1]):
                    groupedTexts[j].append(textBounds[i])
                    addedToGroup = True
                    break
            if not addedToGroup:
                groupedTexts.append([textBounds[i]])
                row += 1
        for i, group in enumerate(groupedTexts):
            for bound in group:
                bound.Row = i
        textBounds.sort(key=lambda bound: (bound.Center.X, -bound.Center.Y))
        base0 = textBounds[0]
        groupedTexts = [[base0]]
        col = 0
        for i in range(1, len(textBounds)):
            addedToGroup = False
            for j in range(col + 1):
                if textBounds[i].AreSameCol(groupedTexts[j][-1]):
                    groupedTexts[j].append(textBounds[i])
                    addedToGroup = True
                    break
            if not addedToGroup:
                groupedTexts.append([textBounds[i]])
                col += 1
        for i, group in enumerate(groupedTexts):
            for bound in group:
                bound.Col = i
        rowmax = max(bound.Row for bound in textBounds) + 1
        colmax = max(bound.Col for bound in textBounds) + 1
        arr = [["NA" for _ in range(colmax)] for _ in range(rowmax)]
        for bound in textBounds:
            arr[bound.Row][bound.Col] = bound
        transposed_arr = [list(row) for row in zip(*arr)]
        string = "\n".join(["\t".join([bound for bound in bounds]) for bounds in transposed_arr])
        Clipboard.Clear()
        Clipboard.SetText(string)
 | 
 |