2024-11-15 11:36:38 -05:00
|
|
|
---
|
|
|
|
title: "KCL Modules"
|
|
|
|
excerpt: "Documentation of modules for the KCL language for the Zoo Modeling App."
|
|
|
|
layout: manual
|
|
|
|
---
|
|
|
|
|
|
|
|
`KCL` allows splitting code up into multiple files. Each file is somewhat
|
|
|
|
isolated from other files as a separate module.
|
|
|
|
|
|
|
|
When you define a function, you can use `export` before it to make it available
|
|
|
|
to other modules.
|
|
|
|
|
|
|
|
```
|
|
|
|
// util.kcl
|
2024-12-03 14:47:21 -05:00
|
|
|
export fn increment(x) {
|
2024-11-15 11:36:38 -05:00
|
|
|
return x + 1
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Other files in the project can now import functions that have been exported.
|
|
|
|
This makes them available to use in another file.
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2024-11-15 11:36:38 -05:00
|
|
|
// main.kcl
|
|
|
|
import increment from "util.kcl"
|
|
|
|
|
|
|
|
answer = increment(41)
|
|
|
|
```
|
|
|
|
|
|
|
|
Imported files _must_ be in the same project so that units are uniform across
|
|
|
|
modules. This means that it must be in the same directory.
|
|
|
|
|
|
|
|
Import statements must be at the top-level of a file. It is not allowed to have
|
|
|
|
an `import` statement inside a function or in the body of an if-else.
|
|
|
|
|
|
|
|
Multiple functions can be exported in a file.
|
|
|
|
|
|
|
|
```
|
|
|
|
// util.kcl
|
2024-12-03 14:47:21 -05:00
|
|
|
export fn increment(x) {
|
2024-11-15 11:36:38 -05:00
|
|
|
return x + 1
|
|
|
|
}
|
|
|
|
|
2024-12-03 14:47:21 -05:00
|
|
|
export fn decrement(x) {
|
2024-11-15 11:36:38 -05:00
|
|
|
return x - 1
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
When importing, you can import multiple functions at once.
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2024-11-15 11:36:38 -05:00
|
|
|
import increment, decrement from "util.kcl"
|
|
|
|
```
|
|
|
|
|
|
|
|
Imported symbols can be renamed for convenience or to avoid name collisions.
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2024-11-15 11:36:38 -05:00
|
|
|
import increment as inc, decrement as dec from "util.kcl"
|
|
|
|
```
|
2025-02-13 06:24:27 +13:00
|
|
|
|
|
|
|
## Importing files from other CAD systems
|
|
|
|
|
|
|
|
`import` can also be used to import files from other CAD systems. The format of the statement is the
|
|
|
|
same as for KCL files. You can only import the whole file, not items from it. E.g.,
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2025-02-13 06:24:27 +13:00
|
|
|
import "tests/inputs/cube.obj"
|
|
|
|
|
|
|
|
// Use `cube` just like a KCL object.
|
|
|
|
```
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2025-02-13 06:24:27 +13:00
|
|
|
import "tests/inputs/cube-2.sldprt" as cube
|
|
|
|
|
|
|
|
// Use `cube` just like a KCL object.
|
|
|
|
```
|
|
|
|
|
|
|
|
You can make the file format explicit using a format attribute (useful if using a different
|
|
|
|
extension), e.g.,
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2025-02-13 06:24:27 +13:00
|
|
|
@(format = obj)
|
|
|
|
import "tests/inputs/cube"
|
|
|
|
```
|
|
|
|
|
|
|
|
For formats lacking unit data (such as STL, OBJ, or PLY files), the default
|
|
|
|
unit of measurement is millimeters. Alternatively you may specify the unit
|
|
|
|
by using an attirbute. Likewise, you can also specify a coordinate system. E.g.,
|
|
|
|
|
2025-02-27 09:34:55 +13:00
|
|
|
```norun
|
2025-02-13 06:24:27 +13:00
|
|
|
@(unitLength = ft, coords = opengl)
|
|
|
|
import "tests/inputs/cube.obj"
|
|
|
|
```
|
|
|
|
|
|
|
|
When importing a GLTF file, the bin file will be imported as well.
|
|
|
|
|
|
|
|
Import paths are relative to the current project directory. Imports currently only work when
|
|
|
|
using the native Modeling App, not in the browser.
|
|
|
|
|
|
|
|
### Supported values
|
|
|
|
|
|
|
|
File formats: `fbx`, `gltf`/`glb`, `obj`+, `ply`+, `sldprt`, `step`/`stp`, `stl`+. (Those marked with a
|
|
|
|
'+' support customising the length unit and coordinate system).
|
|
|
|
|
|
|
|
Length units: `mm` (the default), `cm`, `m`, `inch`, `ft`, `yd`.
|
|
|
|
|
|
|
|
Coordinate systems:
|
|
|
|
|
|
|
|
- `zoo` (the default), forward: -Y, up: +Z, handedness: right
|
|
|
|
- `opengl`, forward: +Z, up: +Y, handedness: right
|
|
|
|
- `vulkan`, forward: +Z, up: -Y, handedness: left
|