Skip to content

MxServiceCode CLI Command Reference ​

Compiled from the project source (src/MxCommand.ts, src/sample/*.ts). MxServiceCode is a Node.js command-line project; the entry point is mxapp.js, and the process exits after each command (no daemon).

I. Invocation Format ​

<node executable> mxapp.js <command> param1=value1 param2=value2 [debug=1]
  • Built-in runtime: Windows bin\node.exe, Linux ./node (located in the project directory)
  • On Linux you may add linux=1 to force Linux-mode execution
  • Output: one line of JSON on stdout; on success code or ret is 0

II. Command Overview (12 commands) ​

CommandFunction
convert⚠️ Unusable (wgh format deprecated; see detailed 1); use mxcadassembly directly for DWG→mxweb
getcaddataExtract text/block attribute data from a drawing to JSON
getPolylineTheSpecifiedLayerExtract vertex data of all polylines on a specified layer to JSON
testParamDrawParametric drawing: lines/dashed lines/blocks/title block/text/hatch
testParamDrawTkParametric drawing: insert title block frame (A3) + title block (BT)
compare_dwgCompare whether the block table contents of two DWG files match
frameCutBatch cutting by title block frame blocks (⚠️ depends on the 1337 service, not provided in the standalone edition; use frameCut2)
frameCut2Batch cutting by title block frame blocks (calls mxcadassembly directly; recommended)
getAllLayerGet all layer information of a drawing as JSON
margeDwgMerge multiple DWGs into a target DWG (layer filtering supported)
mxtest_getxdataDebug example: read extended data, geometry calculations, intersection (hardcoded paths; do not use in production)
Mx_Custom_ExportPdfExport the whole drawing to PDF (auto-scaled to sheet size, includes title block frame location info)

There is also a generic entry fileparam (JSON file parameter passing; see section 5).

III. Command Details ​

1. convert — DWG to frontend display file (unusable) ​

bin\node.exe mxapp.js convert file=t1.dwg
  • Parameter: file source DWG path (relative paths are relative to the project directory)

⚠️ Tested: unusable: returns {"code":-4,"message":"js program exception, see log for details"}; the log shows TypeError: this.imp.writeWebGLFile is not a function. Reason: the wgh format output by writeWebGLFile is deprecated and the new native module has removed that function; the mxcad frontend only accepts mxweb.

Workaround (recommended): call mxcadassembly directly (see "Deployment and Startup Notes" section 3):

js
// Run with the project's built-in node (CommonJS); put the source drawing in the converter directory
const { execFileSync } = require('child_process');
const jsonStr = JSON.stringify({ srcpath: 't1.dwg', outname: 't1.mxweb', outpath: './out' });
const enc = encodeURIComponent(jsonStr);   // percent-encoding, NOT plain hex!
const out = execFileSync('mxcadassembly.exe', ['[' + enc + ']'], {
  cwd: '<converter directory>', encoding: 'utf8', timeout: 180000
});
// Output {"code":0,"message":"ok"}; produces out\t1.mxweb (relative to the converter directory)

Notes: ① In the source, this command concatenates curPath + param.file directly — passing an absolute path produces an invalid path; the workaround accepts an absolute path via srcpath. ② The outpath directory must pre-exist (the program does not create it; otherwise it returns {"code":2,"message":"write file error"}).

2. getcaddata — Drawing data extraction ​

bin\node.exe mxapp.js getcaddata file=t1.dwg outfile=t1.json
  • Parameters: file source DWG; outfile output JSON path
  • Behavior: extracts all text (TEXT) and block attributes (INSERT attributes) in the drawing; each item contains the text content, coordinates (posx/posy), and the owning layer
  • Returns: {"ret":0,"resultfile":"<output file>"}
  • ⚠️ Limitation: extracts TEXT and block attributes only — no geometry entities (lines/polylines/circles, etc.). For geometry data, do secondary development (see the example in section 9 of "Mx2DNodeSecondaryDevAPIReference.md"). The output is an array; each item contains fields such as layer

3. getPolylineTheSpecifiedLayer — Polyline extraction on a specified layer ​

bin\node.exe mxapp.js getPolylineTheSpecifiedLayer file=t1.dwg outfile=t1.json [debug=1]
  • Behavior: extracts the vertex sequences (x/y/z/bulge) of LWPOLYLINE entities whose layer name matches. The layer name filter value is hardcoded in GetDwgData.ts; modify it for the target drawing before use
  • Returns: {"ret":0,"resultfile":"<output file>"}
  • ⚠️ Limitations: ① extracts LWPOLYLINE only; no other geometry such as LINE; ② the layer filter value is hardcoded in GetDwgData.ts — before using across drawings you must change the source and recompile with npm run build

4. testParamDraw — Parametric drawing ​

bin\node.exe mxapp.js testParamDraw outfile=testdraw.dwg [debug=1]
  • Behavior: based on the blank template, draws lines, dashed lines (custom linetype), block references (tree.dwg), text (SimHei), and pattern hatching (rectangular path). The drawing logic lives in the TestDraw class of sample/ParamDraw.ts; replace that drawing code for secondary development
  • Returns: {"ret":0}

5. testParamDrawTk — Title block frame + title block generation ​

bin\node.exe mxapp.js testParamDrawTk outfile=testdrawtk.dwg [debug=1]
  • Behavior: inserts the title block frame (the A3 block of A3.dwg) + the title block (the BT block of BT.DWG) and fills in the title block attribute text. The frame/title block DWGs must sit at the same level as the project directory
  • Returns: {"ret":0}

6. compare_dwg — Drawing comparison ​

bin\node.exe mxapp.js compare_dwg file1=t1.dwg file2=t2.dwg
  • Behavior: bidirectionally compares object differences among all entities in the two drawings' block tables (aligned by handle)
  • Returns: {"ret":0} when identical; non-zero means differences exist (negative values mean missing objects)
  • ⚠️ Limitation: compares only the block table contents (block definitions), not whole-drawing geometry

7. frameCut — Cutting by title block frames (1337 service edition; unusable in the standalone edition) ​

bin\node.exe mxapp.js frameCut file=testcut.dwg tkname="Frame0521" outtype=.pdf [debug=1]
  • Parameters: file source DWG; tkname title block frame name prefix; outtype output format suffix (.pdf/.dwg); optional savefile output prefix, colorPolicy
  • Behavior: selects blocks whose names start with tkname → sorts them by rows in the Y direction → calls the port-1337 service one by one to cut each by its bounding rectangle
  • The standalone MxServiceCode does not provide the 1337 service, so this command is unusable; use frameCut2 (functionally equivalent; see detailed 8)
  • Returns (when a service is available): {"code":0,"cutnum":n,"errornum":0}
bin\node.exe mxapp.js frameCut2 file=testcut.dwg tkname="Frame0521" outtype=.pdf width="5000" height="3000" [colorPolicy=mono]
  • Parameters: file, tkname, outtype (.pdf/.dwg/.mxweb); width/height output size; colorPolicy color policy
  • Behavior: same selection and sorting as frameCut, but calls mxcadassembly locally to run the cut_dwg cutting — no dependency on the 1337 service; with outtype=.mxweb it first cuts to DWG and then converts to mxweb
  • Returns: {"code":0,"cutnum":n,"errornum":0,"out_files":[...]}

9. getAllLayer — Layer information extraction ​

bin\node.exe mxapp.js getAllLayer file="A.dwg"
  • Behavior: outputs <source filename>_layers.json containing each layer's name, colorIndex, color, and off (whether turned off)
  • Returns: {"ret":0,...}
  • ⚠️ Limitation: outputs only the layer table information; no entities on the layers

10. margeDwg — Drawing merge ​

bin\node.exe mxapp.js margeDwg file="mB.dwg" marge_file="mA.dwg" out_file="mC.dwg" marge_layer="['2306','2307']" [debug=1]
  • Parameters: file target DWG (opened and kept); marge_file the DWG(s) to merge in (a single file, or a JSON array string ['a.dwg','b.dwg'] for multiple); out_file output file; marge_layer optional — merge only the specified layers (JSON array string)
  • Returns: {"code":0,"message":"ok"}

11. mxtest_getxdata — Extended data debugging ​

bin\node.exe mxapp.js mxtest_getxdata
  • A debug example: opens a hardcoded-path DWG and demonstrates xdata read/write, explode (decomposition), curve area (getArea), and intersection of two entities (intersectWith). File paths are hardcoded in the source; for reference only.
  • ⚠️ Limitation: hardcoded file paths — must not be used directly in production; an API usage example only

12. Mx_Custom_ExportPdf — Whole-drawing PDF export ​

bin\node.exe mxapp.js Mx_Custom_ExportPdf file="TkPdf.dwg" outfile="TkPdf22.pdf" pdf_width=1500 pdf_height=2500
  • Parameters: file source DWG; outfile output PDF; pdf_width/pdf_height PDF size (default 1500x2500)
  • Behavior: scales the whole drawing to the sheet ratio and outputs a PDF (black and white), and computes the coordinate range of the title block frame (located based on the "version/design/drawn by" texts) within the PDF
  • Returns: the print_to_pdf result + tk, x1/y1/x2/y2 coordinates
  • ⚠️ Limitation: title block frame localization depends on the "version/design/drawn by" texts inside the title block; if the title block does not match, the tk location may fail

IV. General Parameter Notes ​

  • Path parameters: relative paths are relative to the MxServiceCode project directory (the directory containing mxapp.js). Convention: put target files in the project directory and pass relative paths
  • ⚠️ Do not pass absolute paths (tested): some commands' source does direct string concatenation curPath + param.file (e.g. getcaddata, getPolylineTheSpecifiedLayer); an absolute path produces an invalid path
  • colorPolicy values: noPolicy (default, keep original colors), mono (black and white)
  • Error code convention: 0 success; 1~8 parameter/file errors; -1~-5 program/file open errors

V. fileparam — JSON File Parameter Mode ​

A command can take a JSON file in place of command-line parameters (suited for complex parameters or special characters):

bin\node.exe mxapp.js fileparam filepath="testcut.dwg_param.json"

JSON file contents:

json
{
  "cmd": "frameCut2",
  "file": "testcut.dwg",
  "tkname": "Frame0521",
  "outtype": ".pdf",
  "width": "5000",
  "height": "3000"
}

The cmd field names the command actually executed; the remaining fields are that command's parameters.

VI. Secondary Development Guide ​

  1. New command: create a .ts under src/sample/ with a handler function (takes a param object, returns a JSON object), register it via MxProcessor.addCommand("command name", function) in ReginstCommand() of src/MxCommand.ts, then recompile with npm run build
  2. All drawing/data APIs: see "Mx2DNodeSecondaryDevAPIReference.md"
  3. Runtime environment, logs, and test scripts: see "Deployment and Startup Notes"