discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

My Dxf2Turtle python script

YV
yur_vol@yahoo.com
Thu, Jun 18, 2026 8:22 AM

I wrote simple python script to convert my dxf files to openscad/bosl2 turtle script.

Only LINE and ARC supported as I build dxf manually upon reference image.

Script uses ezdxf python lib. Hope it will be usefull for you too.

Please report any mistakes


import ezdxf
import sys
import math
dwg_file = ezdxf.readfile(sys.argv[1])
modelspace = dwg_file.modelspace()
arcleft="\"arcleft\""
arcright="\"arcright\""
arcarc=arcleft
lv = 0;
dv = [0,0];
print ("include <BOSL2/std.scad>")
print ("include <BOSL2/turtle3d.scad>")
print (" ")
print ("//generated by dxf-to-turtle python script")
print (" path =  turtle([")
for entity in modelspace:
        dxf_type = entity.dxftype()
      #  print (dxf_type)
       # print  (" Entity attributes ", entity.dxfattribs())
for e in modelspace:
     if e.dxftype() == "LINE":
        # print  (" Entity attributes ", entity.dxfattribs())
         lv = math.sqrt((e.dxf.end[0] - e.dxf.start[0] )**2 + (e.dxf.end[1] - e.dxf.start[1] )**2)
         dv[0] = (e.dxf.end[0] - e.dxf.start[0] )/lv
         dv[1] = (e.dxf.end[1] - e.dxf.start[1] )/lv
         print ("\"setdir\",[", dv[0],",",dv[1] , "],")
         print ("\"move\",",lv,",")
			
     if e.dxftype() == "ARC":
        # print(f"Центр: {e.dxf.center}")
        # print(f"Радиус: {e.dxf.radius}")
        # print(f"Начальный угол: {e.dxf.start_angle}")
         #print(f"Конечный угол: {e.dxf.end_angle}")
         #print(f"//Вектор выдавливания: {e.dxf.extrusion}")		
         arc_angle=e.dxf.end_angle - e.dxf.start_angle
         if arc_angle < -90:
             arc_angle = arc_angle+360		 
			 
         if e.dxf.extrusion == (0,0,-1):  
#             if arc_angle <=180:
             arcarc=arcright			 
  #           else:
   #              arcarc=arcleft
  #       elif arc_angle <=180:
  #           arcarc = arcleft
         else:
             arcarc=arcleft				 
         print( arcarc ,","  ,     e.dxf.radius    ,   ","   ,    arc_angle,     ",   // ", e.dxf.extrusion)
print("]);")
print("path_sweep2d(circle(1),path);")
I wrote simple python script to convert my dxf files to openscad/bosl2 turtle script. Only LINE and ARC supported as I build dxf manually upon reference image. Script uses ezdxf python lib. Hope it will be usefull for you too.\ \ Please report any mistakes --- ``` import ezdxf ``` ``` import sys ``` ``` import math ``` ``` dwg_file = ezdxf.readfile(sys.argv[1]) ``` ``` modelspace = dwg_file.modelspace() ``` ``` arcleft="\"arcleft\"" ``` ``` arcright="\"arcright\"" ``` ``` arcarc=arcleft ``` ``` lv = 0; ``` ``` dv = [0,0]; ``` ``` print ("include <BOSL2/std.scad>") ``` ``` print ("include <BOSL2/turtle3d.scad>") ``` ``` print (" ") ``` ``` print ("//generated by dxf-to-turtle python script") ``` ``` print (" path = turtle([") ``` ``` for entity in modelspace: ``` ``` dxf_type = entity.dxftype() ``` ``` # print (dxf_type) ``` ``` # print (" Entity attributes ", entity.dxfattribs()) ``` ``` for e in modelspace: ``` ``` if e.dxftype() == "LINE": ``` ``` # print (" Entity attributes ", entity.dxfattribs()) ``` ``` lv = math.sqrt((e.dxf.end[0] - e.dxf.start[0] )**2 + (e.dxf.end[1] - e.dxf.start[1] )**2) ``` ``` dv[0] = (e.dxf.end[0] - e.dxf.start[0] )/lv ``` ``` dv[1] = (e.dxf.end[1] - e.dxf.start[1] )/lv ``` ``` print ("\"setdir\",[", dv[0],",",dv[1] , "],") ``` ``` print ("\"move\",",lv,",") ``` ``` ``` ``` if e.dxftype() == "ARC": ``` ``` # print(f"Центр: {e.dxf.center}") ``` ``` # print(f"Радиус: {e.dxf.radius}") ``` ``` # print(f"Начальный угол: {e.dxf.start_angle}") ``` ``` #print(f"Конечный угол: {e.dxf.end_angle}") ``` ``` #print(f"//Вектор выдавливания: {e.dxf.extrusion}") ``` ``` arc_angle=e.dxf.end_angle - e.dxf.start_angle ``` ``` if arc_angle < -90: ``` ``` arc_angle = arc_angle+360 ``` ``` ``` ``` if e.dxf.extrusion == (0,0,-1): ``` ``` # if arc_angle <=180: ``` ``` arcarc=arcright ``` ``` # else: ``` ``` # arcarc=arcleft ``` ``` # elif arc_angle <=180: ``` ``` # arcarc = arcleft ``` ``` else: ``` ``` arcarc=arcleft ``` ``` print( arcarc ,"," , e.dxf.radius , "," , arc_angle, ", // ", e.dxf.extrusion) ``` ``` print("]);") ``` ``` print("path_sweep2d(circle(1),path);") ```