Files
silo-mod/freecad/templates/inject_template.py
forbes-0023 a88e104d94 feat(templates): document templating system with Save as Template command
Add a template system that lets users create new items from pre-configured
.kc template files and save existing documents as reusable templates.

Template use (New Item form):
- templates.py: discovery, filtering, TemplateInfo dataclass
- schema_form.py: template combo picker filtered by type/category
- silo_commands.py: SiloSync.create_document_from_template() copies
  template .kc, strips identity, stamps Silo properties

Template creation (Save as Template):
- SaveAsTemplateDialog: captures name, description, item types,
  categories, author, and tags
- Silo_SaveAsTemplate command: copies doc, strips Silo identity,
  injects silo/template.json, optionally uploads to Silo
- Registered in Silo menu via InitGui.py

Template search paths (3-tier, later shadows earlier by name):
1. mods/silo/freecad/templates/ (system)
2. ~/.local/share/FreeCAD/Templates/ (personal, sister to Macro/)
3. ~/projects/templates/ (org-shared)
2026-02-21 09:06:26 -06:00

79 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Inject silo/template.json into a .kc file to make it a template.
Usage:
python inject_template.py <kc_file> <name> [--type part|assembly] [--description "..."]
Examples:
python inject_template.py part.kc "Part (Generic)" --type part
python inject_template.py sheet-metal-part.kc "Sheet Metal Part" --type part \
--description "Body with SheetMetal base feature and laser-cut job"
python inject_template.py assembly.kc "Assembly" --type assembly
"""
import argparse
import json
import os
import sys
# Allow importing from the parent freecad/ directory when run standalone
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from templates import inject_template_json
def main():
parser = argparse.ArgumentParser(
description="Inject template metadata into a .kc file"
)
parser.add_argument("kc_file", help="Path to the .kc file")
parser.add_argument("name", help="Template display name")
parser.add_argument(
"--type",
dest="item_types",
action="append",
default=[],
help="Item type filter (part, assembly). Can be repeated.",
)
parser.add_argument("--description", default="", help="Template description")
parser.add_argument("--icon", default="", help="Icon identifier")
parser.add_argument("--author", default="Kindred Systems", help="Author name")
parser.add_argument(
"--category",
dest="categories",
action="append",
default=[],
help="Category prefix filter. Can be repeated. Empty = all.",
)
parser.add_argument(
"--tag",
dest="tags",
action="append",
default=[],
help="Searchable tags. Can be repeated.",
)
args = parser.parse_args()
if not args.item_types:
args.item_types = ["part"]
template_info = {
"template_version": "1.0",
"name": args.name,
"description": args.description,
"item_types": args.item_types,
"categories": args.categories,
"icon": args.icon,
"author": args.author,
"tags": args.tags,
}
if inject_template_json(args.kc_file, template_info):
print(f"Injected silo/template.json into {args.kc_file}")
print(json.dumps(template_info, indent=2))
else:
sys.exit(1)
if __name__ == "__main__":
main()