Search code examples
python-3.xdrawingcaddxf

How to find dimensions of a shape in DXF in python?


I am trying to get the shape's dimensions, from dxf files. I tried looking into dxfgrabber and ezdxf libraries, I took the lowest and highest points to get the dimensions, but the result was wrong. Using 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)

Using 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)

And manually took the minX minY maxX maxY.
Is there any way of calculating, or any existing libraries to get the dimensions ?


Solution

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

    Using the bounding extents of a element is not going to be correct. This is particularly true if the LINE is not vertical or horizontal. AS soon as it is at a rotation your logic will fail.

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

    I can't comment specifically on the software libraries you are using. They might only read in DXF files and offer no other features.

    It seems that you are working with 2D data. In your question is show:

    • LINE
    • ARC

    For LINE elements here is an example (in 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
    

    If you are working with 3D data then extend the method by introducing dDeltaZ.

    You should be able to port that over to your environment. A can't comment on ARC elements I am afraid.


    With a quick Google I found a related StackOverflow question which provides the formula for working out the length of an arc:

    Arc Length = Radius * (Angle In Radian)

    There is a secondary link in the comments with more information.

    So you should be able to work out your lengths accurately now.