Custom Python Scripts for AutoCAD Plant 3D – Part 2

<?xml encoding=”UTF-8″>By David Wolfe (Contributor)

Check here the Part 1 of this series.

The first example script is taken from an older Autodesk University class (AU Python PDF). The pdf is available here. Beginning on page 44, the pdf creates a sample script. The script should look like this:

<span class="kn">from</span> <span class="nn">aqa.math</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">varmain.primitiv</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">varmain.custom</span> <span class="kn">import</span> <span class="o">*</span>
<span class="nd">@activate</span><span class="p">(</span><span class="n">Group</span><span class="o">=</span><span class="s">"Support"</span><span class="p">,</span>
<span class="n">TooltipShort</span><span class="o">=</span><span class="s">"Test script"</span><span class="p">,</span>
<span class="n">TooltipLong</span><span class="o">=</span><span class="s">"This is a custom Testscript"</span><span class="p">,</span>
<span class="n">LengthUnit</span><span class="o">=</span><span class="s">"in"</span><span class="p">,</span>
<span class="n">Ports</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="nd">@group</span><span class="p">(</span><span class="s">"MainDimensions"</span><span class="p">)</span>
<span class="nd">@param</span><span class="p">(</span><span class="n">D</span><span class="o">=</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">TooltipShort</span><span class="o">=</span><span class="s">"Cylinder Diameter"</span><span class="p">,</span> <span class="n">Ask4Dist</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="nd">@param</span><span class="p">(</span><span class="n">L</span><span class="o">=</span><span class="n">LENGTH</span><span class="p">,</span> <span class="n">TooltipLong</span><span class="o">=</span><span class="s">"Length of the Cylinder"</span><span class="p">)</span>
<span class="nd">@param</span><span class="p">(</span><span class="n">OF</span><span class="o">=</span><span class="n">LENGTH0</span><span class="p">)</span>
<span class="nd">@group</span><span class="p">(</span><span class="n">Name</span><span class="o">=</span><span class="s">"meaningless enum"</span><span class="p">)</span>
<span class="nd">@param</span><span class="p">(</span><span class="n">K</span><span class="o">=</span><span class="n">ENUM</span><span class="p">)</span>
<span class="nd">@enum</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">"align X"</span><span class="p">)</span>
<span class="nd">@enum</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s">"align Y"</span><span class="p">)</span>
<span class="nd">@enum</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s">"align Z"</span><span class="p">)</span>
<span class="c">#--------------------------------------------------------</span>
<span class="c">#(arxload "PnP3dACPAdapter")</span>
<span class="c">#(testacpscript "TESTSCRIPT" "D" "4.5" "L" "12")</span>
<span class="k">def</span> <span class="nf">TESTSCRIPT</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">D</span><span class="o">=</span><span class="mf">80.0</span><span class="p">,</span> <span class="n">L</span><span class="o">=</span><span class="mf">150.0</span><span class="p">,</span> <span class="n">OF</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">K</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="n">CYLINDER</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">R</span><span class="o">=</span><span class="n">D</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">H</span><span class="o">=</span><span class="n">L</span><span class="p">,</span> <span class="n">O</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span><span class="o">.</span><span class="n">rotateY</span><span class="p">(</span><span class="mi">90</span><span class="p">)</span>

Scripts Parts

There are three sections to this script, the imports section, the metadata section, and the actual shape script.  In addition, you may create functions as needed for your shape generation.

Imports

The imports section lists other python classes and functions that may be used by the current script. In order to avoid having to tell Plant 3D what a cylinder is and how to draw it, we can simpler reference a function (CYLINDER) and use that in our scripts.  The imports section is resolved by the AutoCAD Plant 3D Python interpreter, and as such, doesn’t load into other IDE’s.  The imports shown here are basic ones, but others available in the actual product scripts. Some of the scripts used by Plant 3D are available upon request through ADN.

01

Metadata

The metadata section instructs the PLANTREGISTERCUSTOMSCRIPTS command how to treat the script being registered. One of the key pieces not shown in the original Python script sample is the number of Ports.  In order for the script to be used in the Spec Editor, the number of ports must be included in the metadata section.

Some of the other parameters referenced in the AU python documentation aren’t used. The image options will be explained in future posts.

02

Shape Scripts

Before getting into the script portion, notice the comments above the line that starts with def.  In Python, comment rows start with pound signs (#). It’s easier to test your script if you can copy the lines minus the comments to the command line to load the testing adapter, and then call the script with values.

In any case, the def line defines our shape function that will be called to create a script. The registration command looks for the function that matches the script file name.  If you create additional functions to use within your shape script, you must locate them above the script so the script will compile. For example, to use a function called FlangeSizeCalc, you would have to define it first and then reference it lower in the script file.

03

Shape scripts can get complicated, so you should contact ADN to view samples that work.  In addition the AU Python PDF gives more details on how to move objects and use Boolean operations to get the shape needed.

Finished Script

At the beginning you saw the finished script.  Here is a screenshot of the final product with images being used in the spec editor.

fig01

Figure 1: script thumbnail

fig02

Figure 2: dimension images and tool tips

fig03

Figure 3: register custom script files


Comments

One response to “Custom Python Scripts for AutoCAD Plant 3D – Part 2”

  1. vahap sungur Avatar
    vahap sungur

    hello, when I trying to run this codes it gives me error “import error , magic number……”
    can you help me to solve this error?
    best regatds
    vahap sungur
    vsungur@yahoo.com

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading