Documentation
¶
Overview ¶
Package tmf implements parsing and construction of 3MF (3D Manufacturing Format) files.
3MF is an XML-based packaging format for additive manufacturing data, distributed as an OPC (Open Packaging Conventions) ZIP container. This package implements the 3MF core specification together with the major 3MF extensions (Materials, Production, Beam Lattice, Slice, Secure Content, and Volumetric Extensions).
Reading ¶
pkg, err := tmf.Open("model.3mf")
if err != nil { ... }
defer pkg.Close()
model := pkg.Model()
for _, obj := range model.Resources().Objects() {
// ...
}
Writing ¶
mesh := tmf.NewMesh(
tmf.WithVertices(verts),
tmf.WithTriangles(tris),
)
obj := tmf.NewObject(
tmf.WithObjectID(1),
tmf.WithObjectType(tmf.ObjectTypeModel),
tmf.WithMesh(mesh),
)
model := tmf.NewModel(
tmf.WithUnit(tmf.UnitMillimeter),
tmf.WithObjects(obj),
tmf.WithBuildItem(tmf.NewBuildItem(tmf.WithObjectRef(obj))),
)
pkg := tmf.NewPackage(tmf.WithModel(model))
if err := pkg.Save("output.3mf"); err != nil { ... }
Extensions ¶
Extension data is exposed both through the typed sub-packages (github.com/lestrrat-go/3mf/materials, .../production, .../beamlattice, .../slice, .../securecontent, .../volumetric) and through a generic attachment mechanism on Object, Mesh, Build, and Model values so that unrecognized extension data is preserved on round-trip.
Index ¶
- Constants
- Variables
- func FormatMatrix(m Matrix) string
- func MarshalModel(m *Model) ([]byte, error)
- func RegisterExtensionReader(r ExtensionReader)
- func RegisterExtensionWriter(w ExtensionWriter)
- func WriteModel(w io.Writer, m *Model) error
- type Attachment
- type BaseExtensionReader
- type BaseExtensionWriter
- type BaseMaterial
- type BaseMaterials
- type Build
- type BuildItem
- type Color
- type Component
- type Element
- func (e *Element) Attr(local string) string
- func (e *Element) AttrNS(local, namespaceURI string) string
- func (e *Element) Attributes() []ElementAttribute
- func (e *Element) ChildElements(local string) iter.Seq[*Element]
- func (e *Element) LocalName() string
- func (e *Element) NamespaceURI() string
- func (e *Element) TextContent() string
- type ElementAttribute
- type ExtensionReader
- type ExtensionWriter
- type Matrix
- type Mesh
- func (m *Mesh) AppendTriangle(t Triangle)
- func (m *Mesh) AppendVertex(v Vertex) uint32
- func (m *Mesh) Extension(ns string) any
- func (m *Mesh) Extensions() map[string]any
- func (m *Mesh) SetExtension(ns string, v any)
- func (m *Mesh) SetTriangles(t []Triangle)
- func (m *Mesh) SetVertices(v []Vertex)
- func (m *Mesh) Triangles() []Triangle
- func (m *Mesh) Vertices() []Vertex
- type Metadata
- type Model
- func (m *Model) AppendMetadata(md *Metadata)
- func (m *Model) Build() *Build
- func (m *Model) Extension(ns string) any
- func (m *Model) Extensions() map[string]any
- func (m *Model) Language() string
- func (m *Model) Metadata() []*Metadata
- func (m *Model) RequireExtension(ns, prefix string)
- func (m *Model) RequiredExtensions() map[string]string
- func (m *Model) Resources() *Resources
- func (m *Model) SetExtension(ns string, v any)
- func (m *Model) SetLanguage(s string)
- func (m *Model) SetThumbnail(s string)
- func (m *Model) SetUnit(u Unit)
- func (m *Model) Thumbnail() string
- func (m *Model) Unit() Unit
- type Object
- func (o *Object) AppendComponent(c *Component)
- func (o *Object) Components() []*Component
- func (o *Object) Extension(ns string) any
- func (o *Object) Extensions() map[string]any
- func (o *Object) ID() uint32
- func (o *Object) Mesh() *Mesh
- func (o *Object) Name() string
- func (o *Object) PID() (uint32, bool)
- func (o *Object) PIndex() uint32
- func (o *Object) PartNumber() string
- func (o *Object) SetComponents(cs []*Component)
- func (o *Object) SetExtension(ns string, v any)
- func (o *Object) SetID(id uint32)
- func (o *Object) SetMesh(m *Mesh)
- func (o *Object) SetName(s string)
- func (o *Object) SetPID(pid uint32)
- func (o *Object) SetPIndex(idx uint32)
- func (o *Object) SetPartNumber(s string)
- func (o *Object) SetThumbnail(s string)
- func (o *Object) SetType(t ObjectType)
- func (o *Object) SetUUID(s string)
- func (o *Object) Thumbnail() string
- func (o *Object) Type() ObjectType
- func (o *Object) UUID() string
- type ObjectType
- type Option
- func WithAttachment(a Attachment) Option
- func WithBaseMaterials(b *BaseMaterials) Option
- func WithBuildItem(b *BuildItem) Option
- func WithBuildObjectID(id uint32) Option
- func WithComponent(c *Component) Option
- func WithComponentObjectRef(o *Object) Option
- func WithComponentPath(p string) Option
- func WithComponentTransform(m Matrix) Option
- func WithComponentUUID(u string) Option
- func WithComponents(cs []*Component) Option
- func WithExtension(namespace string, value any) Option
- func WithItemMetadata(m *Metadata) Option
- func WithItemPartNumber(s string) Option
- func WithItemPath(p string) Option
- func WithItemTransform(m Matrix) Option
- func WithItemUUID(s string) Option
- func WithLanguage(s string) Option
- func WithMesh(m *Mesh) Option
- func WithMetadataName(s string) Option
- func WithMetadataPreserve(b bool) Option
- func WithMetadataType(s string) Option
- func WithMetadataValue(s string) Option
- func WithModel(m *Model) Option
- func WithModelMetadata(md *Metadata) Option
- func WithModelPath(p string) Option
- func WithObject(o *Object) Option
- func WithObjectID(id uint32) Option
- func WithObjectName(s string) Option
- func WithObjectPID(pid uint32) Option
- func WithObjectPIndex(idx uint32) Option
- func WithObjectPartNumber(s string) Option
- func WithObjectRef(o *Object) Option
- func WithObjectThumbnail(s string) Option
- func WithObjectType(t ObjectType) Option
- func WithObjectUUID(s string) Option
- func WithObjects(objs ...*Object) []Option
- func WithPackageThumbnail(t Thumbnail) Option
- func WithRequiredExtension(namespace, prefix string) Option
- func WithResources(r *Resources) Option
- func WithThumbnail(s string) Option
- func WithTriangles(t []Triangle) Option
- func WithUnit(u Unit) Option
- func WithVertices(v []Vertex) Option
- type Package
- func (p *Package) AddAttachment(a Attachment)
- func (p *Package) AddPart(name, contentType string, data []byte)
- func (p *Package) AdditionalModel(name string) (*Model, error)
- func (p *Package) Model() *Model
- func (p *Package) ModelPath() string
- func (p *Package) OPC() *opc.Package
- func (p *Package) Part(name string) []byte
- func (p *Package) Save(filePath string) error
- func (p *Package) SetModel(m *Model)
- func (p *Package) WriteTo(w io.Writer) (int64, error)
- type Resources
- func (r *Resources) AppendBaseMaterials(b *BaseMaterials)
- func (r *Resources) AppendObject(o *Object)
- func (r *Resources) BaseMaterials() []*BaseMaterials
- func (r *Resources) ExtensionResources(ns string) any
- func (r *Resources) FindBaseMaterials(id uint32) *BaseMaterials
- func (r *Resources) FindObject(id uint32) *Object
- func (r *Resources) Objects() []*Object
- func (r *Resources) SetExtensionResources(ns string, v any)
- type Thumbnail
- type Triangle
- type Unit
- type Vertex
- type Writer
- func (w *Writer) Attr(name, value string) error
- func (w *Writer) AttrNS(prefix, localName, value string) error
- func (w *Writer) DeclarePrefix(prefix, uri string)
- func (w *Writer) EndDocument() error
- func (w *Writer) EndElement() error
- func (w *Writer) PrefixFor(uri string) string
- func (w *Writer) StartDocument(version, encoding, standalone string) error
- func (w *Writer) StartElement(name string) error
- func (w *Writer) StartElementNS(prefix, localName string) error
- func (w *Writer) WriteString(s string) error
Constants ¶
const ( // NSCore is the 3MF core specification namespace. NSCore = "http://schemas.microsoft.com/3dmanufacturing/core/2015/02" // NSMaterials is the 3MF Materials and Properties Extension namespace. NSMaterials = "http://schemas.microsoft.com/3dmanufacturing/material/2015/02" // NSProduction is the 3MF Production Extension namespace. NSProduction = "http://schemas.microsoft.com/3dmanufacturing/production/2015/06" // NSBeamLattice is the 3MF Beam Lattice Extension namespace. NSBeamLattice = "http://schemas.microsoft.com/3dmanufacturing/beamlattice/2017/02" // NSSlice is the 3MF Slice Extension namespace. NSSlice = "http://schemas.microsoft.com/3dmanufacturing/slice/2015/07" // NSSecureContent is the 3MF Secure Content Extension namespace. NSSecureContent = "http://schemas.microsoft.com/3dmanufacturing/securecontent/2019/04" // NSVolumetric is the 3MF Volumetric Extension namespace. NSVolumetric = "http://schemas.microsoft.com/3dmanufacturing/volumetric/2022/01" // NSXML is the XML namespace itself (used for xml:lang etc.). NSXML = "http://www.w3.org/XML/1998/namespace" // NSOPCContentTypes is the OPC Content Types namespace. NSOPCContentTypes = "http://schemas.openxmlformats.org/package/2006/content-types" // NSOPCRelationships is the OPC Relationships namespace. NSOPCRelationships = "http://schemas.openxmlformats.org/package/2006/relationships" // NSOPCMetadataCoreProperties is the OPC Core Properties namespace. NSOPCMetadataCoreProperties = "http://schemas.openxmlformats.org/package/2006/metadata/core-properties" )
Namespace URIs used in 3MF documents and OPC packages.
const ( PrefixMaterials = "m" PrefixProduction = "p" PrefixBeamLattice = "b" PrefixSlice = "s" PrefixSecureContent = "sc" PrefixVolumetric = "v" )
Conventional prefixes used when writing 3MF XML. These are the same prefixes used by Microsoft's reference implementation and are widely recognized by 3MF consumers.
const ( // RelTypeStartPart is the OPC relationship type for the primary 3MF model part. RelTypeStartPart = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dmodel" // RelTypeThumbnail is the OPC relationship type for a package-level thumbnail. RelTypeThumbnail = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail" // RelTypeTexture is the relationship type for a texture referenced from a model part. RelTypeTexture = "http://schemas.microsoft.com/3dmanufacturing/2013/01/3dtexture" // RelTypePrintTicket is the relationship type for a print ticket. RelTypePrintTicket = "http://schemas.microsoft.com/3dmanufacturing/2013/01/printticket" // RelTypeCoreProperties is the relationship type for OPC core metadata. RelTypeCoreProperties = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" // RelTypeMustPreserve is the relationship type used by the Production // extension to flag non-root model parts that must be preserved. RelTypeMustPreserve = "http://schemas.openxmlformats.org/package/2006/relationships/mustpreserve" )
Relationship type URIs.
const ( ContentType3DModel = "application/vnd.ms-package.3dmanufacturing-3dmodel+xml" ContentTypePrintTicket = "application/vnd.ms-printing.printticket+xml" ContentTypeCoreProperties = "application/vnd.openxmlformats-package.core-properties+xml" ContentTypeRelationships = "application/vnd.openxmlformats-package.relationships+xml" ContentTypePNG = "image/png" ContentTypeJPEG = "image/jpeg" ContentTypeTexture = "application/vnd.ms-package.3dmanufacturing-3dmodeltexture" )
Content types for parts contained in a 3MF package.
const DefaultModelPath = "/3D/3dmodel.model"
DefaultModelPath is the conventional path for the root 3MF model part.
Variables ¶
var ( // ErrInvalidPackage is returned when an OPC package cannot be read because // it is malformed or missing required parts. ErrInvalidPackage = errors.New("tmf: invalid package") // ErrNoModelPart is returned when an OPC package has no relationship that // points to a 3MF model part. ErrNoModelPart = errors.New("tmf: no 3D model part") // ErrUnknownExtension is returned when a parser encounters a namespace // declared in the requiredextensions list that this library does not // implement. ErrUnknownExtension = errors.New("tmf: unknown required extension") // ErrMalformedModel is returned when the 3dmodel.model XML does not // match the expected schema. ErrMalformedModel = errors.New("tmf: malformed model XML") )
var ReservedMetadataNames = map[string]struct{}{
"Title": {},
"Designer": {},
"Description": {},
"Copyright": {},
"LicenseTerms": {},
"Rating": {},
"CreationDate": {},
"ModificationDate": {},
"Application": {},
}
ReservedMetadataNames is the set of metadata names reserved by the 3MF core specification. Producers should not use these names with custom semantics.
Functions ¶
func FormatMatrix ¶
FormatMatrix serializes m as a 12-component, space-separated transform string suitable for use as a transform attribute. The shortest decimal representation that round-trips is used.
func MarshalModel ¶
MarshalModel serializes m as a 3dmodel.model XML document.
func RegisterExtensionReader ¶
func RegisterExtensionReader(r ExtensionReader)
RegisterExtensionReader installs r as the reader for r.Namespace(). It is safe to call from package init functions. Re-registering a namespace replaces the previous handler.
func RegisterExtensionWriter ¶
func RegisterExtensionWriter(w ExtensionWriter)
RegisterExtensionWriter installs w as the writer for w.Namespace().
Types ¶
type Attachment ¶
type Attachment struct {
Path string
ContentType string
RelType string // when non-empty, a package relationship is created
Data []byte
}
Attachment is an extra OPC part written into the package, optionally with a package-level relationship of the given type.
type BaseExtensionReader ¶
type BaseExtensionReader struct{}
BaseExtensionReader is a convenience embedding that provides no-op implementations of every ExtensionReader hook. Extension implementations embed it and override only the hooks they care about.
func (BaseExtensionReader) ReadBuildElement ¶
func (BaseExtensionReader) ReadBuildElement(*Build, *Element) error
func (BaseExtensionReader) ReadMeshElement ¶
func (BaseExtensionReader) ReadMeshElement(*Mesh, *Element) error
func (BaseExtensionReader) ReadObjectElement ¶
func (BaseExtensionReader) ReadObjectElement(*Object, *Element) error
func (BaseExtensionReader) ReadResourceElement ¶
func (BaseExtensionReader) ReadResourceElement(*Resources, *Element) error
type BaseExtensionWriter ¶
type BaseExtensionWriter struct{}
BaseExtensionWriter is the write-side analog of BaseExtensionReader.
func (BaseExtensionWriter) WriteBuildElements ¶
func (BaseExtensionWriter) WriteBuildElements(*Build, *Writer) error
func (BaseExtensionWriter) WriteMeshElements ¶
func (BaseExtensionWriter) WriteMeshElements(*Mesh, *Writer) error
func (BaseExtensionWriter) WriteObjectElements ¶
func (BaseExtensionWriter) WriteObjectElements(*Object, *Writer) error
func (BaseExtensionWriter) WriteResourceElements ¶
func (BaseExtensionWriter) WriteResourceElements(*Resources, *Writer) error
type BaseMaterial ¶
BaseMaterial is a single named material with an optional display color.
type BaseMaterials ¶
type BaseMaterials struct {
// contains filtered or unexported fields
}
BaseMaterials is a resource that groups one or more named, colored Base materials. Triangles and Objects reference a BaseMaterials by its id and an index into the slice returned by Materials.
func NewBaseMaterials ¶
func NewBaseMaterials(id uint32, mats ...BaseMaterial) *BaseMaterials
NewBaseMaterials constructs a BaseMaterials resource.
func (*BaseMaterials) Append ¶
func (b *BaseMaterials) Append(m BaseMaterial) uint32
Append appends a single material and returns its zero-based index.
func (*BaseMaterials) Materials ¶
func (b *BaseMaterials) Materials() []BaseMaterial
Materials returns the contained materials slice.
type Build ¶
Build represents the model's <build> element: the ordered list of items to be manufactured, plus an optional Production-extension UUID.
func (*Build) AppendItem ¶
AppendItem appends an item to the build list.
type BuildItem ¶
type BuildItem struct {
// ObjectID is the id of the referenced object in the model part named by
// Path (or in the current part when Path is empty).
ObjectID uint32
// Path, when non-empty, identifies the model part holding the referenced
// object (Production extension, p:path attribute).
Path string
// Transform is the affine transform applied to the referenced object
// during build. IdentityMatrix means no transform is serialized.
Transform Matrix
// PartNumber is an optional human-readable part identifier.
PartNumber string
// UUID is the Production-extension UUID for this build item.
UUID string
// Metadata is the optional list of metadata entries attached to this
// build item. The 3MF spec allows metadata on build items via the
// production extension.
Metadata []*Metadata
}
BuildItem is one entry in the model's build list. Each item references an Object resource (by id, or by id plus path when the Production extension is in use) and may apply an affine transform when the object is placed on the print bed.
func NewBuildItem ¶
NewBuildItem constructs a BuildItem from functional options.
type Color ¶
Color is an sRGB color with 8-bit channels and an alpha channel. A zero Color (R=G=B=A=0) is treated as "unspecified" so that 3MF's distinction between "no color" and "fully transparent black" can be preserved.
func NewColorRGBA ¶
NewColorRGBA returns a Color with the given channels marked as set.
func ParseColor ¶
ParseColor parses a 3MF color string of the form "#RRGGBB" or "#RRGGBBAA" (case-insensitive). An empty string returns an unset Color.
type Component ¶
type Component struct {
// ObjectID is the id attribute of the referenced object in the same
// model part. When Path is non-empty (Production extension) the reference
// is resolved against that part instead.
ObjectID uint32
// Path, when non-empty, identifies the model part that holds the
// referenced object (Production extension, p:path attribute).
Path string
// UUID is the Production-extension UUID of this component instance.
UUID string
// Transform is the affine transform applied to the referenced object.
// IdentityMatrix is treated as "no transform" and is not serialized.
Transform Matrix
}
Component is a reference to another Object that participates in the containing Object's geometry, optionally with an affine transform applied.
func NewComponent ¶
NewComponent constructs a Component from functional options.
type Element ¶
type Element struct {
// contains filtered or unexported fields
}
Element is the parsed XML subtree passed to an ExtensionReader hook.
func (*Element) Attr ¶
Attr returns the first attribute with the given local name, regardless of namespace.
func (*Element) Attributes ¶
func (e *Element) Attributes() []ElementAttribute
Attributes returns the element's attributes in source order.
func (*Element) ChildElements ¶
ChildElements iterates over direct child elements, optionally filtered by local name. An empty local name includes every element child.
func (*Element) NamespaceURI ¶
NamespaceURI returns the namespace URI, or an empty string for an unqualified element.
func (*Element) TextContent ¶
TextContent concatenates direct text children, including CDATA content.
type ElementAttribute ¶
ElementAttribute is an attribute on a parsed extension element.
type ExtensionReader ¶
type ExtensionReader interface {
// Namespace returns the URI of the namespace this reader handles.
Namespace() string
// ReadResourceElement is invoked for each direct child of <resources>
// whose namespace matches Namespace().
ReadResourceElement(res *Resources, elem *Element) error
// ReadObjectElement is invoked for each direct child of <object> whose
// namespace matches Namespace().
ReadObjectElement(obj *Object, elem *Element) error
// ReadMeshElement is invoked for each direct child of <mesh> whose
// namespace matches Namespace().
ReadMeshElement(mesh *Mesh, elem *Element) error
// ReadBuildElement is invoked for each direct child of <build> whose
// namespace matches Namespace().
ReadBuildElement(b *Build, elem *Element) error
}
ExtensionReader is implemented by extension sub-packages and registered via RegisterExtensionReader. The core 3MF reader invokes the appropriate hook when it encounters an element belonging to the extension's namespace at a position where the extension is allowed to contribute.
All hooks have a default no-op implementation; extensions need only implement the ones that apply to them. ExtensionReader is intentionally minimal — it covers the four contexts where 3MF extensions inject new elements: top-level <resources>, individual <object>, individual <mesh>, and the model's <build>.
func LookupExtensionReader ¶
func LookupExtensionReader(ns string) ExtensionReader
LookupExtensionReader returns the registered reader for ns, or nil.
type ExtensionWriter ¶
type ExtensionWriter interface {
// Namespace returns the URI of the namespace this writer handles.
Namespace() string
// WriteResourceElements is invoked once per model, after all core
// resource elements have been written but while the <resources> element
// is still open.
WriteResourceElements(res *Resources, w *Writer) error
// WriteObjectElements is invoked once per object, after the core
// <mesh>/<components> child has been written but while the <object>
// element is still open.
WriteObjectElements(obj *Object, w *Writer) error
// WriteMeshElements is invoked once per mesh, after the core
// <vertices>/<triangles> children have been written but while the
// <mesh> element is still open.
WriteMeshElements(mesh *Mesh, w *Writer) error
// WriteBuildElements is invoked once per build, after the core
// <item> children have been written but while the <build> element
// is still open.
WriteBuildElements(b *Build, w *Writer) error
}
ExtensionWriter is the symmetric write-side hook for an extension. Each method is called by the core writer at the corresponding position in the XML stream so that the extension may emit additional elements.
Implementations must use the supplied *Writer's StartElementNS/AttrNS helpers with the extension's pre-declared prefix; they must not close the elements opened by the core writer.
func LookupExtensionWriter ¶
func LookupExtensionWriter(ns string) ExtensionWriter
LookupExtensionWriter returns the registered writer for ns, or nil.
type Matrix ¶
type Matrix [12]float64
Matrix is a 4x3 affine transform stored in row-major order matching the 3MF wire format ("m00 m01 m02 m10 m11 m12 m20 m21 m22 m30 m31 m32"). The implicit fourth column is (0 0 0 1)^T.
func ParseMatrix ¶
ParseMatrix parses a 3MF transform string ("m00 m01 m02 m10 m11 m12 m20 m21 m22 m30 m31 m32", 12 whitespace-separated floats) and returns the corresponding Matrix. An empty string yields IdentityMatrix().
func (Matrix) IsIdentity ¶
IsIdentity reports whether m equals the identity transform within a tight tolerance.
type Mesh ¶
type Mesh struct {
// contains filtered or unexported fields
}
Mesh is the geometry attached to an Object.
func (*Mesh) AppendTriangle ¶
AppendTriangle appends a single triangle.
func (*Mesh) AppendVertex ¶
AppendVertex appends a single vertex and returns its zero-based index.
func (*Mesh) Extension ¶
Extension returns the parsed extension payload that was attached to the mesh for namespace ns, or nil if none was attached. The returned value is the extension-specific Go type — for instance, a *beamlattice.BeamLattice when ns is beamlattice.Namespace.
func (*Mesh) Extensions ¶
Extensions returns a copy of the extension namespaces present on the mesh.
func (*Mesh) SetExtension ¶
SetExtension attaches an extension payload for namespace ns to the mesh. Passing nil removes any existing payload for that namespace.
func (*Mesh) SetTriangles ¶
SetTriangles replaces the triangle list. The provided slice is retained without copying.
func (*Mesh) SetVertices ¶
SetVertices replaces the vertex list. The provided slice is retained without copying.
type Metadata ¶
Metadata is a key/value annotation attached to a Model, Object, or BuildItem. Names use the form "namespace:localname"; the 3MF specification reserves a fixed set of namespace-free names (Title, Designer, etc.) and allows arbitrary additional names when the namespace is declared on the owning element.
func NewMetadata ¶
NewMetadata constructs a Metadata from functional options.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the in-memory representation of a single 3MF model part (3dmodel.model). A Package may hold multiple Models when the Production extension is in use; the primary Model is reached via Package.Model().
func NewModel ¶
NewModel constructs a Model from functional options. The model's Resources and Build are always non-nil after construction so callers can append to them directly.
func (*Model) AppendMetadata ¶
AppendMetadata appends a metadata entry.
func (*Model) Extension ¶
Extension returns an arbitrary extension payload attached at the model level, or nil.
func (*Model) Extensions ¶
Extensions returns a copy of the attached extension payloads.
func (*Model) RequireExtension ¶
RequireExtension declares that namespace ns is required by this model and should be prefixed with prefix when serialized.
func (*Model) RequiredExtensions ¶
RequiredExtensions returns a copy of the map URI -> declared prefix.
func (*Model) SetExtension ¶
SetExtension attaches an extension payload. Passing nil removes it.
func (*Model) SetLanguage ¶
SetLanguage sets the xml:lang attribute.
func (*Model) SetThumbnail ¶
SetThumbnail sets the in-package thumbnail path.
type Object ¶
type Object struct {
// contains filtered or unexported fields
}
Object is a 3MF resource that holds either a mesh or a list of components (or, in the case of certain extensions, alternative geometry such as a beam lattice or implicit function reference).
func (*Object) AppendComponent ¶
AppendComponent appends a single component.
func (*Object) Components ¶
Components returns the components participating in this object. The returned slice is owned by the object.
func (*Object) Extension ¶
Extension returns an extension payload attached to this object, keyed by namespace URI.
func (*Object) Extensions ¶
Extensions returns a copy of the extension namespaces attached.
func (*Object) Mesh ¶
Mesh returns the mesh geometry attached to this object, or nil if the object is a components-only or extension-defined object.
func (*Object) PIndex ¶
PIndex returns the index into the property group named by PID. Only meaningful when PID is set.
func (*Object) PartNumber ¶
PartNumber returns the part-number attribute, or "".
func (*Object) SetComponents ¶
SetComponents replaces the component list.
func (*Object) SetExtension ¶
SetExtension attaches an extension payload. Passing nil removes it.
func (*Object) SetPartNumber ¶
SetPartNumber sets the part-number attribute.
func (*Object) SetThumbnail ¶
SetThumbnail sets the in-package thumbnail path.
func (*Object) Type ¶
func (o *Object) Type() ObjectType
Type returns the ObjectType (defaults to ObjectTypeModel).
type ObjectType ¶
type ObjectType int
ObjectType identifies the role of an Object inside a 3MF model.
const ( ObjectTypeModel ObjectType = iota ObjectTypeSolidSupport ObjectTypeSupport ObjectTypeSurface ObjectTypeOther )
func ParseObjectType ¶
func ParseObjectType(s string) (ObjectType, error)
ParseObjectType returns the ObjectType corresponding to s. An empty string returns ObjectTypeModel (the default per the spec). Unknown strings return an error.
func (ObjectType) String ¶
func (t ObjectType) String() string
type Option ¶
Option is the type passed to the New* constructors in this package and to the extension sub-packages. It is a type alias for github.com/lestrrat-go/option/v3.Interface.
func WithAttachment ¶
func WithAttachment(a Attachment) Option
WithAttachment attaches an extra OPC part.
func WithBaseMaterials ¶
func WithBaseMaterials(b *BaseMaterials) Option
WithBaseMaterials appends a BaseMaterials resource to a new Model.
func WithBuildItem ¶
WithBuildItem appends a BuildItem to a new Model.
func WithBuildObjectID ¶
WithBuildObjectID sets a BuildItem's ObjectID directly (useful when the target object lives in a different model part referenced by WithItemPath).
func WithComponent ¶
WithComponent appends a single Component to a new Object.
func WithComponentObjectRef ¶
WithComponentObjectRef sets the referenced Object on a Component. The component's ObjectID is taken from the object's id.
func WithComponentPath ¶
WithComponentPath sets the Production-extension path attribute on a Component.
func WithComponentTransform ¶
WithComponentTransform sets the affine transform on a Component.
func WithComponentUUID ¶
WithComponentUUID sets the Production-extension UUID on a Component.
func WithComponents ¶
WithComponents attaches a slice of Components to a new Object.
func WithExtension ¶
WithExtension attaches an extension payload identified by its namespace URI to the surrounding Mesh, Object, Build, or Model value. Extension packages (materials, beamlattice, etc.) typically wrap this option with a more strongly typed helper.
func WithItemMetadata ¶
WithItemMetadata appends a Metadata entry to a BuildItem.
func WithItemPartNumber ¶
WithItemPartNumber sets the optional part-number on a BuildItem.
func WithItemPath ¶
WithItemPath sets the Production-extension path attribute on a BuildItem.
func WithItemTransform ¶
WithItemTransform sets the affine transform on a BuildItem.
func WithItemUUID ¶
WithItemUUID sets the Production-extension UUID on a BuildItem.
func WithLanguage ¶
WithLanguage sets the xml:lang attribute on a new Model.
func WithMetadataName ¶
WithMetadataName sets the name attribute on a new Metadata.
func WithMetadataPreserve ¶
WithMetadataPreserve sets the preserve attribute on a new Metadata.
func WithMetadataType ¶
WithMetadataType sets the type attribute on a new Metadata.
func WithMetadataValue ¶
WithMetadataValue sets the text body of a new Metadata.
func WithModelMetadata ¶
WithModelMetadata appends a Metadata entry to a new Model.
func WithModelPath ¶
WithModelPath overrides the in-package path of the primary 3MF model part. Defaults to DefaultModelPath ("/3D/3dmodel.model").
func WithObject ¶
WithObject appends an Object resource to a new Model.
func WithObjectID ¶
WithObjectID sets the id attribute on a new Object.
func WithObjectName ¶
WithObjectName sets the human-readable name attribute on a new Object.
func WithObjectPID ¶
WithObjectPID sets the object-level property-group id.
func WithObjectPIndex ¶
WithObjectPIndex sets the object-level property index.
func WithObjectPartNumber ¶
WithObjectPartNumber sets the part-number attribute on a new Object.
func WithObjectRef ¶
WithObjectRef wires a BuildItem to an existing Object by reference; the item's ObjectID is taken from the object's id at construction time.
func WithObjectThumbnail ¶
WithObjectThumbnail sets the thumbnail path on a new Object.
func WithObjectType ¶
func WithObjectType(t ObjectType) Option
WithObjectType sets the type attribute on a new Object.
func WithObjectUUID ¶
WithObjectUUID sets the Production-extension UUID on a new Object.
func WithObjects ¶
WithObjects appends multiple Object resources to a new Model.
func WithPackageThumbnail ¶
WithPackageThumbnail attaches a package-level thumbnail.
func WithRequiredExtension ¶
WithRequiredExtension instructs the writer to declare a namespace prefix for the given extension namespace URI on the root model element and to list it in the requiredextensions attribute.
func WithResources ¶
WithResources replaces the Resources of a new Model.
func WithThumbnail ¶
WithThumbnail sets the in-package thumbnail path on a new Model.
func WithTriangles ¶
WithTriangles supplies a slice of triangles to NewMesh.
func WithVertices ¶
WithVertices supplies a slice of vertices to NewMesh.
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
Package is a 3MF package: an OPC container plus a primary in-memory Model. When the Production extension is in use, additional model parts are kept around in OPC.Parts so that AdditionalModel(path) can decode them on demand and writes can faithfully round-trip the package.
func NewPackage ¶
NewPackage constructs an empty Package with the given primary Model and optional package-level metadata (thumbnail, attachments, etc.).
func ReadPackage ¶
ReadPackage parses a 3MF package from r (which must implement io.ReaderAt because the ZIP format requires random access). size is the total size of the package in bytes.
func (*Package) AddAttachment ¶
func (p *Package) AddAttachment(a Attachment)
AddAttachment is a convenience that registers an attachment plus an optional package-level relationship.
func (*Package) AddPart ¶
AddPart writes raw bytes into the package at the given absolute part name, with the given content type.
func (*Package) AdditionalModel ¶
AdditionalModel decodes the secondary 3MF model part at the given absolute part name (Production extension). Results are cached.
func (*Package) OPC ¶
OPC returns the underlying OPC package. Modifying it directly is fine but callers should ensure consistency with the primary Model.
func (*Package) Part ¶
Part returns the raw bytes of the part at the given absolute name, or nil when no such part exists.
type Resources ¶
type Resources struct {
// contains filtered or unexported fields
}
Resources is the top-level resource list of a 3MF model part. It holds Objects, BaseMaterials, and extension-supplied resources (color groups, texture groups, slice stacks, etc.) accessed via Extension/SetExtension.
func (*Resources) AppendBaseMaterials ¶
func (r *Resources) AppendBaseMaterials(b *BaseMaterials)
AppendBaseMaterials appends a base-materials resource.
func (*Resources) AppendObject ¶
AppendObject appends an object resource.
func (*Resources) BaseMaterials ¶
func (r *Resources) BaseMaterials() []*BaseMaterials
BaseMaterials returns the BaseMaterials resources in this list.
func (*Resources) ExtensionResources ¶
ExtensionResources returns the extension-specific resource value for namespace ns, or nil.
func (*Resources) FindBaseMaterials ¶
func (r *Resources) FindBaseMaterials(id uint32) *BaseMaterials
FindBaseMaterials returns the BaseMaterials with the given id, or nil.
func (*Resources) FindObject ¶
FindObject returns the Object with the given id, or nil if none matches. O(n) — for tight loops, build a map.
func (*Resources) SetExtensionResources ¶
SetExtensionResources attaches an extension-specific resource value. Passing nil removes it.
type Thumbnail ¶
type Thumbnail struct {
Path string // typically "/Metadata/thumbnail.png"
ContentType string // typically ContentTypePNG
Data []byte
}
Thumbnail attaches a package-level thumbnail image (referenced from the package's _rels/.rels).
type Triangle ¶
type Triangle struct {
V1, V2, V3 uint32
// PID is the resource id of a property group (BaseMaterials, ColorGroup,
// etc.) that this triangle references. A value of zero means no override.
PID uint32
// P1, P2, P3 are per-vertex indices into the property group named by PID.
// When PID is zero these are ignored.
P1, P2, P3 uint32
// HasPID is true when an explicit PID was set on this triangle (so a
// zero PID can still be distinguished from the default).
HasPID bool
// HasPIndices is true when the per-vertex P1/P2/P3 indices were set.
// Some triangles set only PID, in which case all three vertices share
// the same P1 value.
HasPIndices bool
}
Triangle is a face of a mesh referencing three Vertices by index. The per-vertex property indices PIndices and the per-triangle property identifier PID encode 3MF material/property references; both default to zero, meaning "inherit from the object".
type Unit ¶
type Unit int
Unit identifies the unit of measure used by a 3MF model. The 3MF core specification fixes the set of legal values; any other string is rejected when parsing.
type Vertex ¶
type Vertex struct {
X, Y, Z float64
}
Vertex is a 3D coordinate in the model's local unit system.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is the streaming XML writer surface that the core serializer and the extension sub-packages share. It tracks namespace prefixes that the core writer has pre-declared on the root <model> element so that extension writers can emit prefix-qualified attributes and elements without triggering a duplicate xmlns declaration.
Extension implementations use the methods on this type to write elements, attributes, and text without accessing the internal XML encoder.
func NewWriter ¶
NewWriter wraps an io.Writer and returns a *Writer ready for use. Callers typically prefer the higher-level WriteModel function.
func (*Writer) AttrNS ¶
AttrNS writes an attribute whose qualified name is prefix:localName. The prefix is expected to have been declared via DeclarePrefix; this method does not emit any xmlns declaration.
func (*Writer) DeclarePrefix ¶
DeclarePrefix records that prefix is bound to uri on the root element. Subsequent calls to Attr / Element with this prefix will emit a prefix-qualified name without re-declaring the namespace. DeclarePrefix does not itself write any output — the caller must emit the xmlns:prefix attribute, typically via the helpers used while writing the root element.
func (*Writer) EndDocument ¶
EndDocument flushes the document and reports unclosed elements.
func (*Writer) EndElement ¶
EndElement closes the currently open element.
func (*Writer) PrefixFor ¶
PrefixFor returns the prefix declared for uri, or "" when none has been declared.
func (*Writer) StartDocument ¶
StartDocument writes the XML declaration.
func (*Writer) StartElement ¶
StartElement opens an element with the given local name (no namespace).
func (*Writer) StartElementNS ¶
StartElementNS opens an element with the given prefix and local name. The prefix must have been declared via DeclarePrefix.
func (*Writer) WriteString ¶
WriteString writes escaped text content.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package beamlattice implements the 3MF Beam Lattice Extension.
|
Package beamlattice implements the 3MF Beam Lattice Extension. |
|
internal
|
|
|
xmltree
Package xmltree provides the small XML tree used by 3MF extension readers.
|
Package xmltree provides the small XML tree used by 3MF extension readers. |
|
xmlutil
Package xmlutil contains small helpers shared by the core reader/writer and the extension sub-packages.
|
Package xmlutil contains small helpers shared by the core reader/writer and the extension sub-packages. |
|
Package materials implements the 3MF Materials and Properties Extension.
|
Package materials implements the 3MF Materials and Properties Extension. |
|
Package opc implements just enough of the Open Packaging Conventions (ECMA-376 Part 2) to support reading and writing 3MF (.3mf) packages.
|
Package opc implements just enough of the Open Packaging Conventions (ECMA-376 Part 2) to support reading and writing 3MF (.3mf) packages. |
|
Package production implements the 3MF Production Extension.
|
Package production implements the 3MF Production Extension. |
|
Package securecontent implements the metadata side of the 3MF Secure Content Extension.
|
Package securecontent implements the metadata side of the 3MF Secure Content Extension. |
|
Package slice implements the 3MF Slice Extension.
|
Package slice implements the 3MF Slice Extension. |
|
Package volumetric implements the 3MF Volumetric Extension.
|
Package volumetric implements the 3MF Volumetric Extension. |