Skip to content

[mxcad_2d API documentation] (../README. md)/tools

Module: tools

This module contains exports related to the MXCAD helper tool, and all exported content can be imported directly using the MXCAD package

Example

js
import { getJsonFromUrl } from "mxcad"

Table of contents

Classes

Variables

Functions

Variables

MxTools

Const MxTools: Object

Type declaration

NameType
IsZero(val: number) => boolean
_ML_String(strId: string, str: string) => string
b64Decode(str: string) => string
b64Encode(str: string) => string
copyMcDbEntityProperties(fromEnt: McDbEntity, toEnt: McDbEntity) => void
createCursor(cursorSize: number, targetFrameSize: number, color: string) => any
createMdGeLongLongArrayFormAryId(aryId: McObjectId[]) => any
downloadFile(blob: any, filename: string) => void
downloadFileFromUrl(url: string, filename: string) => void
getFileFromUrl(url: string) => Promise<undefined | Response>
getJsonFromUrl(url: string) => Promise<any>
postEmscriptenIndexedDBFile(filekey: string, url: string, retCall: (code: number, message: string) => void, param?: any) => void
postMemoryFile(memoryData: any, url: string, filename: string, retCall: (code: number, message: string) => void, param?: any) => void
saveAsFileDialog(__namedParameters: { blob: Blob ; filename?: string ; types?: FileTypes }) => Promise<undefined | false | FileSystemFileHandle & { createWritable: () => Promise<WritableStreamDefaultWriter<any>> }>

Functions

IsZero

IsZero(val): boolean

Check if a value is close to zero

Parameters

NameTypeDescription
Valnumbernumerical value

Returns

boolean

Boolean value


_ML_String

_ML_String(strId, str): string

Receive input prompt string

Parameters

NameTypeDescription
StrIdstringThe ID of a string starts with a defined ID name
Strstringprompt string

Returns

string


b64Decode

b64Decode(str): string

Decoding Base64 encoded strings into raw data

Parameters

NameTypeDescription
StrstringBase64 encoding

Returns

string

raw data


b64Encode

b64Encode(str): string

Base64 encoding of data

Parameters

NameTypeDescription
StrStringTarget Data

Returns

string

Base64 encoding


copyMcDbEntityProperties

copyMcDbEntityProperties(fromEnt, toEnt): void

Copy the properties of one McDbEntity object to another McDbEntity object

Parameters

NameTypeDescription
From Ent[McDbEntity] (../classes/2d. McDbEntity. md)Source McDbEntity object
ToEnt[McDbEntity] (../classes/2d. McDbEntity. md)Target McDbEntity object

Returns

void

Description

This function copies the layer, linetype, linetype scale, lineweight, true color, and text style attributes of the source object to the target object. This function is mainly used for batch copying object properties in MxCAD, in order to quickly create new objects with the same properties.


crateHexString

crateHexString(str): string

Convert string to hexadecimal format for editing

Parameters

NameType
strstring

Returns

string

Hexadecimal string

String

Str target string


createCursor

createCursor(cursorSize?, targetFrameSize?, color?): any

Create a custom mouse cursor object

Parameters

NameTypeDefault valueDescription
CursorSizeNumber128Mouse Size
TargetFrameSizenumber10Target box size
Colorstring"# ffffff"cursor color

Returns

any

Description

This function will generate an object containing different types of cursor based on the passed parameters, including rectangle, cross, normal, and grab cursor.


createMdGeLongLongArrayFormAryId

createMdGeLongLongArrayFormAryId(aryId): any

Create an MdGeLongLongArray object and add the id value from the passed McObjectid array to the object.

Parameters

NameTypeDescription
AryId[McObject Id] (../classes/2d. McObject Id. md) []McObject Id array

Returns

any

MdGeLongLongArray object

Description

This function will create a new MdGeLongLongArray object and iterate through the passed McObjectid array, adding the ID value of each object to MdGeLongLongArray. This function is mainly used to process a set of object IDs in MxCAD for batch operations or to pass to other functions for use.

Example

