Metadata-Version: 2.5
Name: gxw3call
Version: 0.1.1
Summary: Drive GX Works3 from Python through its Open Interface (GXW3Call.dll)
Project-URL: Homepage, https://github.com/Wadangzz/py-gxw3call
Project-URL: Issues, https://github.com/Wadangzz/py-gxw3call/issues
Author: Wadangzz
License-Expression: MIT
License-File: LICENSE
Keywords: automation,com,gx works3,melsec,mitsubishi,plc
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Manufacturing
Classifier: Operating System :: Microsoft :: Windows
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# gxw3call

Drive Mitsubishi Electric GX Works3 from Python through its Open Interface
(`GXW3Call.dll`): create projects, import hardware parameters and PLCopen
XML, rebuild, export, run the simulator and read or write devices, without
the GX Works3 window.

The interface is not documented by the vendor. Every one of its 302 methods
can be called here, and what is known about each one comes from real calls,
recorded in [docs/API.md](docs/API.md).

Not affiliated with or endorsed by Mitsubishi Electric. GX Works3 and MELSEC
are trademarks of Mitsubishi Electric Corporation.

## Requirements

- Windows with GX Works3 installed (tested with 1.123D). `GXW3Call.dll` comes
  with the standard installation.
- Python 3.10 or later, any bitness.
- A 32-bit Python 3.10+ for the worker process. The DLL is a 32-bit
  in-process COM server, so the package starts a 32-bit worker and talks to
  it over a local socket. The easiest way to get one:

  ```
  uv python install cpython-3.12-windows-x86
  ```

  It is found automatically; set `GXW3CALL_PYTHON32` to use another
  interpreter.

## Install

```
pip install gxw3call
```

or `uv add gxw3call`.

## Usage

### Build a project from parts

```python
from gxw3call.client import Gx

with Gx() as gx:
    project = gx.create_project(r'C:\work\new.gx3', 'R16')
    try:
        project.import_plc_files(r'C:\work\prm')  # $MELPRJ$\SYSTEM/CPU/UNIT.PRM
        project.import_xml(r'C:\work\ladder.xml')  # PLCopen XML
        errors, warnings = project.rebuild()
        print(project.hardware(), project.pou_names())
        project.save()
    finally:
        project.close()
```

- Order matters: create, PRM files, XML, rebuild. An XML imported before the
  PRM files arrives only in part.
- `import_plc_files` takes PRM bytes as stored inside a `.gx3`.
- CPU names are the ones GX uses (`R16`, `R32EN`, `FX5U`, `L04H`);
  `gx.plc_types()` lists them.
- [examples/build_from_parts.py](examples/build_from_parts.py) runs this flow
  and compares the instruction list with the source project.

### Any method

```python
from gxw3call.api import Api
from gxw3call.client import Gx

with Gx() as gx:
    api = Api(gx)
    out = api.project.open_project(r'C:\work\a.gx3', 180, 2, 0)
    call_id = out['call_id']
    try:
        print(api.project.get_plc_type_string(call_id)['plc_type_string'])
        print(api.project.get_pou_names(call_id, 0)['names']['value'])
    finally:
        api.project.close_project(call_id, 0)
```

Method and argument names are snake_case with the Hungarian prefix dropped
(`bstrProjectPath` becomes `project_path`, `lplReturnCode` becomes
`return_code`); every docstring lists the original names. A docstring that
starts with "Measured" records what real calls showed; "Not measured" means
only the signature is known. Struct arguments take a dict (decoded from a
previous call or built by hand) or a hex string.

## Things to know

- A return code of 0 does not prove success, and some calls work with a
  non-zero code. Read results back.
- `lUserInterfaceMode` 2 runs GX hidden. 0 and 1 show a window that stays
  open after the project is closed.
- Each opened project starts its own GX process (5 to 9 seconds). Close
  projects in `finally`; an open project keeps the process and the file lock.
- Struct sizes in the type library are not reliable: the native side uses
  pack 1, and some methods write more than the declared size.
- Paths must use backslashes; arguments ending in `Path` or `Folder` are
  normalised.

## Repository layout

| Path | Contents |
| --- | --- |
| `src/gxw3call/client.py` | `Gx` (worker connection) and `Project` (measured calls) |
| `src/gxw3call/api.py` | all 302 methods, generated by `tools/gen_api.py` |
| `src/gxw3call/worker.py` | 32-bit worker: loads the DLL, calls through the vtable |
| `src/gxw3call/typelib.py` | signatures, pack 1 struct encode and decode |
| `src/gxw3call/data/` | type library dump of GX Works3 1.123D |
| `docs/API.md` | reference: every method and struct with measured results |
| `notes/` | measured results (source of the docstrings and the reference) |
| `tools/` | probes, catalog dump, generators, type library dump script |

## Contributing measurements

Add what a call showed to `notes/measured.json`
(`{"Method": {"status": "works" | "fails" | "unclear", "note": "..."}}`),
then regenerate:

```
python tools/gen_api.py
python tools/gen_doc.py
```

`tools/probe_readonly.py` calls the read-only methods with default arguments;
`tools/probe_changes.py` calls methods on copies of a project and reports
which parts of the saved file changed. Run them only on copies. For another
GX Works3 version, dump the type library first:

```
powershell -File tools\tlb_dump.ps1 "C:\Program Files (x86)\MELSOFT\GPPW3\GXW3Call.dll" | Out-File -Encoding utf8 src\gxw3call\data\GXW3Call_tlb.txt
```

## License

MIT
