明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 464|回复: 1

[【PyCAD】] 做一个简单的文字排序,可作为CAD文字转表格用法

[复制链接]
发表于 2024-1-17 23:06 | 显示全部楼层 |阅读模式
  1. import math
  2. import typing as typ


  3. class TextBound:
  4.     def __init__(self, text: DBText):
  5.         self.DbText = text
  6.         self.Bounds = self.DbText.GeometricExtents
  7.         self.Xmax = self.Bounds.MaxPoint.X
  8.         self.Xmin = self.Bounds.MinPoint.X
  9.         self.Ymax = self.Bounds.MaxPoint.Y
  10.         self.Ymin = self.Bounds.MinPoint.Y
  11.         self.Center = Point2d(0.5 * (self.Xmax + self.Xmin), 0.5 * (self.Ymax + self.Ymin))
  12.         self.Width = self.Xmax - self.Xmin
  13.         self.Height = self.Ymax - self.Ymin
  14.         self.Row = 0
  15.         self.Column = 0

  16.     def __str__(self) -> str:
  17.         return self.DbText.TextString

  18.     def AreSameRow(self, other: "TextBound", compare_Y=None):
  19.         if compare_Y is None:
  20.             compare_Y = (self.Height + other.Height) / 2
  21.         if abs(self.Center.Y - other.Center.Y) < compare_Y:
  22.             return True
  23.         else:
  24.             return False

  25.     def AreSameCol(self, other: "TextBound", compare_X=None):
  26.         if compare_X is None:
  27.             compare_X = self.Width + other.Width
  28.         if abs(self.Center.X - other.Center.X) <= compare_X:
  29.             return True
  30.         else:
  31.             return False


  32. @Command()
  33. def GroupText(doc):
  34.     with dbtrans(doc) as tr:
  35.         res = ssget(":A", TV(0, "*text"))
  36.         if not res.ok:
  37.             return
  38.         ids = tuple(res)
  39.         texts: typ.List[DBText] = [tr.getobject(id) for id in ids]
  40.         textBounds = [TextBound(text) for text in texts]
  41.         textBounds.sort(key=lambda bound: (-bound.Center.Y, bound.Center.X))
  42.         base0 = textBounds[0]
  43.         groupedTexts = [[base0]]
  44.         row = 0
  45.         for i in range(1, len(textBounds)):
  46.             addedToGroup = False
  47.             for j in range(row + 1):
  48.                 if textBounds[i].AreSameRow(groupedTexts[j][-1]):
  49.                     groupedTexts[j].append(textBounds[i])
  50.                     addedToGroup = True
  51.                     break
  52.             if not addedToGroup:
  53.                 groupedTexts.append([textBounds[i]])
  54.                 row += 1

  55.         for i, group in enumerate(groupedTexts):
  56.             for bound in group:
  57.                 bound.Row = i

  58.         textBounds.sort(key=lambda bound: (bound.Center.X, -bound.Center.Y))
  59.         base0 = textBounds[0]
  60.         groupedTexts = [[base0]]
  61.         col = 0
  62.         for i in range(1, len(textBounds)):
  63.             addedToGroup = False
  64.             for j in range(col + 1):
  65.                 if textBounds[i].AreSameCol(groupedTexts[j][-1]):
  66.                     groupedTexts[j].append(textBounds[i])
  67.                     addedToGroup = True
  68.                     break
  69.             if not addedToGroup:
  70.                 groupedTexts.append([textBounds[i]])
  71.                 col += 1
  72.         for i, group in enumerate(groupedTexts):
  73.             for bound in group:
  74.                 bound.Col = i
  75.         rowmax = max(bound.Row for bound in textBounds) + 1
  76.         colmax = max(bound.Col for bound in textBounds) + 1
  77.         arr = [["NA" for _ in range(colmax)] for _ in range(rowmax)]
  78.         for bound in textBounds:
  79.             arr[bound.Row][bound.Col] = bound
  80.         transposed_arr = [list(row) for row in zip(*arr)]
  81.         string = "\n".join(["\t".join([bound for bound in bounds]) for bounds in transposed_arr])
  82.         Clipboard.Clear()
  83.         Clipboard.SetText(string)
复制代码

发表于 2024-1-19 12:02 | 显示全部楼层
怎么使用啊,太高端了。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-7 06:26 , Processed in 0.367025 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表