You are on page 1of 5

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

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

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

Get start with Part 1 (http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-


for-autocad-plant-3d-part-1.html) and Part 2 (http://adndevblog.typepad.com/autocad/2015/06
/custom-python-scripts-for-autocad-plant-3d-part-2.html) of this series.
Expansion Joint Script
This article will look at creating a completely new script, an expansion joint.
The script should look like this:

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

from varmain.primitiv import *


from varmain.custom import *

@activate(Group="Sleeve", TooltipShort="Expansion Joint", TooltipLong="Expansion Joint with Bellows", F


irstPortEndtypes="FL", LengthUnit="in", Ports="2")
@group("MainDimensions")
@param(D=LENGTH, TooltipShort="Pipe OD", TooltipLong="Pipe OD")
@param(D1=LENGTH, TooltipShort="Flange OD", TooltipLong="Flange OD")
@param(D31=LENGTH, TooltipShort="Bellows OD", TooltipLong="Bellows OD")
@param(L=LENGTH, TooltipShort="Length of the ExpansionJoint", TooltipLong="Overall Length of Expansion
Joint")
@param(L31=LENGTH, TooltipShort="Flange Thickness", TooltipLong="Flange Thickness")
@param(T=LENGTH, TooltipShort="Bellows thickness", TooltipLong="Thickness of a bellow")
@param(B=LENGTH, TooltipShort="Number of Bolts", TooltipLong="Number of bolts")
@param(B1=LENGTH, TooltipShort="Bolt Size", TooltipLong="Bolt Thickness")
@param(B2=LENGTH, TooltipShort="Bolt Length (greater than L)", TooltipLong="Bolt Length")
@param(OF=LENGTH0)

#(arxload "PnP3dACPAdapter")
#(testacpscript "EXPJOINT1")
def EXPJOINT1(s, D=4.5, D31=5.5,D1 = 9.0, B=4, B1=0.75, B2=9.5, L=9.0, L31=0.25, T=0.5, K=1, OF=0, **kw
):
NumOfBellow=round(((L - (2*L31))/ T)/2)-1
#set a default hole size
fhole = .95*D
#create a hole in the flange
if OF > 0:
fhole = (D-OF)
#create our first flange
Flange1=CYLINDER(s,R=D1/2, H=L31,O=0).rotateY(90)
bellowlength = (T*2*NumOfBellow)-1
nb = 0
#overall length - belows length / 2 plus 1/4 the width of the bellow
offset= ((L - bellowlength - (2*L31))/2)+L31
expBody = CYLINDER(s,R=D31/2,H=L-L31,O=D).rotateY(90).translate((L31,0,0))
#union the first flange and main body
Flange1.uniteWith(expBody)
expBody.erase()
while (nb < NumOfBellow):
#create and offset our torus
b=TORUS(s,R1=D31/2,R2=T/2).rotateY(90).translate((offset,0,0))
#increment our offset distance
offset = offset + T*2
#union as we go
Flange1.uniteWith(b)
b.erase()
nb = nb +1
#create our end flange and offset it
Flange2=CYLINDER(s,R=D1/2, H=L31,O=0).rotateY(90).translate((L - L31,0,0))
Flange1.uniteWith(Flange2)
Flange2.erase()
#see if we need to create bolts, if there is a bolt cound and it's greater than 0
if B > 1 and B1 > 0 and B2 > L:
anglesplit = 360/B
startangle = 0
if B%2!=0:
startangle = anglesplit/2
intbolt=0
incangle=startangle
platewidth=3*B1
platelength=4*B1
plateheight= 0.75*L31
#make the bolts the length of the object + 2 flange thickness
boltlength = L + 4*L31
#calculate our offsets
#plate on flg one offset
pof1 = (L31 - plateheight/2,D1/2+platewidth/2,0)
#translate the bolts
bof1 = (-(B2-L),D1/2+platewidth/2,0)
#translate the second plate
pof2 = (L - (L31 - plateheight/2),D1/2+platewidth/2,0)
#create a cylinder to subtract for the opening
boreCy = CYLINDER(s,R=fhole/2, H=L,O=0).rotateY(90)
Flange1.subtractFrom(boreCy)
boreCy.erase()
while (intbolt < B):
#create a plate for the bolt, rotate it, and then move it to the edge and back to face of flang
e 1
p = BOX(s,L=platewidth,W=platelength,H=plateheight).rotateX(90).translate(pof1).rotateX(incangl
e)
#unite with flange 1 so we have a single body
Flange1.uniteWith(p)
p.erase()
#create the bolt
b = CYLINDER(s, R=B1/2, H=boltlength,O=0).rotateY(90).translate(bof1).rotateX(incangle)
#join the flange and bolt

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

Flange1.uniteWith(b)
b.erase()
#create our plate on flange 2
p2 = BOX(s,L=platewidth,W=platelength,H=plateheight).rotateX(90).translate(pof2).rotateX(incang
le)
#merge the last plate
Flange1.uniteWith(p2)
p2.erase()
incangle = incangle + anglesplit
intbolt = intbolt + 1
return

Metadata
The most interesting part of this script, other than the geometry, is the metadata.

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

Note that the group defines where the script shows up in the spec editor.

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01bb08493c02970d-pi)
The tooltips show when the dimensions are hovered over in the editor as well.

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01b7c7a52ce7970b-pi)
Script Testing

