To Create a Rectangle
This AutoCAD JavaScript tutorial demonstrates how to prompt the user for a point and pass values to an AutoCAD command.
In this tutorial you will,
Prompt the user for a point, and if successful then prompt for a second point
Draw a rectangular polyline with the AutoCAD RECTANG command based on the points specified
Type the following JavaScript statements in a plain text file.
var firstPoint, secondPoint; // Prompt for the user for the first point var options = new Acad.PromptPointOptions("Specify the first point of the rectangle: "); Acad.Editor.getPoint(options).then(onFirstPoint,error); // If the first point was successful, prompt the user for a second point function onFirstPoint(arg) { var obj = arg; firstPoint = obj.value; // Prompt for the user for the second point var options = new Acad.PromptPointOptions("Specify the opposite corner of the rectangle: "); Acad.Editor.getPoint(options).then(onSecondPoint,error); } // If an error occurred, display a general error message function error() { alert("Invalid point specified."); } // If both points were successfully specified, then draw the rectangle function onSecondPoint(arg) { var obj = arg; secondPoint = obj.value; // Draw the rectangle Acad.Editor.executeCommand("RECTANG", firstPoint.x + "," + firstPoint.y, "", secondPoint.x + "," + secondPoint.y); }
Save the file as getPoint.js.
Add the path from where you want to load the JavaScript file to the existing paths of the TRUSTEDPATHS system variable.
At the AutoCAD Command prompt, enter webload and then press enter again to use the Load option.
At the
Enter javascript URL to load:
prompt, enter the URI to the getPoint.js file.The getPoint.js file is loaded into and executed by AutoCAD if it is found in one of the paths listed in the TRUSTEDPATHS system variable.
Specify the first and second point to create a rectangular polyline.