How to use @jscad/io-utils - 10 common examples

To help you get started, we’ve selected a few @jscad/io-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jscad / OpenJSCAD.org / packages / io / svg-serializer / index.js View on Github external
const serialize = (...params) => {
  let options = {}
  let objects
  if (params.length === 0) {
    throw new Error('no arguments supplied to serialize function !')
  } else if (params.length === 1) {
    // assumed to be object(s)
    objects = Array.isArray(params[0]) ? params[0] : params
  } else if (params.length > 1) {
    options = params[0]
    objects = params[1]
  }
  // make sure we always deal with arrays of objects as inputs
  objects = toArray(objects)
  console.log('params', params)
  console.log('options', options)
  console.log('objects', objects)
  const defaults = {
    statusCallback: null,
    unit: 'mm', // em | ex | px | in | cm | mm | pt | pc
    decimals: 10000
  }
  options = Object.assign({}, defaults, options)

  options.statusCallback && options.statusCallback({progress: 0})

  // get the lower and upper bounds of ALL convertable objects
  var bounds = getBounds(objects)

  var width = 0
github jscad / io / packages / x3d-serializer / index.js View on Github external
function serialize (CSG, options) {
  options && options.statusCallback && options.statusCallback({progress: 0})
  CSG = ensureManifoldness(CSG)
  const DOMImplementation = typeof document !== 'undefined' ? document.implementation : new xmldom.DOMImplementation()
  // materialPolygonLists
  // key: a color string (e.g. "0 1 1" for yellow)
  // value: an array of strings specifying polygons of this color
  //        (as space-separated indices into vertexCoords)
  var materialPolygonLists = {}
  // list of coordinates (as "x y z" strings)
  var vertexCoords = []
  // map to look up the index in vertexCoords of a given vertex
  var vertexTagToCoordIndexMap = {}

  CSG.polygons.map(function (p, i) {
    var red = 0
    var green = 0
    var blue = 1 // default color is blue
    if (p.shared && p.shared.color) {
github jscad / OpenJSCAD.org / packages / io / dxf-serializer / index.js View on Github external
let entityContents = objects.map(function (object, i) {
    if (isCAG(object)) {
      let paths = object.getOutlinePaths()
      if (options.cagTo === 'polyline') {
        return PathsToPolyine(paths, options)
      }
      return PathsToLwpolyine(paths, options)
    }
    if (isCSG(object)) {
      object = ensureManifoldness(object)
      if (options.csgTo === 'polyline') {
        return PolygonsToPolyline(object, options)
      }
      return PolygonsTo3DFaces(object, options)
    }
    if (isPath(object)) {
    // mimic a CAG outline path
      let points = object.getPoints()
      let closed = object.isClosed()
      let path = {closed: closed, points: points}
      return PathsToLwpolyine([path], options)
    }
    return ''
  })
  let section = `  0
github jscad / io / packages / dxf-serializer / index.js View on Github external
let entityContents = objects.map(function (object, i) {
    if (isCAG(object)) {
      let paths = object.getOutlinePaths()
      if (options.cagTo === 'polyline') {
        return PathsToPolyine(paths, options)
      }
      return PathsToLwpolyine(paths, options)
    }
    if (isCSG(object)) {
      object = ensureManifoldness(object)
      if (options.csgTo === 'polyline') {
        return PolygonsToPolyline(object, options)
      }
      return PolygonsTo3DFaces(object, options)
    }
    if (isPath(object)) {
    // mimic a CAG outline path
      let points = object.getPoints()
      let closed = object.isClosed()
      let path = {closed: closed, points: points}
      return PathsToLwpolyine([path], options)
    }
    return ''
  })
  let section = `  0
github jscad / io / packages / amf-serializer / index.js View on Github external
function serialize (CSG, m, options) {
  options && options.statusCallback && options.statusCallback({progress: 0})
  CSG = ensureManifoldness(CSG)
  var result = '\n\n'
  for (var k in m) {
    result += '\n'
  }
  result += '\n\n\n'

  CSG.polygons.map(function (p) { // first we dump all vertices of all polygons
    for (var i = 0; i < p.vertices.length; i++) {
      result += CSGVertextoAMFString(p.vertices[i])
    }
  })
  result += '\n'

  var n = 0
  CSG.polygons.map(function (p, i) { // then we dump all polygons
    result += '\n'
github jscad / OpenJSCAD.org / packages / io / x3d-serializer / index.js View on Github external
const serialize = (...params) => {
  let options = {}
  let objects
  if (params.length === 0) {
    throw new Error('no arguments supplied to serialize function !')
  } else if (params.length === 1) {
    // assumed to be object(s)
    objects = Array.isArray(params[0]) ? params[0] : params
  } else if (params.length > 1) {
    options = params[0]
    objects = params[1]
  }
  // make sure we always deal with arrays of objects as inputs
  objects = toArray(objects)

  const defaults = {
    statusCallback: null,
    unit: 'millimeter', // millimeter, inch, feet, meter or micrometer
    color: [0, 0, 1, 1.0], // default colorRGBA specification
    decimals: 1000
  }
  options = Object.assign({}, defaults, options)

  options.statusCallback && options.statusCallback({progress: 0})

  // construct the contents of the XML
  var body = ['X3D',
    {
      profile: 'Interchange',
      version: '3.3',
github jscad / OpenJSCAD.org / packages / io / dxf-serializer / index.js View on Github external
const serialize = (...params) => {
  let options = {}
  let objects
  if (params.length === 0) {
    throw new Error('no arguments supplied to serialize function !')
  } else if (params.length === 1) {
    // assumed to be object(s)
    objects = Array.isArray(params[0]) ? params[0] : params
  } else if (params.length > 1) {
    options = params[0]
    objects = params[1]
  }
  // make sure we always deal with arrays of objects as inputs
  objects = toArray(objects)

  const defaults = {
    cagTo: 'lwpolyline', // or polyline
    csgTo: '3dface', // or polyline
    pathTo: 'lwpolyline',
    statusCallback: null
  }
  options = Object.assign({}, defaults, options)

  let dxfContent = `999
DXF generated by JSCAD
${dxfHeaders(options)}
${dxfClasses(options)}
${dxfTables(options)}
${dxfBlocks(options)}
${dxfEntities(objects, options)}
github jscad / OpenJSCAD.org / packages / io / stl-serializer / index.js View on Github external
const serialize = (...params) => {
  let options = {}
  let objects
  if (params.length === 0) {
    throw new Error('no arguments supplied to serialize function !')
  } else if (params.length === 1) {
    // assumed to be object(s)
    objects = Array.isArray(params[0]) ? params[0] : params
  } else if (params.length > 1) {
    options = params[0]
    objects = params[1]
  }
  // make sure we always deal with arrays of objects as inputs
  objects = toArray(objects)

  const defaults = {
    binary: true,
    statusCallback: null
  }
  options = Object.assign({}, defaults, options)

  // only use valid CSG objects
  let csgs = []
  objects.forEach(function (object, i) {
    if (isCSG(object) & object.polygons.length > 0) {
      csgs.push(ensureManifoldness(object))
    }
  })
  return options.binary ? serializeBinary(csgs, options) : serializeText(csgs, options)
}
github jscad / OpenJSCAD.org / packages / io / amf-serializer / index.js View on Github external
const serialize = (...params) => {
  let options = {}
  let objects
  if (params.length === 0) {
    throw new Error('no arguments supplied to serialize function !')
  } else if (params.length === 1) {
    // assumed to be object(s)
    objects = Array.isArray(params[0]) ? params[0] : params
  } else if (params.length > 1) {
    options = params[0]
    objects = params[1]
  }
  // make sure we always deal with arrays of objects as inputs
  objects = toArray(objects)

  const defaults = {
    statusCallback: null,
    unit: 'millimeter', // millimeter, inch, feet, meter or micrometer
    metadata: null
  }
  options = Object.assign({}, defaults, options)

  options.statusCallback && options.statusCallback({progress: 0})

  // construct the contents of the XML
  var body = ['amf',
    {
      unit: options.unit,
      version: '1.1'
    },
github jscad / OpenJSCAD.org / packages / io / stl-deserializer / index.js View on Github external
function deserializeBinarySTL (stl, filename, version, elementFormatter, debug = false) {
  // -- This makes more sense if you read http://en.wikipedia.org/wiki/STL_(file_format)#Binary_STL
  let vertices = []
  let triangles = []
  let normals = []
  let colors = []
  let converted = 0
  let vertexIndex = 0
  let err = 0
  let mcolor = null
  let umask = parseInt('01000000000000000', 2)
  let rmask = parseInt('00000000000011111', 2)
  let gmask = parseInt('00000001111100000', 2)
  let bmask = parseInt('00111110000000000', 2)
  let br = new BinaryReader(stl)

  let m = 0
  let c = 0
  let r = 0
  let g = 0
  let b = 0
  let a = 0
  for (let i = 0; i < 80; i++) {
    switch (m) {
      case 6:
        r = br.readUInt8()
        m += 1
        continue
      case 7:
        g = br.readUInt8()
        m += 1

@jscad/io-utils

Utilities for JSCAD IO Packages

MIT
Latest version published 1 month ago

Package Health Score

87 / 100
Full package analysis