Skip to content

Loriowar/dxf_io

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DxfIO

This is a gem for read and write DXF files and for processing of simple actions over dxf entities. Reader part of the gem based on ruby-dxf-reader. Gem supported DXF files generated by AutoCAD 2008 (dxf-specification).

Gem Version

Installation

Add this line to your application's Gemfile:

gem 'dxf_io'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dxf_io

Usage

Gem consists from Reader, Writer, Wrapper and support classes.

Reader

Reader require a path to DXF file and return a hash with dxf-objects.

Reader can be initialized as follows:

reader_instance = DxfIO::Reader.new(path: '/path/to/your/file.dxf')

Additionally you can use open method:

DxfIO::Reader.open('/path/to/your/file.dxf') do |dxf_reader_instance|
  # ...
end

open receive same options as new.

Available options of Reader:

  • path - path to DXF-file (required)
  • encoding - encoding of input file ('Windows-1251' by default)

As alternative you may pass single string as input parameter:

reader_instance = DxfIO::Reader.new('/path/to/your/file.dxf')

To obtain full hash with DXF content eval

dxf_content = reader_instance.run

or

dxf_content = reader_instance.to_h

Format of resulted hash:

{
             "HEADER" => {...}, 
            "CLASSES" => [...],
             "TABLES" => [...],
             "BLOCKS" => [...],
           "ENTITIES" => [...],
            "OBJECTS" => [...],
    "THUMBNAILIMAGES" => [...]
}

If you need certain part of DXF you may execute one of the following functions

  • header
  • classes
  • tables
  • blocks
  • entities
  • objects
  • thumbnailimages

Format of header section is

{
    $PLIMCHECK" => {
        70 => 0
    },
    "$PEXTMIN" => {
        10 => 1.0e+20,
        20 => 1.0e+20,
        30 => 1.0e+20
    },
    ...
}

Other section has different format:

[
    [
        {   0 => "CLASS" },
        {   1 => "VISUALSTYLE" },
        {   2 => "AcDbVisualStyle" },
        {   3 => "ObjectDBX Classes" },
        {  90 => 4095 },
        { 280 => 0 },
        { 281 => 0 }
    ],
    [...]
]

Sequence of groups and values is same as in DXF file.

Notes

  • Reader caches result of parse DXF file. If you want to update a cache call reader_instance.rerun.
  • Reader automatically cast integer and float values to proper Ruby classes.

Writer

Writer receive a dxf-hash in format from Reader and construct a DXF-file.

Writer can be initialized as follow

writer_instance = DxfIO::Writer.new(dxf_hash: dxf_content, path: '/path/to/new/file.dxf')

To process write execute writer_instance.run.

Alternative way to use Writer:

writer_instance = DxfIO::Writer.open('/path/to/new/file.dxf') do |dxf_writer_instance|
  # ...
  dxf_writer_instance.write_hash dxf_hash
end

open receive same options as new.

Available options of Writer:

  • dxf_hash - hash with DXF-file in format of Reader (required in new or in run/write_hash methods)
  • path - path to new DXF-file (required)
  • encoding - encoding of output file ('Windows-1251' by default)
  • delimiter - delimiter in DXF-file ("\r\n" by default)
  • strategy - strategy of Writer (:memory by default)

Write strategy

Writter support two strategy:

  1. :disk - a lot of small write operation for each group, section etc
  2. :memory - prepare full file content in memory and write by a single operation (default behaviour)

Wrapper

Wrapper provide several tools for work with dxf-entities.

Wrapper can be initialized as follow

wrapper_instance = DxfIO::Wrapper.new(dxf_hash: dxf_content)

It has functions for fetch any groups except header:

  • classes
  • tables
  • blocks
  • entities
  • objects
  • thumbnailimages

Any of these functions return array with Entity which is DxfIO::Entity::Other.

Each Entity has follows methods

  • to_a - same as in Reader format
  • to_h - hash with group code on keys and array with group values on values
  • type - value of zero group (for example "LINE")
  • points - array of Points which is DxfIO::Entity::Support::Point
  • xs - array of X-coordinates
  • ys - array of Y-coordinates
  • move_to! - receive Point and move all points of current Entity

Support

Point

This is a simple class for storage 2D points. It provide follows methods:

  • x - access to X-coordinate
  • y - access to Y-coordinate
  • start? - is this start-point of primitive (codes 10 and 20 from dxf-specification)
  • end? - is this end-point of primitive (codes 11 and 21 from dxf-specification)
  • ==
  • binary +, -, *, /
  • unary -
  • rotate_90, rotate_180 (supposed what point is a vector from zero)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

  1. Fork it ( https://github.com/Loriowar/dxf_io/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Releases

No releases published

Packages

No packages published