--- title: Routes ---

We know that we are able to refer to deep objects by using the dot notation:

{% highlight javascript %} var bottomRight_bolt = plate.models.bolts.paths.BottomRight_bolt; {% endhighlight %}

The reference from plate to BottomRight_bolt is hard-coded. Suppose that we had a model plate2 which was a duplicate of plate - we would need to have to hard-code the reference to its BottomRight_bolt:

{% highlight javascript %} var bottomRight_bolt2 = plate2.models.bolts.paths.BottomRight_bolt; {% endhighlight %}

Instead of hard-coded dot notation, we can have an abstract way of referencing deep objects by using a route. It is simply an array of strings that represent the segments names between the dots. We do not put the root object in a route. A route that we can apply to both plate and plate2 would be:

{% highlight javascript %} var route = ["models", "bolts", "paths", "BottomRight_bolt"]; {% endhighlight %}

Travel a route

Use makerjs.travel(rootModel, route) to get to a child object in rootModel via a route. This function will return an object with these 2 properties:

{% highlight javascript %} //mounting plate var makerjs = require('makerjs'); var plate = { models: { outer: makerjs.model.center(new makerjs.models.RoundRectangle(120, 100, 10)), bolts: makerjs.model.center(new makerjs.models.BoltRectangle(100, 80, 5)) }, paths: { hole: new makerjs.paths.Circle(25) } }; var plate2 = makerjs.cloneObject(plate); plate2.origin = [130, 0]; //route to the BottomRight_bolt circle var route = ["models", "bolts", "paths", "BottomRight_bolt"]; //create a local variables for BottomRight_bolt holes var bottomRight_bolt = makerjs.travel(plate, route).result; bottomRight_bolt.radius = 2; var bottomRight_bolt2 = makerjs.travel(plate2, route).result; bottomRight_bolt2.radius = 3; var plates = { models: { plate: plate, plate2: plate2 } }; var svg = makerjs.exporter.toSVG(plates); document.write(svg); {% endhighlight %}

Patterns in Routes

Notice that the schema for Maker.js models has a pattern of models.modelName and paths.pathName. There are always 2 segments between model and/or path objects. So, in any given route to an object, you can always get to its parent by subtracting the last 2 array elements of the route. We will use Array.slice(0, -2) to make a copy of the route array without the last 2 elements:

{% highlight javascript %} //mounting plate var makerjs = require('makerjs'); var plate = { models: { outer: makerjs.model.center(new makerjs.models.RoundRectangle(120, 100, 10)), bolts: makerjs.model.center(new makerjs.models.BoltRectangle(100, 80, 5)) }, paths: { hole: new makerjs.paths.Circle(25) } }; var plate2 = makerjs.cloneObject(plate); plate2.origin = [130, 0]; //route to the BottomRight_bolt circle var route = ["models", "bolts", "paths", "BottomRight_bolt"]; //create a local variables for BottomRight_bolt holes var bottomRight_bolt = makerjs.travel(plate, route).result; bottomRight_bolt.radius = 2; //subtract 2 elements to get the parent var parentRoute = route.slice(0, -2); var bolts = makerjs.travel(plate2, parentRoute).result; //modify children delete bolts.paths.TopLeft_bolt; delete bolts.paths.BottomRight_bolt; var plates = { models: { plate: plate, plate2: plate2 } }; var svg = makerjs.exporter.toSVG(plates); document.write(svg); {% endhighlight %}

Route Keys

Additionally, we can "flatten" a route array into a string, known as a route key, by calling makerjs.createRouteKey(route) and passing a route. Every route key is of course unique in the scope of the root object. It may used as a unique id of a child path or model.