Пример #1
0
def generate_output_file(ai_figure, ai_output_filename, ai_height, ai_info_txt=""):
    """ implement the swith --output_file_basename for 2D figure
  """
    if ai_output_filename != "":
        # create the output directory if needed
        l_output_dir = os.path.dirname(ai_output_filename)
        design_help.mkdir_p(l_output_dir)
        # l_output_basename = os.path.basename(ai_output_filename)
        # print("dbg449: l_output_basename:", l_output_basename)
        # mozman dxfwrite
        if re.search("\.dxf$", ai_output_filename):
            # print("Generate {:s} with mozman dxfwrite".format(ai_output_filename))
            outline_backends.write_figure_in_dxf(ai_figure, ai_output_filename)
        # mozman svgwrite
        elif re.search("\.svg$", ai_output_filename):
            # print("Generate {:s} with mozman svgwrite".format(ai_output_filename))
            outline_backends.write_figure_in_svg(ai_figure, ai_output_filename)
        # FreeCAD
        elif re.search("\.brep$", ai_output_filename):
            print("Generate with FreeCAD the BRep file {:s}".format(ai_output_filename))
            freecad_part = outline_backends.figure_to_freecad_25d_part(ai_figure, ai_height)
            freecad_part.exportBrep("{:s}".format(ai_output_filename))
            print("Generate with FreeCAD the DXF file {:s}.dxf".format(ai_output_filename))
            # slice freecad_part  in the XY plan at a height of ai_height/2
            export_2d.export_to_dxf(
                freecad_part, Base.Vector(0, 0, 1), ai_height / 2, "{:s}.dxf".format(ai_output_filename)
            )
        elif re.search("\.stl$", ai_output_filename):
            print("Generate with FreeCAD the STL file {:s}".format(ai_output_filename))
            freecad_part = outline_backends.figure_to_freecad_25d_part(ai_figure, ai_height)
            freecad_part.exportStl("{:s}".format(ai_output_filename))
            print("Generate with FreeCAD the DXF file {:s}.dxf".format(ai_output_filename))
            # slice freecad_part  in the XY plan at a height of ai_height/2
            export_2d.export_to_dxf(
                freecad_part, Base.Vector(0, 0, 1), ai_height / 2, "{:s}.dxf".format(ai_output_filename)
            )
        else:
            print(
                "ERR124: Error: the suffix of the filename {:s} is unknown. Try with suffix: .dxf, .svg, .brep or .stl".format(
                    ai_output_filename
                )
            )
            sys.exit(2)
        # info_txt
        # if(ai_info_txt!=''):
        #  output_basename = re.sub('(\.dxf$)|(\.svg$)', '', ai_output_filename)
        #  info_txt_filename = "{:s}.txt".format(output_basename)
        #  print("Generate the text info file {:s}".format(info_txt_filename))
        #  ofh = open(info_txt_filename, 'w')
        #  ofh.write("{:s} generated by Cnc25D on {:s}\n\n".format(info_txt_filename, datetime.now().isoformat()))
        #  ofh.write(ai_info_txt)
        #  ofh.close()
    # return
    return 0
