Skip to content

Commit

Permalink
feat(iges): add PolylineMode enum, update addPolyline2d()
Browse files Browse the repository at this point in the history
- add addPolygon2d()
  • Loading branch information
postspectacular committed Jul 24, 2018
1 parent 76dc450 commit f7a084a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
8 changes: 7 additions & 1 deletion packages/iges/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ export enum StatusHierarchy {
USE_PROP
}

export enum PolylineMode {
OPEN,
CLOSED,
FILLED
}

// spec page 24 (53)
export interface DictEntry {
type: number;
Expand Down Expand Up @@ -176,4 +182,4 @@ export const DEFAULT_GLOBALS: Partial<GlobalParams> = {
precision: 3,
numLineWeights: 1,
maxLineWeight: 0.254,
};
};
25 changes: 18 additions & 7 deletions packages/iges/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GlobalParams,
IGESDocument,
Param,
PolylineMode,
Type,
Unit
} from "./api";
Expand Down Expand Up @@ -54,7 +55,7 @@ const $DATE = (d: Date) =>
$Z2(d.getUTCSeconds())
].join(""));

export const newDocument = (g?: Partial<GlobalParams>): IGESDocument => {
export const newDocument = (g?: Partial<GlobalParams>, start?: string[]): IGESDocument => {
const globals = <GlobalParams>{ ...DEFAULT_GLOBALS, ...g };
const $FF = ff(globals.precision);
const $PARAM = defmulti<any[], string>((x) => x[1]);
Expand All @@ -66,7 +67,7 @@ export const newDocument = (g?: Partial<GlobalParams>): IGESDocument => {

return {
globals,
start: ["Generated by @thi.ng/iges"],
start: start || [],
dict: [],
param: [],
offsets: {
Expand Down Expand Up @@ -213,22 +214,24 @@ const formatParams = (doc: IGESDocument, params: Param[], fmtBody: (body: string

// type table: page 38 (67)

// page 77 (106)
export const addPolyline2d = (doc: IGESDocument, pts: number[][], closed = false) => {
// sec 4.7, page 77 (106)
export const addPolyline2d = (doc: IGESDocument, pts: number[][], form: PolylineMode) => {
const did = doc.offsets.D;
const pid = doc.offsets.P;
const params = formatParams(
doc,
[
[106, Type.INT],
[1, Type.INT],
[pts.length + (closed ? 1 : 0), Type.INT],
[pts.length + (form === PolylineMode.CLOSED ? 1 : 0), Type.INT],
[0, Type.FLOAT],
...iterator(
mapcat<number[], Param>(
([x, y]) => [[x, Type.FLOAT], [y, Type.FLOAT]]
),
closed ? wrap(pts, 1, false, true) : pts
form === PolylineMode.CLOSED ?
wrap(pts, 1, false, true) :
pts
)
],
formatParam(did, pid)
Expand All @@ -237,7 +240,7 @@ export const addPolyline2d = (doc: IGESDocument, pts: number[][], closed = false
doc.offsets.D += 2;
doc.dict.push(...formatDictEntry(<DictEntry>{
type: 106,
form: 11,
form: form === PolylineMode.FILLED ? 63 : 11,
param: pid,
index: did,
lineCount: params.length,
Expand All @@ -246,4 +249,12 @@ export const addPolyline2d = (doc: IGESDocument, pts: number[][], closed = false
return doc;
};

export const addPolygon2d = (doc: IGESDocument, pts: number[][]) =>
addPolyline2d(doc, pts, PolylineMode.FILLED);

// sec 4.23, page 123 (type 126)
// export const addNurbsCurve2d = (doc: IGESDocument, degree: number, pts: number[][], closed = false) => {
// doc; degree; pts; closed;
// };

export * from "./api";

0 comments on commit f7a084a

Please sign in to comment.