Skip to content

[Bug]: SchDocReader treats template-owned primitives as top-level document primitives #44

Description

@L-proger

Description

Hi! I found an issue while rendering a .SchDoc with an applied sheet template.

Some graphical primitives that belong to a SchTemplate record are currently added to the document as normal top-level primitives. As a result, the renderer draws template-owned graphics as real schematic content. In my case this produced an extra Altium title block/frame rendered over the schematic, while the original Altium viewer does not show it there.

The root cause seems to be in SchDocReader: record type 39 is parsed as SchTemplate, but primitives whose OWNERINDEX points to that template record are not associated with the template. They fall through into document.AddPrimitive(primitive).

A minimal fix that worked locally:

  • Track template records by their primitive index in SchDocReader.
  • If a primitive has OWNERINDEX pointing to a known SchTemplate, attach it to that template instead of adding it to the document top level.
  • Include template-owned primitives in the model primitive count if round-trip/write fidelity depends on that count.

Pseudo-diff:

var templates = new Dictionary<int, SchTemplate>();

// while reading primitives
if (primitive is SchTemplate template)
    templates[primitiveIndex] = template;

// while assigning ownership
else if (ownerIndex >= 0 && templates.TryGetValue(ownerIndex, out var ownerTemplate))
{
    ownerTemplate.AddPrimitive(primitive);
}

And SchTemplate can keep these separately:

private readonly List<object> _primitives = new();
public IReadOnlyList<object> Primitives => _primitives;
public void AddPrimitive(object primitive) => _primitives.Add(primitive);

This fixed the incorrect extra title block rendering without disabling the normal sheet frame rendering.

Steps to Reproduce

  1. Open a .SchDoc file that has an applied sheet template containing its own title block/frame graphics.
  2. Parse it with SchDocReader.
  3. Render the parsed SchDocument with the SVG renderer.
  4. Compare the result with the same document opened in Altium Designer or another reference viewer.

Expected Behavior

  • SchTemplate should retain primitives owned by the template record, or those primitives should otherwise be excluded from normal document-level rendering.
  • Primitives with OWNERINDEX pointing to a template should not be treated as regular top-level schematic objects.

Observed behavior:

  • Template-owned lines/text/frames are rendered as normal sheet primitives.
  • This can cause duplicate or misplaced title blocks / sheet frames.

Altium File Type

.SchDoc (Schematic Document)

Package Version

2.0.0

Operating System

Windows

Minimal Reproduction Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions