Skip to content

Commit

Permalink
Updated external db sync to handle spool types and new color hex format
Browse files Browse the repository at this point in the history
  • Loading branch information
Donkie committed May 20, 2024
1 parent 5dcc28a commit 52c52bc
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
9 changes: 8 additions & 1 deletion client/src/components/filamentImportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export function FilamentImportModal(props: {
const filamentOptions =
externalFilaments.data?.map((item) => {
return {
label: formatFilamentLabel(item.name, item.diameter, item.manufacturer, item.material, item.weight),
label: formatFilamentLabel(
item.name,
item.diameter,
item.manufacturer,
item.material,
item.weight,
item.spool_type
),
value: item.id,
item: item,
};
Expand Down
10 changes: 9 additions & 1 deletion client/src/pages/filaments/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { IFilament } from "./model";
export async function createFilamentFromExternal(externalFilament: ExternalFilament): Promise<IFilament> {
const vendor = await getOrCreateVendorFromExternal(externalFilament.manufacturer);

let color_hex = undefined;
if (externalFilament.color_hex) {
color_hex = externalFilament.color_hex;
} else if (externalFilament.color_hexes && externalFilament.color_hexes.length > 0) {
// TODO: Support for multi-color filaments
color_hex = externalFilament.color_hexes[0];
}

const body: Omit<IFilament, "id" | "registered" | "extra"> & { vendor_id: number } = {
name: externalFilament.name,
material: externalFilament.material,
Expand All @@ -18,7 +26,7 @@ export async function createFilamentFromExternal(externalFilament: ExternalFilam
diameter: externalFilament.diameter,
weight: externalFilament.weight,
spool_weight: externalFilament.spool_weight || undefined,
color_hex: externalFilament.color_hex,
color_hex: color_hex,
settings_extruder_temp: externalFilament.extruder_temp || undefined,
settings_bed_temp: externalFilament.bed_temp || undefined,
external_id: externalFilament.id,
Expand Down
17 changes: 14 additions & 3 deletions client/src/pages/spools/functions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatLength, formatWeight } from "../../utils/parsing";
import { getAPIURL } from "../../utils/url";
import { ISpool } from "./model";
import { IFilament } from "../filaments/model";
import { useGetExternalDBFilaments } from "../../utils/queryExternalDB";
import { SpoolType, useGetExternalDBFilaments } from "../../utils/queryExternalDB";
import { useMemo } from "react";

export async function setSpoolArchived(spool: ISpool, archived: boolean) {
Expand All @@ -28,7 +28,8 @@ export function formatFilamentLabel(
diameter: number,
vendorName?: string,
material?: string,
weight?: number
weight?: number,
spoolType?: SpoolType
): string {
const portions = [];
if (vendorName) {
Expand All @@ -43,6 +44,9 @@ export function formatFilamentLabel(
if (weight) {
extras.push(formatWeight(weight));
}
if (spoolType) {
extras.push(spoolType.charAt(0).toUpperCase() + spoolType.slice(1) + " spool");
}
return `${portions.join(" - ")} (${extras.join(", ")})`;
}

Expand Down Expand Up @@ -89,7 +93,14 @@ export function useGetFilamentSelectOptions() {
const data =
externalFilaments.data?.map((item) => {
return {
label: formatFilamentLabel(item.name, item.diameter, item.manufacturer, item.material, item.weight),
label: formatFilamentLabel(
item.name,
item.diameter,
item.manufacturer,
item.material,
item.weight,
item.spool_type
),
value: item.id,
weight: item.weight,
spool_weight: item.spool_weight || undefined,
Expand Down
36 changes: 32 additions & 4 deletions client/src/utils/queryExternalDB.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { getAPIURL } from "./url";

export enum SpoolType {
PLASTIC = "plastic",
CARDBOARD = "cardboard",
METAL = "metal",
}

export enum Finish {
MATTE = "matte",
GLOSSY = "glossy",
}

export enum MultiColorDirection {
COAXIAL = "coaxial",
LONGITUDINAL = "longitudinal",
}

export enum Pattern {
MARBLE = "marble",
SPARKLE = "sparkle",
}

export interface ExternalFilament {
id: string;
manufacturer: string;
name: string;
material: string;
density: number;
weight: number;
spool_weight: number | null;
spool_weight?: number;
spool_type?: SpoolType;
diameter: number;
color_hex: string;
extruder_temp: number | null;
bed_temp: number | null;
color_hex?: string;
color_hexes?: string[];
extruder_temp?: number;
bed_temp?: number;
finish?: Finish;
multi_color_direction?: MultiColorDirection;
pattern?: Pattern;
translucent: boolean;
glow: boolean;
}

export interface ExternalMaterial {
Expand Down
43 changes: 42 additions & 1 deletion spoolman/externaldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import datetime
import logging
import os
from enum import Enum
from pathlib import Path
from typing import Optional

Expand All @@ -21,6 +22,27 @@
logger = logging.getLogger(__name__)


class SpoolType(Enum):
PLASTIC = "plastic"
CARDBOARD = "cardboard"
METAL = "metal"


class Finish(Enum):
MATTE = "matte"
GLOSSY = "glossy"


class MultiColorDirection(Enum):
COAXIAL = "coaxial"
LONGITUDINAL = "longitudinal"


class Pattern(Enum):
MARBLE = "marble"
SPARKLE = "sparkle"


class ExternalFilament(BaseModel):
id: str = Field(description="A unique ID for this filament.", example="polymaker_pla_polysonicblack_1000_175")
manufacturer: str = Field(description="Filament manufacturer.", example="Polymaker")
Expand All @@ -29,10 +51,29 @@ class ExternalFilament(BaseModel):
density: float = Field(description="Density in g/cm3.", example=1.23)
weight: float = Field(description="Net weight of a single spool.", example=1000)
spool_weight: Optional[float] = Field(default=None, description="Weight of an empty spool.", example=140)
spool_type: Optional[SpoolType] = Field(description="Type of spool.", example=SpoolType.PLASTIC)
diameter: float = Field(description="Filament in mm.", example=1.75)
color_hex: str = Field(description="Filament color code in hex format.", example="2c3232")
color_hex: Optional[str] = Field(
default=None,
description="Filament color code in hex format, for single-color filaments.",
example="2c3232",
)
color_hexes: Optional[list[str]] = Field(
default=None,
description="For multi-color filaments. List of hex color codes in hex format.",
example=["2c3232", "5f5f5f"],
)
extruder_temp: Optional[int] = Field(default=None, description="Extruder/nozzle temperature in °C.", example=210)
bed_temp: Optional[int] = Field(default=None, description="Bed temperature in °C.", example=50)
finish: Optional[Finish] = Field(default=None, description="Finish of the filament.", example=Finish.MATTE)
multi_color_direction: Optional[MultiColorDirection] = Field(
default=None,
description="Direction of multi-color filaments.",
example=MultiColorDirection.COAXIAL,
)
pattern: Optional[Pattern] = Field(default=None, description="Pattern of the filament.", example=Pattern.MARBLE)
translucent: bool = Field(default=False, description="Whether the filament is translucent.")
glow: bool = Field(default=False, description="Whether the filament is glow-in-the-dark.")


class ExternalFilamentsFile(BaseModel):
Expand Down

0 comments on commit 52c52bc

Please sign in to comment.