You are on page 1of 4

Custom Python Scripts for AutoCAD Plant 3D Part 2 - AutoCA... http://adndevblog.typepad.com/autocad/2015/06/custom-pyth...

AutoCAD DevBlog (http://adndevblog.typepad.com/autocad/)

06/19/2015
Custom Python Scripts for AutoCAD Plant 3D – Part 2
By David Wolfe (http://www.autodesk.com/expert-elite/featured-members/david-wolfe) (Contributor)

Check here the Part 1 (http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-


for-autocad-plant-3d-part-1.html) of this series.

The first example script is taken from an older Autodesk University class (AU Python PDF). The pdf is
available here (http://www.pdoteam.com/download/custom-python-scripts-au-1/). Beginning on page
44, the pdf creates a sample script. The script should look like this:

from aqa.math import *


from varmain.primitiv import *
from varmain.custom import *

@activate(Group="Support",
TooltipShort="Test script",
TooltipLong="This is a custom Testscript",
LengthUnit="in",
Ports=1)
@group("MainDimensions")
@param(D=LENGTH, TooltipShort="Cylinder Diameter", Ask4Dist=True)
@param(L=LENGTH, TooltipLong="Length of the Cylinder")
@param(OF=LENGTH0)
@group(Name="meaningless enum")
@param(K=ENUM)
@enum(1, "align X")
@enum(2, "align Y")
@enum(3, "align Z")
#--------------------------------------------------------
#(arxload "PnP3dACPAdapter")
#(testacpscript "TESTSCRIPT" "D" "4.5" "L" "12")
def TESTSCRIPT(s, D=80.0, L=150.0, OF=-1, K=1, **kw):
CYLINDER(s, R=D/2, H=L, O=0.0).rotateY(90)

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.

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01b7c7a13509970b-pi)

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.

1 of 4 4/03/2017 12:43 AM
Custom Python Scripts for AutoCAD Plant 3D Part 2 - AutoCA... http://adndevblog.typepad.com/autocad/2015/06/custom-pyth...

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

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01bb08454dda970d-pi)
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.

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01b8d12a9b01970c-pi)

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.

2 of 4 4/03/2017 12:43 AM
Custom Python Scripts for AutoCAD Plant 3D Part 2 - AutoCA... http://adndevblog.typepad.com/autocad/2015/06/custom-pyth...

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01b7c7a1353d970b-pi)

Figure 1: script thumbnail

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01b7c7a13544970b-pi)
Figure 2: dimension images and tool tips

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01bb08454e09970d-pi)
Figure 3: register custom script files

Posted at 06:13 AM in Plant3D (http://adndevblog.typepad.com/autocad/plant3d/) | Permalink

3 of 4 4/03/2017 12:43 AM
Custom Python Scripts for AutoCAD Plant 3D Part 2 - AutoCA... http://adndevblog.typepad.com/autocad/2015/06/custom-pyth...

(http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-3d-part-2.html)

Comments
Comment below or sign in with Typepad (http://www.typepad.com/sitelogin?uri=http%3A%2F
%2Fadndevblog.typepad.com%2Fautocad%2F2015%2F06%2Fcustom-python-scripts-for-autocad-plant-3d-part-2.html&
fp=fbaa0b5cc1b2b18731cc3967b71a0400&view_uri=http%3A%2F%2Fprofile.typepad.com%2F&via=blogside&
post_uri=http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-3d-part-2.html)
Facebook (http://www.typepad.com/sitelogin?uri=http%3A%2F
%2Fadndevblog.typepad.com%2Fautocad%2F2015%2F06%2Fcustom-python-scripts-for-autocad-plant-3d-part-2.html&
fp=fbaa0b5cc1b2b18731cc3967b71a0400&view_uri=http%3A%2F%2Fprofile.typepad.com%2F&via=blogside&
service=facebook&post_uri=http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-
3d-part-2.html) Twitter (http://www.typepad.com/sitelogin?uri=http%3A%2F
%2Fadndevblog.typepad.com%2Fautocad%2F2015%2F06%2Fcustom-python-scripts-for-autocad-plant-3d-part-2.html&
fp=fbaa0b5cc1b2b18731cc3967b71a0400&view_uri=http%3A%2F%2Fprofile.typepad.com%2F&via=blogside&
service=twitter&post_uri=http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-
3d-part-2.html) Google+ (http://www.typepad.com/sitelogin?uri=http%3A%2F
%2Fadndevblog.typepad.com%2Fautocad%2F2015%2F06%2Fcustom-python-scripts-for-autocad-plant-3d-part-2.html&
fp=fbaa0b5cc1b2b18731cc3967b71a0400&view_uri=http%3A%2F%2Fprofile.typepad.com%2F&via=blogside&
service=gplus&post_uri=http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-
3d-part-2.html) and more... (http://www.typepad.com/sitelogin?uri=http%3A%2F
%2Fadndevblog.typepad.com%2Fautocad%2F2015%2F06%2Fcustom-python-scripts-for-autocad-plant-3d-part-2.html&
fp=fbaa0b5cc1b2b18731cc3967b71a0400&view_uri=http%3A%2F%2Fprofile.typepad.com%2F&via=blogside&
service=openid&post_uri=http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-
3d-part-2.html)

(You can use HTML tags like <b> <i> and <ul> to style your text. URLs automatically linked.)

E mail addres s is not dis played with c omment.

Name

Email Address

Web Site URL

Post Prev iew

(http://www.typepad.com/)

AutoCAD DevBlog (http://adndevblog.typepad.com/autocad/)

4 of 4 4/03/2017 12:43 AM

You might also like