microsoft / microsoft/maker.js

[request] Simple (alternative) public api or static "make" function on objects

Open
#386 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
2k
Forks
302
Avg merge
21h 28m
Merged PRs (30d)
5

Description

Hi,

I think it would be nice to have a simpler public API for scripting / hacking in the spirit of openscad, basically throwing all functions together in a single object, an example of what I mean would looks like this:

import { make } from 'makerjs'

const ball = make.ellipse(18, 18)
const hanger = make.move(make.ellipse(4, 4), [0, 18 + 2])
const ballInside = make.offset(ball, -2)
const hangerInside = make.offset(hanger, -2)
const ballAndHanger = make.union(ball, hanger)
const christmasBall = make.move({
    models: { ballAndHanger, ballInside, hangerInside }
}, [20, 20])

I have got the above somewhat working using my hack below:

interface Make {
    union: typeof maker.model.combineUnion,
    difference: typeof maker.model.combineSubtraction,
    intersection: typeof maker.model.combineIntersection,
}

const make: Make = {
    ...(Object.keys(maker.models).reduce((memo: any, modelName: string) => {
        const lcFirstModelName = modelName.charAt(0).toLowerCase() + modelName.slice(1);
        memo[lcFirstModelName] = (...args: any[]) => new maker.models[modelName](...args)
        return memo
    }, {})),
    ...(Object.keys(maker.model).reduce((memo: any, modelName: string) => {
        memo[modelName] = (...args: any[]) => new maker.model[modelName](...args)
        return memo
    }, {})),
    union: maker.model.combineUnion,
    difference: maker.model.combineSubtraction,
    intersection: maker.model.combineIntersection,
    offset: (modelToOutline: maker.IModel, offset: number, joints: number = 0) => {
        return maker.model.outline(modelToOutline, Math.abs(offset), joints, offset < 0)
    },
    // ... etc

However, the types of the model constructors are not working (as expected) so I would like to turn the above into something like:

const make = {
    belt: maker.models.Belt.make,
    bezierCurve: maker.models.BezierCurve.make,
    // ...etc
}

Most of the models constructors have overloads, which means I would have to re-implement them & keep them up to date (unless there is another way to do it that I am not aware of?)
So I am wondering if it's possible to get this into the core (not necessarily the simple api, but at least the static "make" functions that construct a new instance of those model classes)

Hope this makes sense,

  • Koen

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the public maker.models and maker.model entry points, including the model constructors and combine/outline functions shown in the request. Determine how a core static make API or combined make object could preserve constructor overloads; the work is done when the proposed public API and its TypeScript types consistently construct and manipulate the referenced models.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.