Exemplo n.º 1
0
Arquivo: parser.go Projeto: spyle/dxf
// ParseLine parses LINE entities.
func ParseLine(d *drawing.Drawing, data [][2]string) (entity.Entity, error) {
	l := entity.NewLine()
	var err error
	for _, dt := range data {
		switch dt[0] {
		default:
			continue
		case "8":
			layer, err := d.Layer(dt[1], false)
			if err == nil {
				l.SetLayer(layer)
			}
		case "10":
			err = setFloat(dt, func(val float64) { l.Start[0] = val })
		case "20":
			err = setFloat(dt, func(val float64) { l.Start[1] = val })
		case "30":
			err = setFloat(dt, func(val float64) { l.Start[2] = val })
		case "11":
			err = setFloat(dt, func(val float64) { l.End[0] = val })
		case "21":
			err = setFloat(dt, func(val float64) { l.End[1] = val })
		case "31":
			err = setFloat(dt, func(val float64) { l.End[2] = val })
		}
		if err != nil {
			return l, err
		}
	}
	return l, nil
}
Exemplo n.º 2
0
Arquivo: parser.go Projeto: spyle/dxf
// ParseBlock parses each BLOCK, which starts with "0\nBLOCK\n" and ends with "0\nENDBLK\n".
func ParseBlock(d *drawing.Drawing, data [][2]string) error {
	b := block.NewBlock("", "")
	var err error
	for _, dt := range data {
		switch dt[0] {
		case "2":
			b.Name = dt[1]
		case "1": // 4?
			b.Description = dt[1]
		case "8":
			layer, err := d.Layer(dt[1], false)
			if err == nil {
				b.SetLayer(layer)
			}
		case "10":
			err = setFloat(dt, func(val float64) { b.Coord[0] = val })
		case "20":
			err = setFloat(dt, func(val float64) { b.Coord[1] = val })
		case "30":
			err = setFloat(dt, func(val float64) { b.Coord[2] = val })
		case "70":
			val, err := strconv.ParseInt(strings.TrimSpace(dt[1]), 10, 64)
			if err != nil {
				return err
			}
			b.Flag = int(val)
		}
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 3
0
Arquivo: parser.go Projeto: spyle/dxf
// ParseEntities parses ENTITIES section.
func ParseEntities(d *drawing.Drawing, line int, data [][2]string) error {
	tmpdata := make([][2]string, 0)
	for i, dt := range data {
		if dt[0] == "0" {
			if len(tmpdata) > 0 {
				e, err := ParseEntity(d, tmpdata)
				if err != nil {
					return fmt.Errorf("line %d: %s", line+2*i, err.Error())
				}
				d.AddEntity(e)
				tmpdata = make([][2]string, 0)
			}
		}
		tmpdata = append(tmpdata, dt)
	}
	if len(tmpdata) > 0 {
		e, err := ParseEntity(d, tmpdata)
		if err != nil {
			return fmt.Errorf("line %d: %s", line+2*len(data), err.Error())
		}
		d.AddEntity(e)
		tmpdata = make([][2]string, 0)
	}
	return nil
}
Exemplo n.º 4
0
Arquivo: parser.go Projeto: spyle/dxf
// ParseLayer parses LAYER tables.
func ParseLayer(d *drawing.Drawing, data [][2]string) (table.SymbolTable, error) {
	var name string
	var flag int
	var col color.ColorNumber
	var lt *table.LineType
	var lw int
	for _, dt := range data {
		switch dt[0] {
		case "2":
			name = dt[1]
		case "70":
			val, err := strconv.ParseInt(strings.TrimSpace(dt[1]), 10, 64)
			if err != nil {
				return nil, err
			}
			flag = int(val)
		case "62":
			val, err := strconv.ParseInt(strings.TrimSpace(dt[1]), 10, 64)
			if err != nil {
				return nil, err
			}
			col = color.ColorNumber(val)
		case "6":
			l, err := d.LineType(dt[1])
			if err != nil {
				return nil, err
			}
			lt = l
		case "370":
			val, err := strconv.ParseInt(strings.TrimSpace(dt[1]), 10, 64)
			if err != nil {
				return nil, err
			}
			lw = int(val)
		case "390":
			// plotstyle
		}
	}
	l := table.NewLayer(name, col, lt)
	l.SetFlag(flag)
	l.SetLineWidth(lw)
	l.SetPlotStyle(d.PlotStyle)
	return l, nil
}