Пример #2
0
def generate_output_file(ai_figure,
                         ai_output_filename,
                         ai_height,
                         ai_info_txt=''):
    """ implement the swith --output_file_basename for 2D figure
  """
    if (ai_output_filename != ''):
        # create the output directory if needed
        l_output_dir = os.path.dirname(ai_output_filename)
        design_help.mkdir_p(l_output_dir)
        #l_output_basename = os.path.basename(ai_output_filename)
        #print("dbg449: l_output_basename:", l_output_basename)
        # mozman dxfwrite
        if (re.search('\.dxf$', ai_output_filename)):
            #print("Generate {:s} with mozman dxfwrite".format(ai_output_filename))
            outline_backends.write_figure_in_dxf(ai_figure, ai_output_filename)
        # mozman svgwrite
        elif (re.search('\.svg$', ai_output_filename)):
            #print("Generate {:s} with mozman svgwrite".format(ai_output_filename))
            outline_backends.write_figure_in_svg(ai_figure, ai_output_filename)
        # FreeCAD
        elif (re.search('\.brep$', ai_output_filename)):
            print("Generate with FreeCAD the BRep file {:s}".format(
                ai_output_filename))
            freecad_part = outline_backends.figure_to_freecad_25d_part(
                ai_figure, ai_height)
            freecad_part.exportBrep("{:s}".format(ai_output_filename))
            print("Generate with FreeCAD the DXF file {:s}.dxf".format(
                ai_output_filename))
            # slice freecad_part  in the XY plan at a height of ai_height/2
            export_2d.export_to_dxf(freecad_part, Base.Vector(0, 0, 1),
                                    ai_height / 2,
                                    "{:s}.dxf".format(ai_output_filename))
        elif (re.search('\.stl$', ai_output_filename)):
            print("Generate with FreeCAD the STL file {:s}".format(
                ai_output_filename))
            freecad_part = outline_backends.figure_to_freecad_25d_part(
                ai_figure, ai_height)
            freecad_part.exportStl("{:s}".format(ai_output_filename))
            print("Generate with FreeCAD the DXF file {:s}.dxf".format(
                ai_output_filename))
            # slice freecad_part  in the XY plan at a height of ai_height/2
            export_2d.export_to_dxf(freecad_part, Base.Vector(0, 0, 1),
                                    ai_height / 2,
                                    "{:s}.dxf".format(ai_output_filename))
        else:
            print(
                "ERR124: Error: the suffix of the filename {:s} is unknown. Try with suffix: .dxf, .svg, .brep or .stl"
                .format(ai_output_filename))
            sys.exit(2)
        # info_txt
        #if(ai_info_txt!=''):
        #  output_basename = re.sub('(\.dxf$)|(\.svg$)', '', ai_output_filename)
        #  info_txt_filename = "{:s}.txt".format(output_basename)
        #  print("Generate the text info file {:s}".format(info_txt_filename))
        #  ofh = open(info_txt_filename, 'w')
        #  ofh.write("{:s} generated by Cnc25D on {:s}\n\n".format(info_txt_filename, datetime.now().isoformat()))
        #  ofh.write(ai_info_txt)
        #  ofh.close()
    # return
    return (0)
