Tutorials →
Get file metadata (mass, volume)
The sample code below reads an OBJ file to get the mass and volume of the object.
The sample below assumes you have exported your API token as an environment variable like so.
export ZOO_API_TOKEN="YOUR-API-TOKEN"
Replace YOUR-API-TOKEN
with one of your API tokens. It looks like you need to generate an API Token first.
Below is our API-client code:
#!/usr/bin/env python3
import json
import os
from kittycad.api.file import create_file_mass, create_file_volume
from kittycad.client import ClientFromEnv
from kittycad.models.error import Error
from kittycad.models.file_import_format import FileImportFormat
from kittycad.models.unit_density import UnitDensity
from kittycad.models.unit_mass import UnitMass
from kittycad.models.unit_volume import UnitVolume
# Create a new client with your token parsed from the environment variable:
# ZOO_API_TOKEN.
client = ClientFromEnv(timeout=500, verify_ssl=True)
# Read in the contents of the file.
file = open("./ORIGINALVOXEL-3.obj", "rb")
# LITTERBOX-END-NON-EDITABLE-SECTION
content = file.read()
file.close()
# We divide by 1e+9 here to convert from cubic millimeters to cubic meters.
steelDensityPerCubicMeter = 0.00785 / 1e9
fm = create_file_mass.sync(
client=client,
material_density=steelDensityPerCubicMeter,
src_format=FileImportFormat.OBJ,
material_density_unit=UnitDensity.KG_M3,
output_unit=UnitMass.G,
body=content,
)
fv = create_file_volume.sync(
client=client,
src_format=FileImportFormat.OBJ,
output_unit=UnitVolume.M3,
body=content,
)
if isinstance(fm, Error) or fm is None:
raise Exception("There was a problem with mass calculation")
if isinstance(fv, Error) or fv is None:
raise Exception("There was a problem with volume calculation")
print(f"File mass (grams): {fm.mass}")
print(f"File volume (m^3): {fv.volume}")
partInfo = {
"title": "output.json",
"volume": fv.volume,
"mass": fm.mass,
}
# LITTERBOX-START-NON-EDITABLE-SECTION
with open("output.json", "w", encoding="utf-8") as f:
json.dump(partInfo, f, ensure_ascii=False, indent=4)
os.system("cp ./ORIGINALVOXEL-3.obj ./output.obj")
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/kittycad/kittycad.go"
)
func main() {
// Create a new client with your token parsed from the environment variable:
// ZOO_API_TOKEN.
client, _ := kittycad.NewClientFromEnv("your apps user agent")
fileBytes, _ := os.ReadFile("./ORIGINALVOXEL-3.obj")
// LITTERBOX-END-NON-EDITABLE-SECTION
densitySteelGramsPerCubicMeter := 0.00784
fc, err := client.File.CreateMass(
densitySteelGramsPerCubicMeter,
kittycad.UnitDensityKgm3,
kittycad.UnitMasG,
kittycad.FileImportFormatObj,
fileBytes,
)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
fv, _ := client.File.CreateVolume(
kittycad.UnitVolumeM3,
kittycad.FileImportFormatObj,
fileBytes,
)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
fmt.Println("File mass (g): ", fc.Mass)
fmt.Println("File volume (m^3): ", fv.Volume)
json_data, _ := json.Marshal(struct {
Title string `json:"title"`
Mass float64 `json:"mass"`
Volume float64 `json:"volume"`
}{
Title: "output.json",
Mass: fc.Mass,
Volume: fv.Volume,
})
// LITTERBOX-START-NON-EDITABLE-SECTION
output_file_path := "./output.json"
output, _ := os.Create(output_file_path)
defer output.Close()
output.Write(json_data)
if err := output.Sync(); err != nil {
panic(err)
}
// Copy the original file to output.obj.
if err := copyFile("./ORIGINALVOXEL-3.obj", "./output.obj"); err != nil {
panic(err)
}
}
func copyFile(src, dst string) error {
// Open the source file for reading
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// Create the destination file
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
// Copy the content from src to dst
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}