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).
Add this line to your application's Gemfile:
gem 'dxf_io'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dxf_io
Gem consists from Reader, Writer, Wrapper and support classes.
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.
- 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 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 inrun
/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)
Writter support two strategy:
:disk
- a lot of small write operation for each group, section etc:memory
- prepare full file content in memory and write by a single operation (default behaviour)
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
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)
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
.
- Fork it ( https://github.com/Loriowar/dxf_io/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request