solidworks_mcp.adapters.solidworks.documentation¶
solidworks_mcp.adapters.solidworks.documentation ¶
Model-data mixin for PyWin32 SolidWorks operations.
Covers the engineering data a drawing or BOM is built from: model dimensions, measurements, material, custom properties and sheet-metal flat patterns.
Every method name, argument order and enum value below was read from the
SOLIDWORKS 2025 type libraries (sldworks.tlb via gen_py, swconst.tlb
via pythoncom.LoadTypeLib), not from memory. Live behaviour is covered by
tests/live/test_live_sw_documentation_tools.py.
Attributes¶
_MEASURE_PREFIXES
module-attribute
¶
_SW_INSERT_DIMENSIONS_MARKED_FOR_DRAWING
module-attribute
¶
_SW_INSERT_HOLE_WIZARD_LOCATION_DIMS
module-attribute
¶
Classes¶
AdapterResult
dataclass
¶
AdapterResult(status: AdapterResultStatus, data: T | None = None, error: str | None = None, execution_time: float | None = None, metadata: dict[str, Any] | None = None)
Result wrapper for adapter operations.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
T | None
|
The data value. |
error |
str | None
|
The error value. |
execution_time |
float | None
|
The execution time value. |
metadata |
dict[str, Any] | None
|
The metadata value. |
status |
AdapterResultStatus
|
The status value. |
AdapterResultStatus ¶
Bases: StrEnum
Result status for adapter operations.
Attributes:
| Name | Type | Description |
|---|---|---|
ERROR |
Any
|
The error value. |
SUCCESS |
Any
|
The success value. |
TIMEOUT |
Any
|
The timeout value. |
WARNING |
Any
|
The warning value. |
SolidWorksDocumentationMixin ¶
Expose model-data and drawing-documentation methods through a mixin.
Methods:¶
arrange_dimensions
async
¶
Move drawing dimensions so none overlaps another, then verify.
Reads every dimension's text box and lines, plans positions outside
the views with :func:dimension_layout.plan_layout, applies them with
IAnnotation::SetPosition, rebuilds, then re-reads the real
geometry and checks the rule with :func:dimension_layout.find_overlaps.
The check covers every view on the sheet, even when only one view is
rearranged, because neighbouring views' dimensions can collide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view_name
|
str | None
|
Only move this view's dimensions; all views when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: |
AdapterResult[dict[str, Any]]
|
(each violation listed), |
AdapterResult[dict[str, Any]]
|
overlap remains - the rule is not met - with the same data, so the |
AdapterResult[dict[str, Any]]
|
caller can see which dimensions still collide. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
export_flat_pattern
async
¶
Export the active sheet-metal part's flat pattern to DXF or DWG.
Wraps IPartDoc::ExportToDWG2 with swExportToDWG_ExportSheetMetal.
SheetMetalOptions 1 exports the flat-pattern geometry only (no bend
lines or sketches).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Output |
required |
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: The written path and its size. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
get_custom_properties
async
¶
Read the custom properties of the active document.
Wraps IModelDocExtension::CustomPropertyManager(config) with
GetNames + Get6. "" reads the file-level properties a
drawing title block usually links to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configuration
|
str | None
|
Configuration name; file-level when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: |
AdapterResult[dict[str, Any]]
|
|
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
get_material
async
¶
Read the material assigned to the active part.
Wraps IPartDoc::GetMaterialPropertyName2; see
:func:_read_material for its Database out-parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
configuration
|
str | None
|
Configuration name; the active one when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: |
AdapterResult[dict[str, Any]]
|
|
AdapterResult[dict[str, Any]]
|
in its |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
insert_model_dimensions
async
¶
insert_model_dimensions(view_name: str | None = None, include_hole_wizard: bool = True) -> AdapterResult[dict[str, Any]]
Bring model dimensions into drawing views, each dimension shown once.
Imports the model's "marked for drawing" dimensions view by view with
IDrawingDoc::InsertModelAnnotations3 (UsePlacementInSketch off,
so SolidWorks places them rather than copying sketch positions).
Drafting rules applied:
- pictorial views (isometric, dimetric, trimetric) are not dimensioned unless named explicitly;
- a model dimension already shown in an earlier view is deleted from later views, so each dimension appears exactly once on the sheet.
Falls back to IDrawingDoc::AutoDimension only when nothing at all
could be imported. Placement is then made overlap-free by
:meth:arrange_dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view_name
|
str | None
|
One view to dimension; every orthographic view when omitted. |
None
|
include_hole_wizard
|
bool
|
Also import Hole Wizard location dimensions. |
True
|
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: Per-view counts, duplicates removed, |
AdapterResult[dict[str, Any]]
|
skipped pictorial views and the |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
list_available_materials
async
¶
List materials from the configured SolidWorks material libraries.
Reads the custom library folders from
swFileLocationsMaterialDatabases (a ;-separated list) plus the
install's built-in sldmaterials folder, and parses every
.sldmat file in them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
search
|
str | None
|
Case-insensitive substring filter on name or classification. |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[list[dict[str, str]]]
|
AdapterResult[list[dict[str, str]]]: |
AdapterResult[list[dict[str, str]]]
|
and |
AdapterResult[list[dict[str, str]]]
|
read and the |
AdapterResult[list[dict[str, str]]]
|
told apart from an unexpected library location. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
list_model_dimensions
async
¶
List every dimension in the active part or assembly.
Walks IModelDoc2::FirstFeature / IFeature::GetNextFeature and
each feature's GetFirstDisplayDimension chain. full_name (e.g.
"D1@Sketch1") is the name set_dimension accepts, so the pair
gives edit-by-name access to every driving parameter in the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_name
|
str | None
|
Only list dimensions of this feature (case-insensitive). |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[list[dict[str, Any]]]
|
AdapterResult[list[dict[str, Any]]]: |
AdapterResult[list[dict[str, Any]]]
|
|
AdapterResult[list[dict[str, Any]]]
|
and |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
measure_entities
async
¶
Measure one or more entities with SolidWorks' Measure tool.
Selects each entity by spec (see :func:_parse_measure_spec), then runs
IModelDocExtension::CreateMeasure + IMeasure::Calculate(None),
which measures the current selection. Values SolidWorks reports as
-1 (not applicable to the selection) are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[str]
|
One or two entity specs, e.g. |
required |
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: Measured values in metres, square |
AdapterResult[dict[str, Any]]
|
metres and radians, plus |
AdapterResult[dict[str, Any]]
|
two entities. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
set_custom_property
async
¶
set_custom_property(name: str, value: str, configuration: str | None = None) -> AdapterResult[dict[str, Any]]
Create or overwrite one text custom property on the active document.
Wraps ICustomPropertyManager::Add3(name, swCustomInfoText, value,
swCustomPropertyReplaceValue) and reads the value back.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Property name, e.g. |
required |
value
|
str
|
Text value; may be a |
required |
configuration
|
str | None
|
Configuration name; file-level when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: The stored and resolved value. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
set_material
async
¶
set_material(material_name: str, database: str | None = None, configuration: str | None = None) -> AdapterResult[dict[str, Any]]
Assign a material to the active part.
Wraps IPartDoc::SetMaterialPropertyName2 (a void call), then
reads the material back - the read-back is the only proof it applied.
A name the library does not hold leaves the part unchanged and is
reported as not found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
material_name
|
str
|
Material name exactly as in the library, e.g.
|
required |
database
|
str | None
|
Library name; |
None
|
configuration
|
str | None
|
Configuration name; |
None
|
Returns:
| Type | Description |
|---|---|
AdapterResult[dict[str, Any]]
|
AdapterResult[dict[str, Any]]: Requested and applied material. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
Functions:¶
_as_com ¶
Wrap a raw dispatch and flag its methods for an interface.
Objects handed back inside arrays (GetViews and friends) arrive as raw
PyIDispatch. Method flagging is a no-op on those, so every call
against them raises until they are wrapped through dynamic.Dispatch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
Any
|
A connected |
required |
obj
|
Any
|
The raw dispatch. |
required |
interface
|
str
|
Interface name, e.g. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The wrapped, flagged object, or |
Source code in src/solidworks_mcp/adapters/solidworks/io.py
_byref_bstr ¶
Return a byref string VARIANT for a SolidWorks String out-parameter.
Under forced late binding an omitted out-parameter raises "Parameter not
optional", and pythoncom.Missing cannot be converted to a COM VARIANT,
so pass VARIANT(VT_BYREF | VT_BSTR, "") and read .value afterwards
(e.g. IPartDoc::GetMaterialPropertyName2's Database; confirmed live
on R2026x).
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The byref VARIANT, or a |
Source code in src/solidworks_mcp/adapters/solidworks/io.py
_doc_type ¶
Return the active document's type: 1 part, 2 assembly, 3 drawing.
GetType is one of the members pywin32 late binding may expose as either
a bound method or a plain value, so calling it directly returns None on
some documents. _get_attr_or_call handles both shapes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
Any
|
A connected |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
int | None: The document type, or |
Source code in src/solidworks_mcp/adapters/solidworks/io.py
_library_files ¶
The .sldmat files in folders, folder by folder, sorted by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folders
|
list[str]
|
Material library folders; missing ones are skipped. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Full paths. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_material_density ¶
A material's mass density in kg/m³, from its .sldmat entry.
The library whose file name matches database is searched first, then
the others. The value is the DENS element's value, which the
libraries store in kg/m³.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
list[str]
|
|
required |
name
|
str
|
The material's name, as |
required |
database
|
str
|
Its library's name, the file name without extension. |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
float | None: The density, or |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_null_callout ¶
Return a VT_DISPATCH null for SelectByID2's Callout parameter.
A plain Python None marshals as VT_NULL, which SolidWorks rejects
with DISP_E_TYPEMISMATCH - measured as
(-2147352571, 'Type mismatch.', None, 8). See runbook item 11.
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
A |
Any
|
when pywin32 is unavailable (mock/Linux runs, where no real COM call |
|
Any
|
will be made anyway, but the value must still never be bare |
Source code in src/solidworks_mcp/adapters/solidworks/features.py
_overlap_summary ¶
Describe the overlaps arrange_dimensions could not clear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
dict[str, Any]
|
The layout result, carrying |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
How many overlaps remain, their kinds, the first five, and what |
str
|
to try next. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_parse_edge_spec ¶
Parse an edge/face specification string into (name, x, y, z, entity_type).
Supports three formats:
- "Edge<1>" — name-based EDGE selection (x=y=z=0.0, SW looks up by
topology name)
- "x,y,z" — coordinate-based EDGE selection (name="", coordinate hint
in metres)
- "face:x,y,z" (case-insensitive prefix) — coordinate-based FACE
selection. Selecting a whole face and handing it to
InsertFeatureChamfer/FeatureFillet3 chamfers/fillets every
edge bounding that face in one call. It is the robust way to reach a
hole's rim: the rim is one edge of the face the hole opens onto, and
a point well inside that face is clear of every boundary, whereas an
edge point has to land on the rim itself.
Every coordinate pick must lie within _PICK_TOLERANCE_M of the edge
or face it resolves to. (A 2026-09-18 note here said an edge pick aimed
at a hole rim landed on a 30 mm outer edge; the test's point was on that
outer edge, because it assumed the wrong height axis. See #199.)
Source code in src/solidworks_mcp/adapters/solidworks/features.py
_parse_material_library ¶
Read material names and classifications from one .sldmat file.
.sldmat is XML: <classification name=...> elements hold
<material name=...> children. Tags are matched by local name because
the root element carries a namespace. Parsed with defusedxml: library
folders can be user-configured, so entity-expansion and external-entity
payloads are refused rather than expanded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
list[dict[str, str]]: |
list[dict[str, str]]
|
for each material; empty when the file cannot be parsed or is refused. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_parse_measure_spec ¶
Parse a measure_entities entity spec into SelectByID2 arguments.
Accepts the same "face:x,y,z" / "x,y,z" / "Edge<1>" forms as
add_fillet (via :func:_parse_edge_spec), plus "edge:x,y,z" and
"vertex:x,y,z". Coordinates are in metres.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
str
|
The entity spec. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, float, float, float, str]
|
tuple[str, float, float, float, str]: name, x, y, z and entity type. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_read_custom_property ¶
Read one custom property's (value, resolved value).
Tries Get6 (value and resolved value) first; under late binding on
SW 2025 its out-parameters raise Parameter not optional, so it falls
back to Get(name), which returns the stored text only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
Any
|
The adapter, for its |
required |
manager
|
Any
|
The document's |
required |
name
|
str
|
Property name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
tuple[str, str]: The stored value and the resolved value; empty |
str
|
strings when the property cannot be read. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_read_material ¶
Read (material, database) from IPartDoc::GetMaterialPropertyName2.
Database is a String out-parameter: under late binding on SW 2026
omitting it raises "Parameter not optional", so it is passed as a byref
BSTR and read back from .value. Older pywin32 builds may instead return
(name, database) as a tuple; both shapes are accepted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
part
|
Any
|
The active |
required |
configuration
|
str | None
|
Configuration name; the active one when |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
tuple[str, str]: Material name and library; empty strings when unset. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_resolve_drawing_view ¶
Return the IView on the active drawing whose name matches.
Walks IDrawingDoc::GetViews (a list of sheets, each shaped
(sheet, view, view, ...)) the same way :func:_view_names does. The
match is case-insensitive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
Any
|
A connected |
required |
drawing
|
Any
|
The drawing document, flagged for |
required |
view_name
|
str
|
The drawing-view name to look for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The flagged |
Source code in src/solidworks_mcp/adapters/solidworks/io.py
_select_edge_by_coord ¶
_select_edge_by_coord(adapter: Any, x: float, y: float, z: float, append: bool, mark: int = 0, entity_type: str = 'EDGE', picker: _BodyEntityPicker | None = None) -> bool
Select the edge (or face) nearest to (x, y, z) in metres.
Calls ForceRebuild3 on the first entity in the selection set so that
recent features are fully tessellated and selectable, then resolves the
point with :class:_BodyEntityPicker - the entity whose closest point is
nearest, independent of the view. Only when no body edge/face can be read
at all does it fall back to the view-dependent SelectByID2 pick.
entity_type is normally "EDGE", but pass "FACE" to select a
whole face instead. Any other type ("VERTEX" from
measure_entities) goes straight to the SelectByID2 pick: a vertex
lies on its edges at distance 0, so the edge walk would select an edge
in its place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
Any
|
A connected adapter with a valid |
required |
x
|
float
|
X in metres. |
required |
y
|
float
|
Y in metres. |
required |
z
|
float
|
Z in metres. |
required |
append
|
bool
|
Add to the current selection instead of replacing it. |
required |
mark
|
int
|
Selection mark. |
0
|
entity_type
|
str
|
|
'EDGE'
|
picker
|
_BodyEntityPicker | None
|
Reuse a picker (and its body walk) across several picks. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if an entity was selected; False otherwise. |
Raises:
| Type | Description |
|---|---|
LookupError
|
When the nearest entity is further than
|
Source code in src/solidworks_mcp/adapters/solidworks/features.py
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 | |
_sequence ¶
Normalise a COM array return (None, scalar or tuple) into a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
What the COM call returned. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
list[Any]: The non- |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_strip_document_suffix ¶
Turn "D1@Sketch1@Part10.Part" into "D1@Sketch1".
Live SW 2025 appends @<document title> to IDimension::FullName;
IModelDoc2::Parameter (behind set_dimension) takes the short form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
full_name
|
Any
|
|
required |
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The name without the document suffix; |
str | None
|
|
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_unpack_custom_property ¶
Pull (value, resolved value) out of an ICustomPropertyManager::Get6 result.
Under late binding the out-parameters come back as a tuple that may or may not lead with the integer return code, depending on the pywin32 build.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Any
|
Whatever |
required |
Returns:
| Type | Description |
|---|---|
str
|
tuple[str, str]: The raw (possibly |
str
|
resolved value; empty strings when the shape is unrecognised. |
Source code in src/solidworks_mcp/adapters/solidworks/documentation.py
_view_names ¶
Return the drawing's view names, excluding sheet formats.
CreateDrawViewFromModelView3 returns None for a model SolidWorks
could not resolve, and Create3rdAngleViews2 returns a bare boolean, so
the view list is the ground truth for whether views were really added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
Any
|
A connected |
required |
drawing
|
Any
|
The drawing document, flagged for |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: View names in sheet order. |