Question

How can I write text entities to a dxf file?

I found a script that can export Sketchup drawings to dxf files, unfortunately it was ignoring layers and text. I fixed it so it outputs the proper layer, but I cannot figure out how to get it to output the text.

I have it to the point where it pops up a message when it comes across a text entity in the entities collection, but I'm not sure how to get it to write it to the file properly.

Was it helpful?

Solution 2

This is how I did it.

def dxf_text_output_test
    $dxf_file = File.new("C:\testfile.dxf" , "w" ) 
    model = Sketchup.active_model
    entities = model.entities
    entities.each do |entity|
        if(entity.typename="Text")
            dxf_output_text(entity)
        end
    end
end

def dxf_ouput_text(text)
    points = text.point
    $dxf_file.puts( "  0\nTEXT\n1\n192\n330\n1F\n100\nAcDbEntity\n8\n"+text.layer.name+"\n")
    $dxf_file.puts("100\nAcDbText\n")
    if(points == nil)   
        $dxf_file.puts("10\n0.0\n")#x
        $dxf_file.puts("20\n0.0\n")#y
        $dxf_file.puts("30\n0.0\n")#z
    else
        $dxf_file.puts("10\n"+(points.x.to_f * $dxf_conv).to_s+"\n")#x
        $dxf_file.puts("20\n"+(points.y.to_f * $dxf_conv).to_s+"\n")#y
        $dxf_file.puts("30\n"+(points.z.to_f * $dxf_conv).to_s+"\n")#z
    end
    $dxf_file.puts("40\n"+(1 * $dxf_conv).to_s+"\n")#text height
    $dxf_file.puts("39\n"+text.line_weight.to_s+"\n")#thickness
    $dxf_file.puts("1\n"+text.text+"\n")#text
end 

OTHER TIPS

I had to generate a DXF from my rails application, I couldn't find a gem, so I generated python code, using the lybrary DXFWriter, and called python sending the code to generate the DXF file, maybe it could be an useful idea to someone.

  def dxf
    elements = ''
    params[:_json].second.each do |l|  #Lines
      elements += "drw.add(dxf.line((#{l[:x1]}, #{l[:y1]}), (#{l[:x2]}, #{l[:y2]}), color=1));"
    end
    params[:_json].third.each do |a|  #Arcs
      elements += "drw.add(dxf.arc(#{a[:radius]}, (#{a[:left]}, #{a[:top]}),
                  #{(a[:startAngle] * Base::Part::TO_DEG).round(3)}, #{(a[:endAngle] * Base::Part::TO_DEG).round(3)}, color=1));"
    end
    file_name = "#{Rails.root}/tmp/#{Dir::Tmpname.make_tmpname([params[:_json].first[:part_name], '.dxf'], nil)}"
    base_script = "import sys;sys.path.insert(0, '" + Rails.root.to_s + "');from dxfwrite import DXFEngine as dxf;"\
                  "drw = dxf.drawing('#{file_name}');"\
                  "#{elements}drw.save()"

    %x(python -c "#{base_script}")
    render json: { export_basename: file_name }
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top