gpt4 book ai didi

python-3.x - 如何在 python 中的 DXF 中查找形状的尺寸?

转载 作者:行者123 更新时间:2023-12-05 06:36:21 24 4
gpt4 key购买 nike

我正在尝试从 dxf 文件中获取形状的尺寸。我尝试查看 dxfgrabber 和 ezdxf 库,我采用了最低点和最高点来获取尺寸,但结果是错误的。使用 dxfgrabber:

import dxfgrabber
import numpy as np
import sys

dxf = dxfgrabber.readfile(sys.argv[1])
shapes = dxf.entities.get_entities()
minX, maxX, minY, maxY = 999999,0,999999,0
for shape in shapes:
print('dxftype', shape.dxftype)
if shape.dxftype == 'LINE':
x, y = shape.start[0], shape.start[1]
if x < minX:
minX = x
if y < minY:
minY = y
if x >= maxX:
maxX = x
if y >= maxY:
maxY = y
x, y = shape.end[0], shape.end[1]
if x < minX:
minX = x
if y < minY:
minY = y
if x >= maxX:
maxX = x
if y >= maxY:
maxY = y
if shape.dxftype == 'ARC':
x, y = shape.center[0], shape.center[1]
if x < minX:
minX = x
if y < minY:
minY = y
if x >= maxX:
maxX = x
if y >= maxY:
maxY = y
print('minX', minX, 'minY', minY)
print('maxX', maxX, 'maxY', maxY)
print('x1', 0, 'y1', 0)
print('x2', maxX-minX, 'y2', maxY-minY)

使用 ezdxf:

modelspace = dwg.modelspace()
for e in modelspace:
if e.dxftype() == 'LINE':
print("start point: ", e.dxf.start)
print("end point: ", e.dxf.end)
if e.dxftype() == 'ARC':
print("ARC on layer: ", e.dxf.layer)
print("center point ", e.dxf.center)

然后手动取了minX minY maxX maxY。
是否有任何计算方法或任何现有库来获取尺寸?

最佳答案

And manually took the minX, minY, maxX, maxY.

使用元素的边界范围是不正确的。如果 LINE 不是垂直或水平的,则尤其如此。一旦处于轮换,您的逻辑就会失败。

Is there any way of calculating, or any existing libraries to get the dimensions ?

我无法具体评论您正在使用的软件库。它们可能只读取 DXF 文件,不提供其他功能。

看来您正在处理二维数据。在你的问题中显示:

  • 在线

对于 LINE 元素,这是一个示例(在 Visual Basic for Applications 中):

Public Function Get2DLength(aryPoint1 As Variant, _
aryPoint2 As Variant) As Double

Dim dDeltaX As Double
Dim dDeltaY As Double

dDeltaX = aryPoint2(0) - aryPoint1(0)
dDeltaY = aryPoint2(1) - aryPoint1(1)

Get2DLength = Sqr((dDeltaX * dDeltaX) + (dDeltaY * dDeltaY))

End Function

如果您正在处理 3D 数据,则通过引入 dDeltaZ 来扩展该方法。

您应该能够将其移植到您的环境中。恐怕 A 不能评论 ARC 元素。


通过快速谷歌,我找到了一个相关的 StackOverflow question其中提供了计算弧长的公式:

Arc Length = Radius * (Angle In Radian)

有一个二级link在评论中提供更多信息。

所以您现在应该能够准确计算出您的长度。

关于python-3.x - 如何在 python 中的 DXF 中查找形状的尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com