Skip to content
On this page

Export jpg

Below we will introduce how to use mxcad plug-in to achieve the function of exporting jpg pictures in CAD drawings. In this function, the user can choose the drawing range and export the image in jpg format. Drawing to jpg function improves the readability and shareability of the file, but also easy to store and process, is a very practical data conversion function.

Function implementation

  1. Set the export range

We can call MxCADUtility.getCorner() Method Obtain the export range.

Users can set the size of the exported image according to their own needs, that is, to change the width and height of the image.

ts
// Get screenshot range
const ret = await MxCADUtility.getCorner("Select screenshot range", undefined, undefined, false, true);
if (!ret) return;
let mxcad = MxCpp.getCurrentMxCAD();
// Set the size of the exported image
let w = Math.abs(ret.pt1.x - ret.pt2.x);
let h = Math.abs(ret.pt1.y - ret.pt2.y);
if (w < 1 || h < 1) return;
let jpg_width = 400;
let jpg_height = jpg_width * h / w;
  1. Export the picture

In the above steps, we have obtained the scope and size of the exported picture on the drawing. So now we can call directly McObject.mxdraw.createCanvasImageData() method to export the target image. If the drawing range is not set, the drawing content in the view range is exported by default.

ts
// Export picture
mxcad.mxdraw.createCanvasImageData(
    (imageData: String) => {
        // The exported image address imageData is obtained
        let newWindow: any = window.open();
        if (newWindow != null) {
            newWindow.document.write('<img src="' + imageData + '"/>');
        }
    },
    {
        width: jpg_width, // Picture width
        height: jpg_height, // Picture height
        range_pt1: ret.pt1.toVector3(), // Screenshot range corner point 1
        range_pt2: ret.pt2.toVector3(), // Screenshot range corner point 2
    }
);

Functional practice

Practical effects are as follows:

  • Click the Export jpg button to perform the Export jpg method
  • Click the left mouse button to select the export range according to the command line prompt
  • Click the left mouse button again to end the box selection
  • Successfully exported the contents of the target range of drawings to jpg format