做一个简单的文字排序,可作为CAD文字转表格用法
import mathimport 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 =
textBounds =
textBounds.sort(key=lambda bound: (-bound.Center.Y, bound.Center.X))
base0 = textBounds
groupedTexts = []
row = 0
for i in range(1, len(textBounds)):
addedToGroup = False
for j in range(row + 1):
if textBounds.AreSameRow(groupedTexts[-1]):
groupedTexts.append(textBounds)
addedToGroup = True
break
if not addedToGroup:
groupedTexts.append(])
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
groupedTexts = []
col = 0
for i in range(1, len(textBounds)):
addedToGroup = False
for j in range(col + 1):
if textBounds.AreSameCol(groupedTexts[-1]):
groupedTexts.append(textBounds)
addedToGroup = True
break
if not addedToGroup:
groupedTexts.append(])
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
transposed_arr =
string = "\n".join(["\t".join() for bounds in transposed_arr])
Clipboard.Clear()
Clipboard.SetText(string)
怎么使用啊,太高端了。。
页:
[1]