Tutorials > 

To Create a Rectangle

 
 
 

The AutoCAD JavaScript tutorial demonstrates how you can use the JavaScript function and execute the AutoCAD RECTANG command to create a rectangular polyline.

  1. Use the following JavaScript code to define the values for the two points and execute the RECTANG command:
    var firstPoint,secondPoint;
    
    var options = new Acad.PromptPointOptions("give me the first point");
    Acad.Editor.getPoint(options).then(onFirstPoint,error);
    
    function onFirstPoint(arg)
    {
    	var obj = JSON.parse(arg);
    	firstPoint = obj.value;
    	
    	//again
    	var options = new Acad.PromptPointOptions("give me the second point");
    	Acad.Editor.getPoint(options).then(onSecondPoint,error);
    	
    }
    
    function error()
    {
    	alert("error getting a point");
    }
    
    
    function onSecondPoint(arg)
    {
    	var obj = JSON.parse(arg);
    	secondPoint = obj.value;
    	
    	//draw rect
    	Acad.Editor.executeCommand("RECTANG",firstPoint.x + "," + 
    firstPoint.y,"",secondPoint.x + "," + secondPoint.y,"");
    }
    NoteSave the file as getPoint.js.
  2. Register the file path from where you want to load the JavaScript file using the TRUSTEDPATHS sysvar.
  3. Use the WEBLOAD command in AutoCAD to load the getPoint.js file.
  4. Specify the first and the second point to create a rectangular polyline.

    Creating a rectangle using JavaScript