SVG ⇄ DWG Conversion Kit (mxcad-svg-dwg-kit)
Scope: bidirectional SVG ↔ DWG conversion. The native mxcadassembly / MxServiceCode does NOT support SVG (
outnameonly accepts.mxweb/.dwg/.dxf/.pdf/.jpg) — such tasks must use this kit. Prerequisite: an MxServiceCode project is needed — but it does NOT have to exist beforehand: when missing, the kit auto-downloads it per the "Program Acquisition" rules in the Backend MxServiceCode Guide (see section III), or you can pre-provision it with thesetupcommand.
Contents
- I. What This Kit Is
- II. Acquisition & Placement
- III. Invocation
- IV. Parameter Tables
- V. Output Contract & Middleware Location
- VI. Self-Check & Regression
- VII. Modifying the Conversion Logic (dev loop)
- VIII. Known Limitations
I. What This Kit Is
mxcad-svg-dwg-kit is a pure-Node bidirectional conversion toolkit built on MxServiceCode (sources maintained in TypeScript; delivered as the dependency-free single file dist/mxkit.js, executed by the project's built-in node):
- SVG→DWG: pure-JS vector extraction (middleware: SvgPlan JSON) → MxCAD parametric drawing; colors/opacity/line widths/dashes/hatch patterns/text alignment are WYSIWYG.
- DWG→SVG: open-and-export DXF + direct DWG binary cross-checking (phantom-entity removal) → dwgspec → SVG; layer visibility, default uniform line width 20 (370-lineweight info is kept in dwgspec; overridable with
line-width=), text recovery.
| Need | Which path |
|---|---|
| DWG/DXF ↔ mxweb / PDF / JPG | mxcadassembly directly (see Command Reference detailed 1) |
| SVG→DWG, DWG→SVG | This kit (dist/mxkit.js) |
| SVG→mxweb (web preview) | Chain: this kit svg2dwg → mxcadassembly DWG→mxweb |
| mxweb→SVG | Chain: mxcadassembly mxweb→DWG → this kit dwg2svg |
II. Acquisition & Placement
Download URL: https://demo.mxdraw3d.com/download/mxcad-svg-dwg-kit.zip (if unreachable, ask the user for the real address)
Fixed placement path (download once, reuse across sessions): Windows = %TEMP%\MxCAD\mxcad-svg-dwg-kit\, Linux = /tmp/MxCAD/mxcad-svg-dwg-kit/ (same level as the MxServiceCode package).
The zip extracts to a single top-level directory mxcad-svg-dwg-kit\; key contents:
mxcad-svg-dwg-kit\
├── dist\mxkit.js ← the only runtime file (dwg2svg / svg2dwg / selftest / setup / version in one)
├── bin\ ← launchers (mxkit.cmd / mxkit.sh: find node automatically, auto-download the backend service when missing; bootstrap-service.*)
├── run-test.bat / .sh ← double-click / CLI conversion entry (edit top-of-file parameters)
├── src\ ← TypeScript sources (the single maintenance source)
├── tests\ ← test.js (real two-direction conversion tests) + data\ fixtures
├── data\ ← font metrics table
├── tools\ scripts\ ← build & gate (used by npm run build; scripts\ only contains node10-gate.js)
└── package.json / README.md / 使用指南.txt (end-user guide, Chinese)The package ships full sources and build tools: after editing src\, run npm install && npm run build inside the kit directory to re-produce dist\mxkit.js.
Acquisition works like the backend package: inspect the fixed path → download & extract if missing; clear that subdirectory before any re-download. Do not scan other directories on the user's machine.
III. Invocation
The kit needs the MxServiceCode project (signature: MxFun.js + bin\node.exe at its root), located in this order: cwd → env var MXCAD_PROJECT → conventional candidates such as %TEMP%\MxCAD\MxServiceCode_TryVersion\MxServiceCode\MxServiceCode. If none is found → the kit auto-downloads the backend package per the mxcad-skill "Program Acquisition" rules (clear %TEMP%\MxCAD → download/extract per platform → trial-validity check), then continues the conversion. Download progress goes to stderr (stdout still holds exactly one JSON envelope). Pass no-setup=1 to disable auto-download (then missing → code:1); use the setup method to pre-provision separately.
Method A — launcher (recommended: any cwd, provisions the runtime automatically):
:: Windows
<kit dir>\bin\mxkit.cmd dwg2svg file=<input.dwg> outfile=<output.svg>
<kit dir>\bin\mxkit.cmd svg2dwg file=<input.svg> outfile=<output.dwg>
<kit dir>\bin\mxkit.cmd selftest :: setup / version follow the same form# Linux
sh <kit dir>/bin/mxkit.sh dwg2svg file=<input.dwg> outfile=<output.svg>The launcher resolves in order: node on PATH → the node bundled with an existing MxServiceCode service (temp dir or MXCAD_PROJECT) → auto-download the MxServiceCode backend package (ships its own node; ≈94MB Windows / ≈163MB Linux, first run only). For end users there are also run-test.bat/run-test.sh (edit the top parameters) and 使用指南.txt (usage guide).
Method B — direct invocation (cwd = MxServiceCode project directory, run with the project's built-in node):
:: Windows (cmd; same form in Git Bash, use bash-style paths)
cd /d <MxServiceCode project dir>
bin\node.exe %TEMP%\MxCAD\mxcad-svg-dwg-kit\dist\mxkit.js dwg2svg file=<input.dwg> outfile=<output.svg>
bin\node.exe %TEMP%\MxCAD\mxcad-svg-dwg-kit\dist\mxkit.js svg2dwg file=<input.svg> outfile=<output.dwg>
bin\node.exe %TEMP%\MxCAD\mxcad-svg-dwg-kit\dist\mxkit.js selftest
bin\node.exe %TEMP%\MxCAD\mxcad-svg-dwg-kit\dist\mxkit.js setup
bin\node.exe %TEMP%\MxCAD\mxcad-svg-dwg-kit\dist\mxkit.js version# Linux (the built-in runtime is ./node)
cd <MxServiceCode project dir>
./node /tmp/MxCAD/mxcad-svg-dwg-kit/dist/mxkit.js dwg2svg file=<input.dwg> outfile=<output.svg>The system node (≥11) can also run node dist\mxkit.js ... directly: the entry auto-relaunches itself under the project's bin\node.exe (the SDK native plugin only supports the node10 ABI), transparent to the caller.
Options are bare key=value (no --); prefer absolute file paths (file=/outfile= are resolved first, so they are not subject to the "relative to the project dir" concatenation limits). Write outputs where the user asked — never leave them in the temp directory.
IV. Parameter Tables
4.1 svg2dwg
| Parameter | Default | Description |
|---|---|---|
file= | required | input SVG |
outfile= | required | output DWG |
scale= | 1 | SVG px → DWG unit ratio |
offset-x= / offset-y= | 0 | translation |
samples= | 32 | curve sampling points (≥4) |
height-scale= | 0.8 | text height factor |
font-ratio= / font-metric= / descender= | 1 / 0.9 / 0.2 | text width / baseline fine-tuning |
no-flip=1 | — | disable Y flip (default: SVG y-down → DWG y-up) |
metrics= | built-in | override the font metrics table |
json= | — | pass a path to keep the intermediate SvgPlan JSON (debugging) |
4.2 dwg2svg
| Parameter | Default | Description |
|---|---|---|
file= | required | input DWG |
outfile= | required | output SVG |
scale= / offset-x= / offset-y= | 1 / 0 / 0 | output coordinate transform |
pad= | 8 | viewport padding |
bg= | auto | background (auto = from the DWG background / roundtrip-tagged original) |
line-width= | 20 | uniform line width (drawing units): default 20, explicit value overrides; entity's own polyline widths still take priority |
no-flip=1 | — | disable Y flip |
dxf= / dump= | — | debug pass-through: ready-made DXF / cross-check dump (inputs) |
spec= | — | file path to write the intermediate dwgspec JSON (output, debugging) |
V. Output Contract & Middleware Location
- stdout contains exactly one line, a JSON envelope
{code, message, ...};code:0means success; the process exit code is 0/1. svg2dwgsuccess:{code:0, outfile, entities, hatches[, json]}dwg2svgsuccess:{code:0, outfile, entities, rendered, dxfile, spec, dump}(the last three are middleware paths for troubleshooting)- All middleware (DXF / dwgspec / dump / SvgPlan / the single-file unpacked body) stays under
<TMP>/mxcad-svg-dwg-kit/; the success envelope returns the exact paths — read those files when troubleshooting.
VI. Self-Check & Regression
selftest: one command runs the full DWG→SVG→DWG→SVG chain with 9 cross-checks (non-empty render / shapes / layers / lineweight chain / text recovery / extraction / drawing / type-family ratios / text character coverage). With no arguments it uses the embedded sample1.dwg; all passing →code:0. Custom sample:selftest sample=<file.dwg>.- With a system node (≥14) available, run the dev-level tests inside the kit dir:
npm install(dev deps only) →npm test(ensures the SDK is ready, then runs the real two-direction conversion teststests/test.js) →npm run selftest.
VII. Modifying the Conversion Logic (dev loop)
- Only modify
src/(TypeScript);dist/mxkit.jsis a build artifact — never hand-edit it. npm run build=tsc+ Node10 runtime gate (APIs/syntax newer than Node10 — rmSync,??,?., etc. — are banned and every file is actually parsed by the project's node10; no pack if the gate fails) + single-file packaging.- Verify:
npm test(real conversions) →npm run selftest(full-chain cross-checks) → re-run the business commands withdist\mxkit.js. npm run cleandeletesdist/too (dist is the deliverable; after deleting you must re-build).
VIII. Known Limitations
- The runtime only executes on the MxServiceCode built-in node (v10, CommonJS only); the system node only bootstraps.
- SVG→DWG unifies line/arc/circle into polylines with bulges (roundtrip checks normalize by "type family").
- MTEXT inside DWG gets re-segmented on roundtrip (character content is lossless; the segmentation changes).
- Roundtrip metadata (XDATA SVGRT tags: original colors/font sizes/background...) is best-effort: whether it survives DXF export depends on the SDK version;
dwgspec.meta.rt_moderecords whether it was read. - The trial MxServiceCode has a license expiry and an open-count limit (see "Deployment and Startup Notes").
