How To |
The following example illustrates the child list rule.
Code |
Description |
---|---|
#! UGNX/KF 2.0 #This class makes a "train" of gears along the X-axis DefClass: GearTrain (ug_base_part); # List of diameters for the gear train. This determines the number of gears as well. (List Parameter) diameters: {5, 4, 5, 6, 3, 5 }; #Engagement distance between gears (Number Parameter) engagement: .25; #Thickness of the gears. (Number Parameter) gear_thickness: .25; |
The list attribute, diameters, determines both the number of gears and the diameter of each gear. |
#Creates the gear train (Child List) gears: { Class, SimpleGear, #If it is first gear position at (0,0,0) else offset the X distance by # the radius of current gear + radius of previous gear - engagement distance. center; if( child:index: = 1) then Point(0,0,0) else ref(nth(child:index:-1, gears:), "center:") + vector((nth(child:index:, diameters:)/2 + nth(child:index:-1, diameters:)/2 - engagement:), 0, 0 ); outer_diameter, nth(child:index:, diameters:); gear_thickness, gear_thickness:; quantity, length(diameters:); }; |
The integer pseudo-parameter quantity specifies how many child instance objects to create. The example creates the attribute "gears" as a list of six instances of the class SimpleGear. Notice the use of child:index:, where Child: refers to the current child in the list. Every child is given an index: attribute whose value is incremented by one as each new child in the child list is created. The six members of the diameters parameter specifies 5, 4, 5, 6, 3, and 5. Attribute rules are not evaluated until demanded, so you can refer to other members of the child list set within the set itself. The first child gear is centered at (0,0,0). Each successive gear is centered by adding a vector to the previous child gear center. The x-coordinate of the vector is half the previous diameter plus half the current diameter minus the engagement (overlap) distance. Nth( n, list ) is a function that returns the nth member of a list. Length(list) is a function that returns the number of items in a list. The ref( instance, string) is a function that returns the value of an attribute named by a string for a particular instance. |
# This class creates a Simple Gear. DefClass: SimpleGear (ug_base_part); (Point Parameter) center: point(0,0,0); (Number Parameter) outer_diameter: 1; (Number Parameter) gear_thickness: .25; (Child) gear: { class; ug_cylinder; Origin; center:; Diameter; outer_diameter:; Height; gear_thickness:; }; |
Simple gear class used by the child list. |
# Comment this section if you do not want the Shaft and key hole (Child) gear_shaft: { class; GearShaftHole; center; center: + vector(0,0,-gear_thickness:); hole_diameter; outer_diameter: * .2; thickness; gear_thickness: * 3; gear_blank; gear:; }; # This class creates the shaft and key hole in a simple gear. DefClass: GearShaftHole (ug_base_part); (Point Parameter) center: point(0,0,0); (Number Parameter) hole_diameter: 1; (Number Parameter) thickness: .25; (Instance Parameter) gear_blank:; |
Puts a hole in the gear's center. |
(Child) gear_shaft_hole: { class; ug_cylinder; Origin; center:; Diameter; hole_diameter:; Height; thickness:; Target; {gear_blank:}; Operation; subtract; }; |
|
(child) key_hole: { class, ug_block; Length, hole_diameter: / 4; Width, hole_diameter: / 4; Height, thickness:; Origin, center: + vector((hole_diameter: / 3), -(hole_diameter: / 8), 0); Operation, subtract; Target, {gear_shaft_hole:}; }; |
Adds a key way to the hole. |