Skip to content
This repository has been archived by the owner on Jul 24, 2022. It is now read-only.

Commit

Permalink
fix(svg deserializer): fixed svg-deserializer to work with Inkscape f…
Browse files Browse the repository at this point in the history
…iles (#72)

* fixed svg-deserializer to work with inkscape file
* Add support for holes via compound paths. Incidentally fixes arc, etc.
* chore(pkg): updated csg dependency version for all io packages where applicable
* test(svg-deserializer): minor test tweaks
  • Loading branch information
kaosat-dev committed Sep 2, 2018
1 parent 399e965 commit f35ea5e
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/amf-serializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@
"xmldom": "^0.1.27"
},
"devDependencies": {
"@jscad/csg": "0.3.6"
"@jscad/csg": "0.7.0"
}
}
2 changes: 1 addition & 1 deletion packages/io-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
],
"license": "MIT",
"devDependencies": {
"@jscad/csg": "0.3.6"
"@jscad/csg": "0.7.0"
}
}
2 changes: 1 addition & 1 deletion packages/json-deserializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
],
"license": "MIT",
"dependencies": {
"@jscad/csg": "0.3.6"
"@jscad/csg": "0.7.0"
}
}
2 changes: 1 addition & 1 deletion packages/json-serializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"license": "MIT",
"dependencies": {
"@jscad/csg": "0.3.6",
"@jscad/csg": "0.7.0",
"@jscad/io-utils": "^0.1.2"
}
}
2 changes: 1 addition & 1 deletion packages/stl-deserializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"license": "MIT",
"dependencies": {
"@jscad/csg": "0.3.6",
"@jscad/csg": "0.7.0",
"@jscad/io-utils": "^0.1.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/stl-serializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"license": "MIT",
"devDependencies": {
"@jscad/csg": "0.3.6"
"@jscad/csg": "0.7.0"
},
"dependencies": {
"@jscad/io-utils": "^0.1.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/svg-deserializer/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const cssStyle = function (element, name) {
if (v !== null) {
v = v[0]
var i = v.length
while (v[i] !== ' ') i--
while (v[i] !== ' ' && i > 0) i--
v = v.slice(i + 1, v.length - 1)
return v
}
Expand Down
20 changes: 10 additions & 10 deletions packages/svg-deserializer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ const objectify = function (group) {
if ('transforms' in obj) {
// NOTE: SVG specifications require that transforms are applied in the order given.
// But these are applied in the order as required by CSG/CAG
let tr
let ts
let tt
let tr = null
let ts = null
let tt = null

for (let j = 0; j < obj.transforms.length; j++) {
const t = obj.transforms[j]
Expand Down Expand Up @@ -233,27 +233,27 @@ const codify = function (group) {
if ('transforms' in obj) {
// NOTE: SVG specifications require that transforms are applied in the order given.
// But these are applied in the order as required by CSG/CAG
let tr
let ts
let tt
let tr = null;
let ts = null;
let tt = null;

for (let j = 0; j < obj.transforms.length; j++) {
var t = obj.transforms[j]
if ('rotate' in t) { tr = t }
if ('scale' in t) { ts = t }
if ('translate' in t) { tt = t }
}
if (ts !== null && ts !== undefined) {
if (ts !== null) {
const x = ts.scale[0]
const y = ts.scale[1]
code += indent + on + ' = ' + on + '.scale([' + x + ',' + y + ']);\n'
}
if (tr !== null && tr !== undefined) {
if (tr !== null) {
console.log('tr', tr)
const z = 0 - tr.rotate
code += indent + on + ' = ' + on + '.rotateZ(' + z + ');\n'
}
if (tt !== null && tt !== undefined) {
if (tt !== null) {
const x = cagLengthX(tt.translate[0], svgUnitsPmm, svgUnitsX)
const y = (0 - cagLengthY(tt.translate[1], svgUnitsPmm, svgUnitsY))
code += indent + on + ' = ' + on + '.translate([' + x + ',' + y + ']);\n'
Expand Down Expand Up @@ -294,7 +294,7 @@ function createSvgParser (src, pxPmm) {
POLYGON: svgPolygon,
PATH: svgPath,
USE: svgUse,
DEFS: () => { svgInDefs = true },
DEFS: () => { svgInDefs = true; return null; },
DESC: () => undefined, // ignored by design
TITLE: () => undefined, // ignored by design
STYLE: () => undefined, // ignored by design
Expand Down
2 changes: 1 addition & 1 deletion packages/svg-deserializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"license": "MIT",
"dependencies": {
"@jscad/csg": "0.3.6",
"@jscad/csg": "0.7.0",
"sax": "1.2.4"
},
"devDependencies": {
Expand Down
23 changes: 16 additions & 7 deletions packages/svg-deserializer/shapesMapCsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups) {
}
// close the previous path
if (pi > 0 && pc === false) {
paths[pathName].expandToCAG(CSG.defaultResolution2D)
paths[pathName] = paths[pathName].expandToCAG(CSG.defaultResolution2D)
// code += indent + pathName + ' = ' + pathName + '.expandToCAG(' + r + ',CSG.defaultResolution2D);\n'
}
// open a new path
Expand All @@ -168,7 +168,7 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups) {
while (pts.length >= 2) {
cx = cx + parseFloat(pts.shift())
cy = cy + parseFloat(pts.shift())
paths[pathName].appendPoint([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)])
paths[pathName] = paths[pathName].appendPoint([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)])
}
break
case 'M': // absolute move to X,Y
Expand All @@ -190,7 +190,7 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups) {
while (pts.length >= 2) {
cx = parseFloat(pts.shift())
cy = parseFloat(pts.shift())
paths[pathName].appendPoint([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)])
paths[pathName] = paths[pathName].appendPoint([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)])
}
break
case 'a': // relative elliptical arc
Expand All @@ -202,7 +202,7 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups) {
let sf = (pts.shift() === '1')
cx = cx + parseFloat(pts.shift())
cy = cy + parseFloat(pts.shift())
paths[pathName].appendArc([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)], {xradius: svg2cagX(rx, svgUnitsPmm), yradius: svg2cagY(ry, svgUnitsPmm), xaxisrotation: ro, clockwise: sf, large: lf})
paths[pathName] = paths[pathName].appendArc([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)], {xradius: svg2cagX(rx, svgUnitsPmm), yradius: svg2cagY(ry, svgUnitsPmm), xaxisrotation: ro, clockwise: sf, large: lf})
}
break
case 'A': // absolute elliptical arc
Expand All @@ -214,7 +214,7 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups) {
let sf = (pts.shift() === '1')
cx = parseFloat(pts.shift())
cy = parseFloat(pts.shift())
paths[pathName].appendArc([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)], {xradius: svg2cagX(rx, svgUnitsPmm), yradius: svg2cagY(ry, svgUnitsPmm), xaxisrotation: ro, clockwise: sf, large: lf})
paths[pathName] = paths[pathName].appendArc([svg2cagX(cx, svgUnitsPmm), svg2cagY(cy, svgUnitsPmm)], {xradius: svg2cagX(rx, svgUnitsPmm), yradius: svg2cagY(ry, svgUnitsPmm), xaxisrotation: ro, clockwise: sf, large: lf})
}
break
case 'c': // relative cubic Bézier
Expand Down Expand Up @@ -357,8 +357,17 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, svgGroups) {
break
case 'z': // close current line
case 'Z':
paths[pathName] = paths[pathName].close().innerToCAG()
pathCag = pathCag.union(paths[pathName])
let closedpath = paths[pathName].close();
paths[pathName] = closedpath.innerToCAG();
switch (closedpath.getTurn()) {
default:
case 'clockwise':
pathCag = pathCag.union(paths[pathName])
break;
case 'counter-clockwise':
pathCag = pathCag.subtract(paths[pathName])
break;
}
cx = sx
cy = sy // return to the starting point
pc = true
Expand Down
13 changes: 11 additions & 2 deletions packages/svg-deserializer/shapesMapJscad.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,17 @@ function path (obj, svgUnitsPmm, svgUnitsX, svgUnitsY, svgUnitsV, params, svgGro
case 'z': // close current line
case 'Z':
tmpCode += indent + pathName + ' = ' + pathName + '.close();\n'
tmpCode += indent + pathName + ' = ' + pathName + '.innerToCAG();\n'
tmpCode += indent + on + ' = ' + on + '.union(' + pathName + ');\n'
tmpCode += indent + 'switch (' + pathName + '.getTurn()) {\n';
tmpCode += indent + ' default:\n';
tmpCode += indent + ' case "clockwise":\n';
tmpCode += indent + ' ' + pathName + ' = ' + pathName + '.innerToCAG();\n'
tmpCode += indent + ' ' + on + ' = ' + on + '.union(' + pathName + ');\n'
tmpCode += indent + ' break;\n';
tmpCode += indent + ' case "counter-clockwise":\n';
tmpCode += indent + ' ' + pathName + ' = ' + pathName + '.innerToCAG();\n'
tmpCode += indent + ' ' + on + ' = ' + on + '.subtract(' + pathName + ');\n'
tmpCode += indent + ' break;\n';
tmpCode += indent + '}\n';
cx = sx
cy = sy // return to the starting point
pc = true
Expand Down

0 comments on commit f35ea5e

Please sign in to comment.