Пример #3
0
def outline_arc_line_test1():
    """ test the functions outline_arc_line and outline_circle.
  """
    l_ol1 = [[0, 0], [20, 0], [20, 20], [0, 20], [0, 0]]

    l_ol2 = [[110, 0], [120, 0], [130, 0, 130, 10], [130, 20],
             [130, 30, 120, 30], [110, 30], [100, 30, 100, 20], [100, 10],
             [100, 0, 110, 0]]

    l_ol3 = [[210, 0], [220, 0], [230, 0, 230, 10], [230, 20],
             [230, 30, 220, 30], [210, 30], [200, 30, 200, 20], [200, 10]]
    #[200,0, 210,0]]

    # check CC (clock wise)
    l_ol4 = [[300, 10], [300, 20], [300, 30, 310, 30], [320, 30],
             [330, 30, 330, 20], [330, 10], [330, 0, 320, 0], [310, 0]]

    l_ol5 = [[0, 100], [100, 150], [110, 155, 120, 150], [150, 110],
             [160, 100, 170, 105], [200, 200], [0, 200], [0, 100]]

    l_ols = [l_ol1, l_ol2, l_ol3, l_ol4, l_ol5]
    #l_ols = [l_ol2]
    # circle
    l_circle_center = [200, 200]
    l_circle_radius = 150
    # backend freecad
    print("dbg701: test1 backend freecad")
    for i_ol in l_ols:
        r_ol = outline_arc_line(i_ol, 'freecad')
        #Part.show(r_ol)
        l_test_face = Part.Face(Part.Wire(r_ol.Edges))
        r_test_solid = l_test_face.extrude(Base.Vector(
            0, 0, 1))  # straight linear extrusion
        Part.show(r_test_solid)
    r_ol = outline_circle(l_circle_center, l_circle_radius, 'freecad')
    l_test_face = Part.Face(Part.Wire(r_ol.Edges))
    r_test_solid = l_test_face.extrude(Base.Vector(
        0, 0, 1))  # straight linear extrusion
    Part.show(r_test_solid)
    # create the output directory
    l_output_dir = "test_output"
    print("Create the output directory: {:s}".format(l_output_dir))
    design_help.mkdir_p(l_output_dir)
    # backend svgwrite
    print("dbg702: test1 backend svgwrite")
    output_svg_file_name = "{:s}/outline_arc_line_test1_00.svg".format(
        l_output_dir)
    object_svg = svgwrite.Drawing(filename=output_svg_file_name)
    #output_file_idx = 0
    for i_ol in l_ols:
        #output_file_idx += 1
        #output_svg_file_name =  "outline_arc_line_test1_{:02d}.svg".format(output_file_idx)
        #object_svg = svgwrite.Drawing(filename = output_svg_file_name)
        svg_outline = outline_arc_line(i_ol, 'svgwrite')
        for one_line_or_arc in svg_outline:
            object_svg.add(one_line_or_arc)
        #object_svg.save()
    one_circle = outline_circle(l_circle_center, l_circle_radius, 'svgwrite')
    object_svg.add(one_circle[0])
    object_svg.save()
    # backend dxfwrite
    print("dbg703: test1 backend dxfwrite")
    output_dxf_file_name = "{:s}/outline_arc_line_test1_00.dxf".format(
        l_output_dir)
    object_dxf = DXFEngine.drawing(output_dxf_file_name)
    #object_dxf.add_layer(default_dxf_layer_name)
    for i_ol in l_ols:
        dxf_outline = outline_arc_line(i_ol, 'dxfwrite')
        for one_line_or_arc in dxf_outline:
            object_dxf.add(one_line_or_arc)
    one_circle = outline_circle(l_circle_center, l_circle_radius, 'dxfwrite')
    object_dxf.add(one_circle[0])
    object_dxf.save()
    # backend tkinter
    print("dbg704: test1 backend tkinter")
    tk_root = Tkinter.Tk()
    #my_canvas = display_backend.Two_Canvas(tk_root)
    my_canvas = Two_Canvas(tk_root)

    # callback function for display_backend
    def sub_canvas_graphics(ai_rotation_direction, ai_angle_position):
        # angle position
        l_angle_position = float(ai_angle_position) / 100
        #
        r_canvas_graphics = []
        for i_ol in l_ols:
            r_canvas_graphics.append(
                ('graphic_lines', outline_arc_line(i_ol, 'tkinter'), 'red', 2))
        r_canvas_graphics.append(
            ('graphic_lines',
             outline_circle(l_circle_center, l_circle_radius,
                            'tkinter'), 'blue', 2))
        return (r_canvas_graphics)

    # end of callback function
    my_canvas.add_canvas_graphic_function(sub_canvas_graphics)
    tk_root.mainloop()
    del (my_canvas, tk_root
         )  # because Tkinter will be used again later in this script
    #time.sleep(0.3)
    ### test the figure-level functions
    wfl_outer_rectangle_B = [[-60, -40], [60, -40], [60, 40], [-60, 40],
                             [-60, -40]]

    wfl_inner_square_B = [[-10, -10], [10, -10], [10, 10], [-10, 10],
                          [-10, -10]]

    wfl_inner_circle1 = [30, 0, 15]
    wfl_inner_circle2 = [40, 0, 10]

    wfl_figure = [
        wfl_outer_rectangle_B, wfl_inner_square_B, wfl_inner_circle1,
        wfl_inner_circle2
    ]

    # display the figure
    figure_simple_display(wfl_figure)

    wfl_extrude_height = 20.0
    # create a FreeCAD part
    wfl_part = figure_to_freecad_25d_part(wfl_figure, wfl_extrude_height)

    # output file with mozman
    print("Generate {:s}/obt1_with_mozman.svg".format(l_output_dir))
    write_figure_in_svg(wfl_figure,
                        "{:s}/obt1_with_mozman.svg".format(l_output_dir))
    print("Generate {:s}/obt1_with_mozman.dxf".format(l_output_dir))
    write_figure_in_dxf(wfl_figure,
                        "{:s}/obt1_with_mozman.dxf".format(l_output_dir))

    # wfl_part in 3D BRep
    print("Generate {:s}/obt1_part.brep".format(l_output_dir))
    wfl_part.exportBrep("{:s}/obt1_part.brep".format(l_output_dir))
    # wfl_part in 2D DXF
    print("Generate {:s}/obt1_part.dxf".format(l_output_dir))
    export_2d.export_to_dxf(
        wfl_part, Base.Vector(0, 0, 1), wfl_extrude_height / 2,
        "{:s}/obt1_part.dxf".format(l_output_dir)
    )  # slice wfl_part in the XY plan at a height of wfl_extrude_height/2
    #
    r_test = 1
    return (r_test)
