Create Loops
In DynaMaker, loops can be created with the built-in functions given by the TypeScript languge. These are the most common loops.
Loops such as while and do...while are not included here because they can more easily lead to infinite loops if
their exit condition is never met. Always make sure that the exit condition of a loop can eventually become false.
for
A for loop repeats some code while a condition is true. Define where the loop starts, when it should stop, and how
it changes after each iteration.
for (start; condition; iterationUpdate) {
// code executed on each iteration
}
-
Example with a sketch containing 20 hole-sketches with dynamic position in x-direction:
export function getHolesSketch() {const holesSketch = new SKYCAD.Sketch()const nrHoles = 20let posX = 0for (let i = 0; i < nrHoles; i++) {const circleSketch = SKYCAD.generateCircleSketch(posX, 0, 50)holesSketch.mergeSketch(circleSketch)posX += 100}return holesSketch} -
Example with hinges (static models) distributed equally throughout a door height:
export function getHingesGeometry(doorHeight: number) {const geometryGroup = new SKYCAD.GeometryGroup()const hingeModel = ASSETS.STATIC_MODELS.HINGEconst MAX_DISTANCE_BETWEEN_HINGES = 500const nrHinges = Math.ceil(doorHeight / MAX_DISTANCE_BETWEEN_HINGES) // Math.ceil() to round upconst stepPosZ = height / nrHingesfor (let posZ = 0.5 * stepPosZ; posZ < height; posZ += stepPosZ) {geometryGroup.addGeometry(hingeModel, {position: new SKYMATH.Vector3D(0, 0, posZ),})}return geometryGroup}
forEach
A forEach loop runs some code once for each item in a itemList. You don't need to keep track of the current index or
define when the loop should stop.
itemList.forEach((item) => {
// logic for each item
})
-
Example to get the total length given these
plankLengths:export function getTotalLength() {const plankLengths = [100, 250, 150, 300]let totalLength = 0plankLengths.forEach((plankLength) => {totalLength += plankLength})return totalLength} -
Example to generate hinges geometry given
hingeModeland thehingePositionsas a list of numbers:export function getHingesGeometry(hingePositions: number[]) {const geometryGroup = new SKYCAD.GeometryGroup()const hingeModel = ASSETS.STATIC_MODELS.HINGEhingePositions.forEach((posZ) => {geometryGroup.addGeometry(hingeModel, {position: new SKYMATH.Vector3D(0, 0, posZ),})})return geometryGroup}
for...of
A for...of loop runs some code once for each item in an itemList. You don't need to keep track of the current
index or define when the loop should stop.
for (const item of itemList) {
// logic for each item
}
-
Example to get the total length given these
plankLengths:export function getTotalLength() {const plankLengths = [100, 250, 150, 300]let totalLength = 0for (const plankLength of plankLengths) {totalLength += plankLength}return totalLength} -
Example to generate hinges geometry given
hingeModeland thehingePositionsas a list of numbers:export function getHingesGeometry(hingePositions: number[]) {const geometryGroup = new SKYCAD.GeometryGroup()const hingeModel = ASSETS.STATIC_MODELS.HINGEfor (const posZ of hingePositions) {geometryGroup.addGeometry(hingeModel, {position: new SKYMATH.Vector3D(0, 0, posZ),})}return geometryGroup}