Skip to main content

Module swpart

Module swpart 

Source
Expand description

SOLIDWORKS files from version 2015 on (.sldprt, .sldasm, .slddrw): SOLIDWORKS’ own section container.

Up to SOLIDWORKS 2014 the files were OLE compound files; newer ones are a sequence of Deflate-compressed sections, similar to the local entries of a ZIP file. The format is not documented. This reader only relies on what was verified with the NIST test files in tests/fixtures/cad/nist (SOLIDWORKS 2018):

section  14 00 06 00 08 00   marker (ZIP: version 2.0, flags 6, method 8 = Deflate)
         u32 LE              DOS time and date
         u32 LE              CRC-32 of the uncompressed data
         u32 LE              compressed size
         u32 LE              uncompressed size
         u32 LE              name length
         name                each byte with its two nibbles swapped ("Contents/CMgr")
         data                raw Deflate (no zlib header), compressed size bytes

Every section is preceded by the same four bytes (a per-file signature). The file also holds records that look like section headers but carry no data (often their “CRC” is four times, their “size” twice the “compressed size”; they are not preceded by the signature) and a directory at the end whose names are prefixed with binary fields. Neither is needed: a section counts only if it follows the signature (once that is known), its name is printable, its data lies inside the file and – when it is read – it inflates to exactly the stored size with the stored CRC-32.

Sections used by TeamCraft:

  • PreviewPNG: the preview image (PNG, 640 × 480 in the test files);
  • swXmlContents/COMPINSTANCETREE (assemblies): XML with the component files (swFile swPath="…") and their instances (swReference);
  • SwDocContentMgr/SwDocContentMgrInfo: the document manager’s record of the referenced documents with their full paths – used for files without the component tree (drawings; not verified with a real drawing).

Hostile input: offsets and sizes are never trusted. Sections are found by a bounded search (MAX_SEARCH bytes looked at byte by byte, at most MAX_SECTIONS sections, the file read in chunks that are each read about once), the data of a section after the signature is skipped, and inflating stops one byte after the stored size; sections larger than the caller’s limit are not inflated at all (decompression bombs end there), and at most MAX_TRIES sections of one name are inflated.

Structs§

ReferenceData
What a SOLIDWORKS 2015+ file says about the files it references.
Section
A section of the container: where its compressed data lies in the file.
SwContainer
A SOLIDWORKS 2015+ file opened from a file (or any seekable reader).

Constants§

COMPONENT_TREE_SECTION
Section with the component tree of an assembly (XML).
CONTENT_MANAGER_SECTION
Section with the referenced documents as recorded by the document manager.
MAX_SEARCH
Most bytes looked at one by one while searching for section headers (the data of recognised sections is skipped, not searched).
MAX_SECTIONS
Most sections read from the directory of one file.
MAX_TRIES
Most sections of the same name inflated by one SwContainer::read (the NIST files have every name once).
PREVIEW_SECTION
Section with the preview image (PNG).

Functions§

component_counts
The instance counts of the direct components of an assembly: every file of components with the number of its instances that count for a BOM (those without swExcludeFromBOM="YES"; a file with only excluded instances has the count 0).
components
The files of the direct components of an assembly, from its component tree (swXmlContents/COMPINSTANCETREE), each once in the order of the tree:
file_component_counts
The instance counts (component_counts) of a SOLIDWORKS 2015+ file, or None when it is no such file or has no component tree.
inflate
Inflates raw Deflate data of a section: Some only if it yields exactly size bytes with the CRC-32 crc. Never produces more than size + 1 bytes, whatever the data claims.
is_container
True if the first bytes of a file start like a SOLIDWORKS 2015+ file: a plausible section header within the first FIRST_WITHIN bytes (the first 600 bytes of a file are enough).
placements
The parts of an assembly with their placements, from its component tree (swXmlContents/COMPINSTANCETREE), sub-assemblies resolved:
reference_data
The reference sections of a SOLIDWORKS 2015+ file (each at most max bytes uncompressed), or None if the file is not one.