Startup code

  • To create a parametric script, you will probably need to import functions from modules .
  • The startup code example on this Help page is an abbreviated version of the startup code.

On this page :


Example of some startup code :

# startup code begin
from param import *
Units("feet")
from point import Point, PointLocate
from member import Member, MemberLocate
from cons_line import MtrlLocate
from rnd_plate import RndPlate
from rect_plate import RectPlate
from rolled_section import RolledSection
from weld_add import Weld
# startup code end

Startup code : Questions & answers

Q. Why is startup code needed?

SDS2 Python Prompt
>>> Shape("W10x33").Depth
Traceback (most recent call last):
   File "<console", line 1, in ?
NameError: name 'Shape' is not defined
>>> from shape import Shape
>>> from param import Units
>>> Units("feet")
>>> Shape("W10x33").Depth
9.73000000000004
Shape("W10x22").Depth generates an error when Shape has not yet been imported. After Shape is imported, it returns the depth of the W10x22 section.

A . Python has a number of built-in functions and methods that you can use without having to import anything. However, to get the capabilities that you need to parametrically generate members and materials in the SDS2 model, you need to import functions from modules that were created specifically for SDS2 software.

Tip: If you are creating a script using a text editor , it is generally good practice to import only those functions that are needed.

----------

Q. What's with those # symbols in the startup code?

SDS2 Python Prompt
>>> # num = 4.5
>>> num
Traceback (most recent call last):
   File "<console", line 1, in ?
NameError: name 'num' is not defined
>>> num = 4.5
>>> num
4.5
Python disregards statements beginning with #. In this example, the statement num = 4.5 correctly assigns the value 4.5 to num only when it is not preceded by #.

A . In Python code, the pound symbol (#) is used to signify to Python that the text following the # is a comment. A comment is informational only. Comments do not affect the functionality of a script. They are used to communicate information about the code, but Python does not run them as actual code.Comments are placed to serve as section dividers.

----------

Q. What about the * symbol?

A . In an import statement, an asterisk (*) may be used as a "wild card" to import the contents of a module . In other contexts within a Python script, the asterisk may be used as the multiplication symbol. In the above example , from param import * imports all methods from the param module, which is a "generic" module that includes methods for prompting and dimension units. On the other hand, statements in the form from __ import __ specify that particular functions be imported from a particular module. For example, from member import Member, MemberLocate imports the Member() and MemberLocate() functions from the member module.

----------

Q. Why import FROM a module? Why not just import the module?

SDS2 Python Prompt
>>> import math
>>> math.pi
3.1415926535897931
>>> pi
Traceback (most recent call last):
   File "<consolength,;", line 1, in ?
NameError: name 'pi' is not defined
>>> from math import *
>>> pi
3.1415926535897931
When the math module is imported, math.pi must be typed to call pi. When the contents of the math module is imported, typing pi calls pi.

A. Importing a module does give you access to everything in that module. However, to call a function from that module, you need to type the module name followed by a period followed by the function name (e.g., math.pi ). On the other hand, when you import the entire contents of a module (e.g., from math import * ) or a particular name from the module (e.g., from math import pi ), you can type in the name by itself (e.g., pi ).

----------

Q. What does Units("feet") do?

SDS2 Python Prompt
>>> from shape import Shape
>>> from param import Units
>>> Units("feet")
>>> Shape("W10x33").Depth
9.73000000000004
>>> Units("metric")
>>> Shape("W10x33").Depth
247.142
Units("feet") sets the output to be in decimal inches. Units("metric") converts the inch value to millimeters by multiplying that value by 25.4.

A . If your script returns any values related to distances, you also need to specify the Units("...") that you want distance values in your script to be interpreted as. Units("feet") or Units("inch") or Units("inch-fraction") or Units("inch-decimal") all require that decimal inches be used for distance entries to a script (or at the Python Prompt ). Units("metric") requires that decimal millimeters be used for distance entries to a script.

Tip: The Units("...") set in the script do not have to match the " Units " set in Drawing Presentation . The parametric will Run properly even if the units do not match.

Warning: Always include the startup code Units("...") in any script that returns distances. If you do not, the script will still Run , but it will use the Units("...") that were set by the last parametric that was Run , or by the loading of custom member plugins at the time that Modeling is started, or by using Advanced Selection .