Пример #4
0
def outline_arc_line_test1():
  """ test the functions outline_arc_line and outline_circle.
  """
  l_ol1 = [
    [0,0],
    [20,0],
    [20,20],
    [0,20],
    [0,0]]

  l_ol2 = [
    [110,0],
    [120,0],
    [130,0, 130,10],
    [130,20],
    [130,30, 120,30],
    [110,30],
    [100,30, 100,20],
    [100,10],
    [100,0, 110,0]]

  l_ol3 = [
    [210,0],
    [220,0],
    [230,0, 230,10],
    [230,20],
    [230,30, 220,30],
    [210,30],
    [200,30, 200,20],
    [200,10]]
    #[200,0, 210,0]]

  # check CC (clock wise)
  l_ol4 = [
    [300,10],
    [300, 20],
    [300,30, 310,30],
    [320,30],
    [330,30, 330,20],
    [330,10],
    [330,0, 320,0],
    [310,0]]

  l_ol5 = [
    [0,100],
    [100,150],
    [110,155, 120, 150],
    [150,110],
    [160,100, 170, 105],
    [200,200],
    [0,200],
    [0,100]]

  l_ols = [l_ol1, l_ol2, l_ol3, l_ol4, l_ol5]
  #l_ols = [l_ol2]
  # circle
  l_circle_center = [200,200]
  l_circle_radius = 150
  # backend freecad
  print("dbg701: test1 backend freecad")
  for i_ol in l_ols:
    r_ol = outline_arc_line(i_ol, 'freecad')
    #Part.show(r_ol)
    l_test_face = Part.Face(Part.Wire(r_ol.Edges))
    r_test_solid = l_test_face.extrude(Base.Vector(0,0,1)) # straight linear extrusion
    Part.show(r_test_solid)
  r_ol = outline_circle(l_circle_center, l_circle_radius, 'freecad')
  l_test_face = Part.Face(Part.Wire(r_ol.Edges))
  r_test_solid = l_test_face.extrude(Base.Vector(0,0,1)) # straight linear extrusion
  Part.show(r_test_solid)
  # create the output directory
  l_output_dir = "test_output"
  print("Create the output directory: {:s}".format(l_output_dir))
  design_help.mkdir_p(l_output_dir)
  # backend svgwrite
  print("dbg702: test1 backend svgwrite")
  output_svg_file_name =  "{:s}/outline_arc_line_test1_00.svg".format(l_output_dir)
  object_svg = svgwrite.Drawing(filename = output_svg_file_name)
  #output_file_idx = 0
  for i_ol in l_ols:
    #output_file_idx += 1
    #output_svg_file_name =  "outline_arc_line_test1_{:02d}.svg".format(output_file_idx)
    #object_svg = svgwrite.Drawing(filename = output_svg_file_name)
    svg_outline = outline_arc_line(i_ol, 'svgwrite')
    for one_line_or_arc in svg_outline:
      object_svg.add(one_line_or_arc)
    #object_svg.save()
  one_circle = outline_circle(l_circle_center, l_circle_radius, 'svgwrite')
  object_svg.add(one_circle[0])
  object_svg.save()
  # backend dxfwrite
  print("dbg703: test1 backend dxfwrite")
  output_dxf_file_name =  "{:s}/outline_arc_line_test1_00.dxf".format(l_output_dir)
  object_dxf = DXFEngine.drawing(output_dxf_file_name)
  #object_dxf.add_layer(default_dxf_layer_name)
  for i_ol in l_ols:
    dxf_outline = outline_arc_line(i_ol, 'dxfwrite')
    for one_line_or_arc in dxf_outline:
      object_dxf.add(one_line_or_arc)
  one_circle = outline_circle(l_circle_center, l_circle_radius, 'dxfwrite')
  object_dxf.add(one_circle[0])
  object_dxf.save()
  # backend tkinter
  print("dbg704: test1 backend tkinter")
  tk_root = Tkinter.Tk()
  #my_canvas = display_backend.Two_Canvas(tk_root)
  my_canvas = Two_Canvas(tk_root)
  # callback function for display_backend
  def sub_canvas_graphics(ai_rotation_direction, ai_angle_position):
    # angle position
    l_angle_position = float(ai_angle_position)/100
    #
    r_canvas_graphics = []
    for i_ol in l_ols:
      r_canvas_graphics.append(('graphic_lines', outline_arc_line(i_ol, 'tkinter'), 'red', 2))
    r_canvas_graphics.append(('graphic_lines', outline_circle(l_circle_center, l_circle_radius, 'tkinter'), 'blue', 2))
    return(r_canvas_graphics)
  # end of callback function
  my_canvas.add_canvas_graphic_function(sub_canvas_graphics)
  tk_root.mainloop()
  del (my_canvas, tk_root) # because Tkinter will be used again later in this script
  #time.sleep(0.3)
  ### test the figure-level functions
  wfl_outer_rectangle_B = [
    [-60, -40],
    [ 60, -40],
    [ 60,  40],
    [-60,  40],
    [-60, -40]]
  
  wfl_inner_square_B = [
    [-10, -10],
    [ 10, -10],
    [ 10,  10],
    [-10,  10],
    [-10, -10]]
  
  wfl_inner_circle1 = [30,0, 15]
  wfl_inner_circle2 = [40,0, 10]
  
  wfl_figure = [wfl_outer_rectangle_B, wfl_inner_square_B, wfl_inner_circle1, wfl_inner_circle2]
  
  # display the figure
  figure_simple_display(wfl_figure)
  
  wfl_extrude_height = 20.0
  # create a FreeCAD part
  wfl_part = figure_to_freecad_25d_part(wfl_figure, wfl_extrude_height)
  
  # output file with mozman
  print("Generate {:s}/obt1_with_mozman.svg".format(l_output_dir))
  write_figure_in_svg(wfl_figure, "{:s}/obt1_with_mozman.svg".format(l_output_dir))
  print("Generate {:s}/obt1_with_mozman.dxf".format(l_output_dir))
  write_figure_in_dxf(wfl_figure, "{:s}/obt1_with_mozman.dxf".format(l_output_dir))

  # wfl_part in 3D BRep
  print("Generate {:s}/obt1_part.brep".format(l_output_dir))
  wfl_part.exportBrep("{:s}/obt1_part.brep".format(l_output_dir))
  # wfl_part in 2D DXF
  print("Generate {:s}/obt1_part.dxf".format(l_output_dir))
  export_2d.export_to_dxf(wfl_part, Base.Vector(0,0,1), wfl_extrude_height/2, "{:s}/obt1_part.dxf".format(l_output_dir)) # slice wfl_part in the XY plan at a height of wfl_extrude_height/2
  #
  r_test = 1
  return(r_test)