Basic Scripts for AutoCAD

by TWohlers in Circuits > Software

595 Views, 3 Favorites, 0 Comments

Basic Scripts for AutoCAD

Icon2.png

Scripts can be used to create irregular shapes that are still mathematically defined, such as non-circle-based curves. A basic single-cycle (0 to 2pi) sin graph is used as the example here. The output from this example creates a high segment count polyline from a parametric function (one variable input, two variable output).

Supplies

Screenshot 2022-11-07 094238.png

Requires

  • A blank spreadsheet (Google Sheets or Excel)
  • A text editor capable of find and replace all (Google Docs is used for this example, but others can be used the same way)
  • File extensions must be set to enabled in the file explorer, otherwise reformatting the file to a script is not possible! (The attached image shows where the setting is in the windows file explorer under the "View" tab)

Setting Up the Spreadsheet

Screenshot 2022-11-07 092052.png
Screenshot 2022-11-07 092200.png

Put the starting value for the input variable in B1. Put the step for the input variable in A2. Put "=B1+A$1" into B2. Copy B2 down using the handle on the lower right corner of the box until you get the desired maximum value for the input variable. In this example I use 0 as the start, .025 as the step, and 2 as the max.

Entering Functions

Screenshot 2022-11-07 092529.png

Now that the spreadsheet is set up, the functions can now be added.

The function for the output x position should be in C1. (here, "=B1*" is used)

The function for the output y position should be in D1. (here, "=sin(B1*pi())" is used)

Both functions should be based only on B1!

Copy both functions all the way down the same way as the B column.

(When you copy the functions down, the function will automatically adjust to use other values in the B column)

Converting to Text

Copy Values.png
Paste Without Formatting.png

Copy the C and D columns from the spreadsheet.

Open a new document in a text editor. (The example here uses Google Docs, but others should work as well)

Right-click and click "paste without formatting" (or use ctrl+shift+v to do the same thing).

Making the Text Usable

CopyTab.png
Find And Replace.png

Copy one of the tabs (wide space) from the text.

Hit ctrl+f, then more/three dots in the pop-up. (This step may be a bit different for editors other than Google Docs- all you want is find and replace)

Paste the tab that you copied into the "Find" box, and put a comma in the "Replace With" box.

Click the "Replace All" button.

Creating the Final File

New txt.png

Create a new plain text document by right-clicking the desktop or a file explorer window, hovering over new, then clicking "Text Document." (The new doc should be a .txt file)

Copy all of the text from the previous doc to the new blank text document.

Add a new line to the beginning of the document and type "pline" (without the quotes).

The resulting file should look approximately like this:

pline
0,0
0.025,0.07845909573
0.05,0.156434465
...
1.95,-0.156434465
1.975,-0.07845909573
2,0

Type Conversion

Change extension1.png
Change Extension2.png

Begin renaming the .txt file. Change the ".txt" to ".scr"

If the file name does not end in ".scr" then see the image in the "Supplies" section to enable file extensions.

When you finish renaming (changing the file extension), it will throw a warning that the file "might become unusable."

Just click the 'Yes" button.

AutoCAD will most likely change the file icon from a standard text document to the script file icon immediately; even if this does not happen, don't worry, the file should work anyway.

Running the Script

Running1.png

Once in AutoCAD, type script into the command bar and hit the enter key.

That will open a new window asking you to pick a file. (see the image attached to this step)

Select your .scr file, then click "open" and it should run.

If it does not give the output you are expecting, check your original function and/or try the tips in the next step.

What to Do If the Script Fails

Running Fail.png
Running Success.png

Both images are from the same script file. The first failed while the second did not.

A simple fix for the failure mode shown here is to zoom in on a location that does not include any points on the output pline. (this is the solution used in the example images)

Disabling object snapping may also improve output reliability while the output intersects or is contained by the view window.

If only the last segment is missing, try converting the .scr back to a .txt. After converting back to .txt, add one additional line break to the end of he file, save, and convert to .scr again. (This may or may not work. I suspect line breaks are required to perform any operation.)