ts
import { MxCADUtility, MxCADResbuf, MxCpp } from "mxdraw";
//Select multiple objects and merge them
   async function Mx_Join() {
     let filter = new MxCADResbuf();
     filter.AddMcDbEntityTypes("LWPOLYLINE,ARC,LINE");
Let aryId=await MxCADUtility. userSelect ("Select objects to merge", filter);
     if (aryId.length == 0) {
       return;
     }
     
     MxCpp.App.MxCADAssist.MxJoin(createMdGeLongLongArrayFormAryId(aryId));
   }

downloadFile

downloadFile(blob, filename): void

Download a file in the browser

Parameters

NameTypeDescription
BlobanyBlob object, which contains the data of the file to be downloaded
FilenamestringThe file name of the file to be saved

Returns

void


downloadFileFromUrl

downloadFileFromUrl(url, filename): void

Download files from the specified URL to the user's computer

Parameters

NameTypeDescription
UrlstringURL address for downloading files
FilenamestringThe file name of the file to be saved

Returns

void


getFileFromUrl

getFileFromUrl(url): Promise<undefined | Response>

Retrieve files from the specified URL

Parameters

NameTypeDescription
URL address of file 'url''string'

Returns

Promise<undefined | Response>

Target file


getJsonFromUrl

getJsonFromUrl(url): Promise<any>

Retrieve the file JSON string from the specified URL

Parameters

NameTypeDescription
URL address of file 'url''string'

Returns

Promise<any>

JSON string


postEmscriptenIndexedDBFile

postEmscriptenIndexedDBFile(filekey, url, retCall, param?): void

Retrieve files from IndexedDB (Indexed Database) and upload them to the specified URL

Parameters

NameTypeDescription
FilekeystringThe key of the file obtained from IndexedDB
URL address to which the data will be sent, used for uploading files
RetCall(code: number, message: string)=>voidcallback function
param?AnyOptional additional parameters that can be used to pass additional data or configuration information to the upload function

Returns

void


postMemoryFile

postMemoryFile(memoryData, url, filename, retCall, param?): void

Send data from memory to the specified URL for file upload functionality

Parameters

NameTypeDescription
MemoryDataanyObjects or data structures of data in memory
URL address to which the data will be sentstring
FilenamestringThe file name of the uploaded file
RetCall(code: number, message: string)=>voidcallback function
param?AnyOptional additional parameters that can be used to pass additional data or configuration information to the upload function

Returns

void


saveAsFileDialog

saveAsFileDialog(«destructured»): Promise<undefined | false | FileSystemFileHandle & { createWritable: () => Promise<WritableStreamDefaultWriter<any>> }>

File download save popup (save as only supports Chrome 86 or Edge 86 and Opera 72) compatible with lower versions of browsers such as iE10

Parameters

NameType
«destructured»Object
› blobBlob
› filename?string
› types?FileTypes

Returns

Promise<undefined | false | FileSystemFileHandle & { createWritable: () => Promise<WritableStreamDefaultWriter<any>> }>

Example

ts
//Export DWG file
   import { MxCpp, MxTools } from "mxcad";

   async function Mx_Export_DWG() {
       const baseUrl = "http://localhost:1337";
       const mxfilepath = "/mxcad/file/";
       const saveDwgUrl = baseUrl + "/mxcad/savedwg";
//Save the MXWeb file to the server, convert it to a DWG file, and then download it.
       MxCpp.getCurrentMxCAD().saveFileToUrl(saveDwgUrl, (iResult: number, sserverResult: string) => {
           try {
               let ret = JSON.parse(sserverResult);
               if (ret.ret == "ok") {
                   let filePath = baseUrl + mxfilepath + ret.file;
                   fetch(filePath).then(async (res) => {
                       const blob = await res.blob()
                       MxTools.saveAsFileDialog({
                           blob,
                           filename: ret.file,
                           types: [{
Description: "DWG drawings",
                               accept: {
                                   "application/octet-stream": [".dwg"],
                               },
                           }]
                       })
                   })
               }
               else {
                   console.log(sserverResult);
               }
           } catch {
               console.log("Mx: sserverResult error");
           }
       });
   };