While creating the script, you must be able to test it to see if your geometry can be generated. Because
these scripts are designed to be processed within AutoCAD, currently there is no IDE that you can use
to debug the shapes. The process of testing the script looks like this.

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

1. Create your script file at C:\AutoCAD Plant 3D 2016 Content\CPak Common\CustomScripts (or
wherever your Shared Content folder is for plant (MODIFYSHAREDCONTENTFOLDER).
2. Create your geometry functions, and metadata sections.
3. Start Plant 3D
4. Use PLANTREGISTERCUSTOMSCRIPTS. If there is an error in the script, you will get a warning
at the command line with the line number. For most errors line of the script with the error will be
listed. If no errors are shown, the script compiled.

(http://adndevblog.typepad.com
/.a/6a0167607c2431970b01bb08493c14970d-pi)
5. Load the PnP3dACPAdapter.arx. You can do this by pasting this at the command line (arxload
"PnP3dACPAdapter")
6. Test the script. You can test with the default parameters by pasting this at the command line:
(testacpscript "EXPJOINT1"). Fill in your script function where EXPJOINT1 is.
You can also test the script from one of the other following options:
(TESTACPSCRIPT "CPFLR")
(TESTACPSCRIPT "CPFLR" "D1" "300.5")
(TESTACPSCRIPT "CPFLR" "L" "40" "D1" "300.5" "D2" "88.9")
Or
(TESTACPSCRIPT1 "CPFLR")
(TESTACPSCRIPT1 "CPFLR" "D1=300.5")
(TESTACPSCRIPT1 "CPFLR" "L=40,D1=300.5,D2=88.9")
7. If you need to make changes, make them before closing Plant. That was you can try to register the
script again to see if you still have breaking code.
8. If the script has been changed and registered, you must close Plant 3D in order to test the geometry
again. Once the script is registered or run, the Python processor stores that script in memory until
Plant 3D is closed, so starting a new session is the only way test new script changes.

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


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

Comments
Wane said...

Thanks for posting about Python scripts its been an interesting demo so far! Where do I find the .py files for the prebuilt
equipment? in the .peqx files? all I can see is the .xmls and the images.

I was hoping to use some of the existing equipment to see how custom ones should be built.

Reply
11/24/2015 at 08:21 AM (http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-
3d-part-3.html#comment-6a0167607c2431970b01bb0894e68b970d)
_davewolfe (http://twitter.com/_davewolfe) said...

Equipment scripts are defined as part of the deployed product scripts in


"C:\AutoCAD Plant 3D 2016 Content\CPak Common\variants.zip"

The EquipmentType > Name attribute is where the xml in the peqx references the stock script.

Reply
12/08/2015 at 07:08 AM (http://adndevblog.typepad.com/autocad/2015/06/custom-python-scripts-for-autocad-plant-
3d-part-3.html#comment-6a0167607c2431970b01b7c7f713ea970b)
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-3.html&

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

fp=f8b013211a92b5357449ef3ffcfd01ed&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-3.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-3.html&
fp=f8b013211a92b5357449ef3ffcfd01ed&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-3.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-3.html&
fp=f8b013211a92b5357449ef3ffcfd01ed&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-3.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-3.html&
fp=f8b013211a92b5357449ef3ffcfd01ed&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-3.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-3.html&
fp=f8b013211a92b5357449ef3ffcfd01ed&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-3.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/)

5 of 5 4/03/2017 12:41 AM

You might also like