Skip to content

Instantly share code, notes, and snippets.

@StephS
Created July 10, 2019 18:12
Show Gist options
  • Save StephS/510d121bd5ca4eb8dd85d5c4ec74c658 to your computer and use it in GitHub Desktop.
Save StephS/510d121bd5ca4eb8dd85d5c4ec74c658 to your computer and use it in GitHub Desktop.
DXF Circles to Points
import ezdxf
import argparse
class DiameterToRadius(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print('%r %r %r' % (namespace, values, option_string))
if values > 0:
values = values/2.0
setattr(namespace, self.dest, values)
parser = argparse.ArgumentParser(description="Converts circles to points in DXF File")
parser.add_argument("input", help="The input DXF File name")
parser.add_argument("output", help="The output DXF File name")
group = parser.add_mutually_exclusive_group()
group.add_argument("-r", "--radius", type=float, dest='radius',
help="maximum radius of circle to convert to point (Default=4)")
group.add_argument("-d", "--diameter", type=float, dest='radius', action=DiameterToRadius, metavar="DIAMETER",
help="maximum diameter of circle to convert to point (Default=8)")
parser.set_defaults(radius=4)
args = parser.parse_args()
if args.radius and args.radius < 0:
parser.error("Cannot set radius less than zero")
dwg = ezdxf.readfile(args.input)
modelspace = dwg.modelspace()
for circle in modelspace.query('CIRCLE'):
if circle.dxf.radius < args.radius:
modelspace.delete_entity(circle)
point = modelspace.add_point(circle.dxf.center, dxfattribs={'layer': circle.dxf.layer})
print(circle)
print(point)
dwg.saveas(args.output)
@StephS
Copy link
Author

StephS commented Jul 10, 2019

Opens a DXF file and converts all circles under a specified radius to points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment