Skip to content

Commit

Permalink
add comments for godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
yofu committed Jan 7, 2016
1 parent 62c1f18 commit e5dcad0
Show file tree
Hide file tree
Showing 43 changed files with 407 additions and 3 deletions.
10 changes: 10 additions & 0 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/yofu/dxf/table"
)

// Block represents each BLOCK.
type Block struct {
Name string
Description string
Expand All @@ -15,6 +16,7 @@ type Block struct {
Coord []float64
}

// NewBlock create a new Block.
func NewBlock(name, desc string) *Block {
b := &Block{
Name: name,
Expand All @@ -27,6 +29,8 @@ func NewBlock(name, desc string) *Block {
return b
}


// Format writes data to formatter.
func (b *Block) Format(f *format.Formatter) {
f.WriteString(0, "BLOCK")
f.WriteHex(5, b.handle)
Expand All @@ -47,30 +51,36 @@ func (b *Block) Format(f *format.Formatter) {
f.WriteString(100, "AcDbBlockEnd")
}

// String outputs data using default formatter.
func (b *Block) String() string {
f := format.New()
return b.FormatString(f)
}

// FormatString outputs data using given formatter.
func (b *Block) FormatString(f *format.Formatter) string {
b.Format(f)
return f.Output()
}

// Handle returns a handle value of BLOCK.
func (b *Block) Handle() int {
return b.handle
}
// SetHandle sets handles to BLOCK and ENDBLK.
func (b *Block) SetHandle(v *int) {
b.handle = *v
(*v)++
b.endhandle = *v
(*v)++
}

// Layer returns BLOCK's Layer.
func (b *Block) Layer() *table.Layer {
return b.layer
}

// SetLayer sets Layer to BLOCK.
func (b *Block) SetLayer(layer *table.Layer) {
b.layer = layer
}
6 changes: 6 additions & 0 deletions block/blocks.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// BLOCK section
package block

import (
"github.com/yofu/dxf/format"
)

// Blocks represents BLOCKS section.
type Blocks []*Block

// New creates a new Blocks.
func New() Blocks {
b := make([]*Block, 3)
b[0] = NewBlock("*Model_Space", "")
Expand All @@ -14,6 +17,7 @@ func New() Blocks {
return b
}

// WriteTo writes BLOCKS data to formatter.
func (bs Blocks) WriteTo(f *format.Formatter) {
f.WriteString(0, "SECTION")
f.WriteString(2, "BLOCKS")
Expand All @@ -23,11 +27,13 @@ func (bs Blocks) WriteTo(f *format.Formatter) {
f.WriteString(0, "ENDSEC")
}

// Add adds a new block to BLOCKS section.
func (bs Blocks) Add(b *Block) Blocks {
bs = append(bs, b)
return bs
}

// SetHandle sets handles to each block.
func (bs Blocks) SetHandle(v *int) {
for _, b := range bs {
b.SetHandle(v)
Expand Down
9 changes: 9 additions & 0 deletions class/classes.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
// CLASSES section
package class

import (
"github.com/yofu/dxf/format"
)

// Class represents each CLASS.
type Class struct {
}

// Format writes data to formatter.
func (c *Class) Format(f *format.Formatter) {
f.WriteString(0, "CLASS")
}

// String outputs data using default formatter.
func (c *Class) String() string {
f := format.New()
return c.FormatString(f)
}

// FormatString outputs data using given formatter.
func (c *Class) FormatString(f *format.Formatter) string {
c.Format(f)
return f.Output()
}

// Classes represents CLASSES section.
type Classes []*Class

// New creates a new Classes.
func New() Classes {
c := make([]*Class, 0)
return c
}

// WriteTo writes CLASSES data to formatter.
func (cs Classes) WriteTo(f *format.Formatter) {
f.WriteString(0, "SECTION")
f.WriteString(2, "CLASSES")
Expand All @@ -37,6 +45,7 @@ func (cs Classes) WriteTo(f *format.Formatter) {
f.WriteString(0, "ENDSEC")
}

// SetHandle sets handles to each class.
func (cs Classes) SetHandle(v *int) {
return
}
7 changes: 5 additions & 2 deletions color/colornumber.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Color Number
package color

// AutoCAD color index number (code 62)
type ColorNumber uint8

// 1 - 9
// color number: 1 - 9
const (
Red ColorNumber = iota + 1
Yellow
Expand All @@ -15,7 +17,7 @@ const (
Grey192
)

// 250 - 255
// color number: 250 - 255
const (
Grey51 = iota + 250
Grey91
Expand All @@ -25,6 +27,7 @@ const (
Grey255
)

// color number to RGB equivalents
var (
ColorRGB = [][]uint8{
[]uint8{0, 0, 0}, // 0
Expand Down
26 changes: 26 additions & 0 deletions drawing/drawing.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package drawing defines Drawing struct for DXF.
package drawing

import (
Expand All @@ -15,6 +16,7 @@ import (
"os"
)

// Drawing contains DXF drawing data.
type Drawing struct {
FileName string
Layers map[string]*table.Layer
Expand All @@ -29,6 +31,7 @@ type Drawing struct {
PlotStyle handle.Handler
}

// New creates a new Drawing.
func New() *Drawing {
d := new(Drawing)
d.Layers = make(map[string]*table.Layer)
Expand Down Expand Up @@ -78,18 +81,22 @@ func (d *Drawing) saveFile(filename string) error {
return nil
}

// Save saves the drawing file.
// If it is the first time, use SaveAs(filename).
func (d *Drawing) Save() error {
if d.FileName == "" {
return errors.New("filename is blank, use SaveAs(filename)")
}
return d.saveFile(d.FileName)
}

// SaveAs saves the drawing file as given filename.
func (d *Drawing) SaveAs(filename string) error {
d.FileName = filename
return d.saveFile(filename)
}

// setHandle sets all the handles contained in Drawing.
func (d *Drawing) setHandle() {
h := 1
for _, s := range d.Sections[1:] {
Expand All @@ -98,6 +105,8 @@ func (d *Drawing) setHandle() {
d.Sections[0].SetHandle(&h)
}

// Layer returns the named layer if exists.
// If setcurrent is true, set current layer to it.
func (d *Drawing) Layer(name string, setcurrent bool) (*table.Layer, error) {
if l, exist := d.Layers[name]; exist {
if setcurrent {
Expand All @@ -108,6 +117,8 @@ func (d *Drawing) Layer(name string, setcurrent bool) (*table.Layer, error) {
return nil, fmt.Errorf("layer %s doesn't exist", name)
}

// AddLayer adds a new layer with given name, color and line type.
// If setcurrent is true, set current layer to it.
func (d *Drawing) AddLayer(name string, cl color.ColorNumber, lt *table.LineType, setcurrent bool) (*table.Layer, error) {
if l, exist := d.Layers[name]; exist {
if setcurrent {
Expand All @@ -125,6 +136,7 @@ func (d *Drawing) AddLayer(name string, cl color.ColorNumber, lt *table.LineType
return l, nil
}

// ChangeLayer changes current layer to the named layer.
func (d *Drawing) ChangeLayer(name string) error {
if l, exist := d.Layers[name]; exist {
d.CurrentLayer = l
Expand All @@ -133,6 +145,7 @@ func (d *Drawing) ChangeLayer(name string) error {
return errors.New(fmt.Sprintf("layer %s doesn't exist", name))
}

// LineType returns the named line type if exists.
func (d *Drawing) LineType(name string) (*table.LineType, error) {
lt, err := d.Sections[TABLES].(table.Tables)[table.LTYPE].Contains(name)
if err != nil {
Expand All @@ -141,14 +154,17 @@ func (d *Drawing) LineType(name string) (*table.LineType, error) {
return lt.(*table.LineType), nil
}

// Entities returns slice of all entities contained in Drawing.
func (d *Drawing) Entities() (entity.Entities) {
return d.Sections[ENTITIES].(entity.Entities)
}

// AddEntity adds a new entity.
func (d *Drawing) AddEntity(e entity.Entity) {
d.Sections[4] = d.Sections[4].(entity.Entities).Add(e)
}

// Point creates a new POINT at (x, y, z).
func (d *Drawing) Point(x, y, z float64) (*entity.Point, error) {
p := entity.NewPoint()
p.Coord = []float64{x, y, z}
Expand All @@ -157,6 +173,7 @@ func (d *Drawing) Point(x, y, z float64) (*entity.Point, error) {
return p, nil
}

// Line creates a new LINE from (x1, y1, z1) to (x2, y2, z2).
func (d *Drawing) Line(x1, y1, z1, x2, y2, z2 float64) (*entity.Line, error) {
l := entity.NewLine()
l.Start = []float64{x1, y1, z1}
Expand All @@ -166,6 +183,7 @@ func (d *Drawing) Line(x1, y1, z1, x2, y2, z2 float64) (*entity.Line, error) {
return l, nil
}

// Circle creates a new CIRCLE at (x, y, z) with radius r.
func (d *Drawing) Circle(x, y, z, r float64) (*entity.Circle, error) {
c := entity.NewCircle()
c.Center = []float64{x, y, z}
Expand All @@ -175,6 +193,7 @@ func (d *Drawing) Circle(x, y, z, r float64) (*entity.Circle, error) {
return c, nil
}

// Polyline creates a new POLYLINE with given vertices.
func (d *Drawing) Polyline(closed bool, vertices ...[]float64) (*entity.Polyline, error) {
p := entity.NewPolyline()
p.SetLayer(d.CurrentLayer)
Expand All @@ -188,6 +207,7 @@ func (d *Drawing) Polyline(closed bool, vertices ...[]float64) (*entity.Polyline
return p, nil
}

// LwPolyline creates a new LWPOLYLINE with given vertices.
func (d *Drawing) LwPolyline(closed bool, vertices ...[]float64) (*entity.LwPolyline, error) {
size := len(vertices)
l := entity.NewLwPolyline(size)
Expand All @@ -202,6 +222,7 @@ func (d *Drawing) LwPolyline(closed bool, vertices ...[]float64) (*entity.LwPoly
return l, nil
}

// ThreeDFace creates a new 3DFACE with given points.
func (d *Drawing) ThreeDFace(points [][]float64) (*entity.ThreeDFace, error) {
f := entity.New3DFace()
if len(points) < 3 {
Expand All @@ -220,6 +241,7 @@ func (d *Drawing) ThreeDFace(points [][]float64) (*entity.ThreeDFace, error) {
return f, nil
}

// Text creates a new TEXT str at (x, y, z) with given height.
func (d *Drawing) Text(str string, x, y, z, height float64) (*entity.Text, error) {
t := entity.NewText()
t.Coord1 = []float64{x, y, z}
Expand All @@ -235,6 +257,8 @@ func (d *Drawing) addObject(o object.Object) {
d.Sections[5] = d.Sections[5].(object.Objects).Add(o)
}

// Group adds given entities to the named group.
// If the named group doesn't exist, create it.
func (d *Drawing) Group(name, desc string, es ...entity.Entity) (*object.Group, error) {
if g, exist := d.Groups[name]; exist {
g.AddEntity(es...)
Expand All @@ -247,6 +271,8 @@ func (d *Drawing) Group(name, desc string, es ...entity.Entity) (*object.Group,
return g, nil
}

// AddGroup adds given entities to the named group.
// If the named group doesn't exist, returns error.
func (d *Drawing) AddToGroup(name string, es ...entity.Entity) error {
if g, exist := d.Groups[name]; exist {
g.AddEntity(es...)
Expand Down
7 changes: 7 additions & 0 deletions drawing/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"github.com/yofu/dxf/format"
)

// Section is interface for DXF sections.
type Section interface {
WriteTo(*format.Formatter)
SetHandle(*int)
}

// SectionType represents Section names (code 2)
type SectionType int

// Section name: code 2
const (
HEADER SectionType = iota
CLASSES
Expand All @@ -20,6 +23,8 @@ const (
OBJECTS
)

// SectionTypeString converts SectionType to string.
// If SectionType is out of range, it returns empty string.
func SectionTypeString(s SectionType) string {
switch s {
case HEADER:
Expand All @@ -39,6 +44,8 @@ func SectionTypeString(s SectionType) string {
}
}

// SectionTypeValue converts string to SectionType.
// If string is unknown SectionType, it returns -1.
func SectionTypeValue(s string) SectionType {
switch s {
case "HEADER":
Expand Down
Loading

0 comments on commit e5dcad0

Please sign in to comment.