Project Overview
This repository demonstrates a code-first, parametric CAD workflow using the Autodesk Inventor COM API driven from Python. Change a single parameter in the scripts and the parts (base, metaplate, support plate, forks, brackets, wheel) are regenerated automatically. The pipeline saves parts (.ipt) and assembles them with constraints and joints — ideal for rapid design iterations and automation in mechanical design.
What it does
- Programmatically create sketches, profiles and features (extrude, revolve, fillet, chamfer).
- Expose geometric parameters so the model is parametric — update one value to change the design.
- Save generated parts to disk and import them into an assembly with automated constraints and joints.
- Provide utilities to find faces/edges and apply finishing operations (fillets/chamfers).
Setup: Connecting to Inventor
This step uses the Inventor API to connect to Autodesk Inventor, create a new part document, and (optionally) an assembly document. The Python code below demonstrates robust connection and document creation with error handling.
# Connect to Autodesk Inventor
def connect_to_inventor():
try:
inv = wc.GetActiveObject("Inventor.Application")
inv.Visible = True
return inv
except Exception as e:
print(f"Error connecting to Inventor: {e}")
return None
# Create a new part document
def create_part_document(inv):
try:
inv_part_document = inv.Documents.Add(
12290, inv.FileManager.GetTemplateFile(12290, 8962)
)
inv_part_document.UnitsOfMeasure.LengthUnits = 11269 # Set units to mm
return inv_part_document
except Exception as e:
print(f"Error creating part document: {e}")
return None
# Create a new assembly document
def create_assembly_document():
inv = connect_to_inventor()
if not inv:
return None
try:
inv_assembly_document = inv.Documents.Add(
12291, inv.FileManager.GetTemplateFile(12291, 8962)
)
inv_assembly_document.UnitsOfMeasure.LengthUnits = 11269 # Set units to mm
return inv_assembly_document
except Exception as e:
print(f"Error creating assembly document: {e}")
return None
Step 1: Create Base Part
This step uses the Inventor API to create the base part of the assembly. The Python script defines the base sketch and extrudes it to form the solid base.
import config as co
# import makepart as mp
class Base:
L1 = 0.0
L2 = 13.0
L3 = L1 - L2*2.0
W1 = 0.0
W2 = 8.65
W3 = W1 - W2*2.0
D1 = 1.7
D2 = 2.5
D3 = 4.0
TH1 = 0.3
V1 = 1.1
V2 = 1.4
V3 = 0.8
V4 = 1.1
V5 = 9.0
V6 = 1.0
V7 = 11.7
V8 = 1.0
V9 = 4.8
V10 = 2.0
def add_circle(sketch, tg):
circle_params = [
(Base.D1, -(Base.L1/2-Base.V1-Base.V2), 0.25),
(Base.D1, -(Base.L1/2-Base.V1-Base.V2-Base.V3), 0.15),
(Base.D1, -(Base.L1/2-Base.V1-Base.V2-Base.V3-Base.V4), 0.15),
(Base.D1, Base.L1/2-Base.V1-Base.V2, 0.25),
(Base.D1, Base.L1/2-Base.V1-Base.V2-Base.V3, .15),
(Base.D1, (Base.L1/2-Base.V1-Base.V2-Base.V3-Base.V4), .15),
(-Base.D1, Base.L1/2-Base.V1-Base.V2, .25),
(-Base.D1, (Base.L1/2-Base.V1-Base.V2-Base.V3), .15),
(-Base.D1, (Base.L1/2-Base.V1-Base.V2-Base.V3-Base.V4), .15),
(-Base.D1, -(Base.L1/2-Base.V1-Base.V2), .25),
(-Base.D1, -(Base.L1/2-Base.V1-Base.V2-Base.V3), .15),
(-Base.D1, -(Base.L1/2-Base.V1-Base.V2-Base.V3-Base.V4), .15),
(Base.D2, Base.L1/2-Base.V5,.15),
(-Base.D2, Base.L1/2-Base.V5, .15),
(Base.D2, -(Base.L1/2-Base.V5), .15),
(-Base.D2, -(Base.L1/2-Base.V5), .15),
(-(Base.D3+Base.V8/2), -(Base.L1/2-Base.V7), .15),
(-(Base.D3-Base.V8/2), -(Base.L1/2-Base.V7), .15),
(-(Base.D3-Base.V8/2), -(Base.L1/2-Base.V7-Base.V9), .15),
(-(Base.D3+Base.V8/2), -(Base.L1/2-Base.V7-Base.V9), .15),
(Base.D3+Base.V8/2,Base.L1/2-Base.V7, .15),
((Base.D3-Base.V8/2),(Base.L1/2-Base.V7), .15),
((Base.D3-Base.V8/2),(Base.L1/2-Base.V7-Base.V9), .15),
((Base.D3+Base.V8/2), (Base.L1/2-Base.V7-Base.V9), .15)
]
for cx, cy, r in circle_params:
sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(cx, cy), r)
def add_rectangles(sketch, tg):
rect_params = [
(Base.D2, -(Base.L1/2-Base.V5+Base.V6), .25, .5),
(Base.D2,(Base.L1/2-Base.V5+Base.V6), .25, .5),
(-Base.D2,(Base.L1/2-Base.V5+Base.V6), .25, .5),
(-Base.D2, -(Base.L1/2-Base.V5+Base.V6), .25, .5),
(0,-(Base.L1/2-Base.V1),.7,.3),
(0,(Base.L1/2-Base.V1),.7,.3)
]
for cx, cy, width, height in rect_params:
sketch.SketchLines.AddAsTwoPointCenteredRectangle(tg.CreatePoint2d(cx, cy), tg.CreatePoint2d(cx + width / 2, cy + height / 2))
def main():
inv = co.connect_to_inventor()
inv_part_document =co.create_part_document(inv)
part_com_definition = inv_part_document.ComponentDefinition
tg = inv.TransientGeometry
# sketch = co.create_sketch(part_com_definition)
Base.L1 = float(input("Height:"))
Base.W1 = float(input("Width:"))
sketch = co.create_sketch(part_com_definition)
if sketch:
add_circle(sketch, tg)
add_rectangles(sketch, tg)
rectangle_points = [((Base.W1/2-Base.W2+Base.V10), -Base.L1/2),
((Base.W1/2-Base.W2+Base.V10), -Base.L1/2+5.55),
((Base.W1/2-Base.W2+Base.V10+1), -Base.L1/2+5.55+1),
((Base.W1/2-Base.W2+Base.V10+1), -Base.L1/2+5.55+1+3.2),
((Base.W1/2-Base.W2+Base.V10+1+5.65), -Base.L1/2+5.55+1+3.2+3.25),
((Base.W1/2-Base.W2+Base.V10+1+5.65), -(-Base.L1/2+5.55+1+3.2+3.25)),
((Base.W1/2-Base.W2+Base.V10+1), -(-Base.L1/2+5.55+1+3.2)),
((Base.W1/2-Base.W2+Base.V10+1), -(-Base.L1/2+5.55+1)),
((Base.W1/2-Base.W2+Base.V10), -(-Base.L1/2+5.55)),
((Base.W1/2-Base.W2+Base.V10), -(-Base.L1/2)),
(-(Base.W1/2-Base.W2+Base.V10), -(-Base.L1/2)),
(-(Base.W1/2-Base.W2+Base.V10), -(-Base.L1/2+5.55)),
(-(Base.W1/2-Base.W2+Base.V10+1), -(-Base.L1/2+5.55+1)),
(-(Base.W1/2-Base.W2+Base.V10+1), -(-Base.L1/2+5.55+1+3.2)),
(-(Base.W1/2-Base.W2+Base.V10+1+5.65), -(-Base.L1/2+5.55+1+3.2+3.25)),
(-(Base.W1/2-Base.W2+Base.V10+1+5.65), (-Base.L1/2+5.55+1+3.2+3.25)),
(-(Base.W1/2-Base.W2+Base.V10+1), -Base.L1/2+5.55+1+3.2),
(-(Base.W1/2-Base.W2+Base.V10+1), -Base.L1/2+5.55+1),
(-(Base.W1/2-Base.W2+Base.V10), -Base.L1/2+5.55),
(-(Base.W1/2-Base.W2+Base.V10), -Base.L1/2),
]
lines = co.add_rectangle(sketch, tg, rectangle_points)
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
co.close_profile(pnts)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(part_com_definition, profile, distance=Base.TH1, direction= 20994,direction_type=20481)
inv.ActiveView.GoHome()
if __name__ == '__main__':
main()
Step 2: Create Metaplate
The metaplate is created with its own sketch and extrusion, and fillets are added to the edges for smoothness.
def create_metaplate(inv, part_com_definition, tg):
def metaplate_sketch(part_com_definition, tg, inv):
def add_circles(sketch, tg):
# List of circle parameters: (center_x, center_y, radius)
circle_params = [
(2.1, 0.55, 0.15),
(-2.1, 0.55, 0.15),
(2.1, 0.55, 0.35),
(-2.1, 0.55, 0.35),
]
for cx, cy, r in circle_params:
sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(cx, cy), r)
sketch = co.create_sketch(part_com_definition)
if sketch:
add_circles(sketch, tg)
rectangle_points1 = [
(0.35, 0),
(0.35, 0.45),
(1.76459, 0.45),
(2.1, 0.9),
(-2.1, 0.9),
(-1.76459, 0.45),
(-0.35, 0.45),
(-0.35, 0),
]
lines = co.add_rectangle(sketch, tg, rectangle_points1)
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
co.close_profile(pnts)
return sketch
sketch = metaplate_sketch(part_com_definition, tg, inv)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition,
profile,
distance="2.2 mm",
direction=20994,
direction_type=20481,
)
edges1 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(3).Edges.Item(2)
edges2 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(3).Edges.Item(4)
edges3 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(5).Edges.Item(2)
edges4 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(5).Edges.Item(4)
edges5 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(6).Edges.Item(4)
edges6 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(8).Edges.Item(2)
edge_collection = inv.TransientObjects.CreateEdgeCollection()
sided_fillet = part_com_definition.Features.FilletFeatures.CreateFilletDefinition()
edge_collection.Add(edges1)
edge_collection.Add(edges2)
edge_collection.Add(edges3)
edge_collection.Add(edges4)
edge_collection.Add(edges5)
edge_collection.Add(edges6)
sided_fillet.AddConstantRadiusEdgeSet(edge_collection, 0.1)
part_com_definition.Features.FilletFeatures.Add(sided_fillet)
inv.ActiveView.GoHome()
Step 3: Create Support Plate
This step generates the support plate, including additional sketches and chamfers for structural support.
def create_support(inv, part_com_definition, tg):
def Support_sketch(part_com_definition, tg):
sketch = co.create_sketch(
part_com_definition,
)
rect_params = [(0, 2.25, 3.2, 4.5), (0, 0.5, 0.7, 0.3), (0, 2.6, 2.5, 2.8)]
for cx, cy, width, height in rect_params:
sketch.SketchLines.AddAsTwoPointCenteredRectangle(
tg.CreatePoint2d(cx, cy),
tg.CreatePoint2d(cx + width / 2, cy + height / 2),
)
return sketch
def sketch1(sketch, tg, inv, part_com_definition):
if sketch:
rectangle_points = [
(1.25, -0.7),
(1.6, -0.7),
(1.6, -4.5),
(-1.6, -4.5),
(-1.6, -0.7),
(-1.25, -0.7),
(-1.25, -4),
(1.25, -4),
]
lines = co.add_rectangle(sketch, tg, rectangle_points)
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
co.close_profile(pnts)
profile = sketch.Profiles.AddForSolid(False, pnts)
co.extrude_profile(
part_com_definition,
profile,
distance="8 mm",
direction=20993,
direction_type=20481,
)
def sketch2(sketch, tg, inv, part_com_definition):
if sketch:
points = [(1.6, 0), (2, 0), (2.3237, 0.2184), (2.2047, 0.9361), (1.6, 1.1)]
p1 = tg.CreatePoint2d(points[0][0], points[0][1])
p2 = tg.CreatePoint2d(points[1][0], points[1][1])
p3 = tg.CreatePoint2d(points[2][0], points[2][1])
p4 = tg.CreatePoint2d(points[3][0], points[3][1])
p5 = tg.CreatePoint2d(points[4][0], points[4][1])
p = [p1, p2, p3, p4, p5]
line1 = sketch.SketchLines.AddByTwoPoints(p[0], p[1])
line2 = sketch.SketchLines.AddByTwoPoints(p[1], p[2])
line4 = sketch.SketchLines.AddByTwoPoints(p[3], p[4])
line5 = sketch.SketchLines.AddByTwoPoints(p[4], p[0])
sketch.SketchArcs.AddByFillet(line2, line4, 0.4, p[2], p[3])
circle_params = [
(2.1, 0.55, 0.15),
]
for cx, cy, r in circle_params:
sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(cx, cy), r)
pnts = inv.TransientObjects.CreateObjectCollection()
pnts.Add(line1)
pnts.Add(line2)
pnts.Add(line4)
pnts.Add(line5)
l1 = pnts.Item(1) # Get the current line
l2 = pnts.Item(2) # Get the next line
l1.EndSketchPoint.Merge(
l2.StartSketchPoint
) # Merge end point of l1 with start point of l2
l3 = pnts.Item(3) # Get the current line
l4 = pnts.Item(4) # Get the next line
l3.EndSketchPoint.Merge(
l4.StartSketchPoint
) # Merge end point of l3 with start point of l4
first_line = pnts.Item(1)
last_line = pnts.Item(4)
last_line.EndSketchPoint.Merge(
first_line.StartSketchPoint
) # Merge end point of last line with start point of first line
# zuobian
points = [
(-1.6, 0),
(-2, 0),
(-2.3237, 0.2184),
(-2.2047, 0.9361),
(-1.6, 1.1),
]
p6 = tg.CreatePoint2d(points[0][0], points[0][1])
p7 = tg.CreatePoint2d(points[1][0], points[1][1])
p8 = tg.CreatePoint2d(points[2][0], points[2][1])
p9 = tg.CreatePoint2d(points[3][0], points[3][1])
p10 = tg.CreatePoint2d(points[4][0], points[4][1])
p2 = [p6, p7, p8, p9, p10]
line6 = sketch.SketchLines.AddByTwoPoints(p2[0], p2[1])
line7 = sketch.SketchLines.AddByTwoPoints(p2[1], p2[2])
line8 = sketch.SketchLines.AddByTwoPoints(p2[3], p2[4])
line9 = sketch.SketchLines.AddByTwoPoints(p2[4], p2[0])
sketch.SketchArcs.AddByFillet(line7, line8, 0.4, p2[2], p2[3])
circle_params = [
(-2.1, 0.55, 0.15),
]
for cx, cy, r in circle_params:
sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(cx, cy), r)
pnts.Add(line6)
pnts.Add(line7)
pnts.Add(line8)
pnts.Add(line9)
l5 = pnts.Item(5) # Get the current line
l6 = pnts.Item(6) # Get the next line
l5.EndSketchPoint.Merge(
l6.StartSketchPoint
) # Merge end point of l5 with start point of l6
l7 = pnts.Item(7) # Get the current line
l8 = pnts.Item(8) # Get the next line
l7.EndSketchPoint.Merge(
l8.StartSketchPoint
) # Merge end point of l7 with start point of l8
l8.EndSketchPoint.Merge(
l5.StartSketchPoint
) # Merge end point of last line with start point of first line
profile = sketch.Profiles.AddForSolid(True, pnts)
co.extrude_profile(
part_com_definition,
profile,
distance="30 mm",
direction=20994,
direction_type=20481,
)
co.extrude_profile(
part_com_definition,
profile,
distance="4 mm",
direction=20994,
direction_type=20482,
)
def part3(sketch, tg, inv, part_com_definition):
if sketch:
points = [
(2.075, -5.7),
(2.075, -2.7),
(1.675, -2.3),
(1.6, -2.3),
(1.6, -0.7),
(1.25, -0.7),
(1.25, -2.3),
(-1.25, -2.3),
(-1.25, -0.7),
(-1.6, -0.7),
(-1.6, -2.3),
(-1.675, -2.3),
(-2.075, -2.7),
(-2.075, -5.7),
]
p1 = tg.CreatePoint2d(points[0][0], points[0][1])
p2 = tg.CreatePoint2d(points[1][0], points[1][1])
p3 = tg.CreatePoint2d(points[2][0], points[2][1])
p4 = tg.CreatePoint2d(points[3][0], points[3][1])
p5 = tg.CreatePoint2d(points[4][0], points[4][1])
p6 = tg.CreatePoint2d(points[5][0], points[5][1])
p7 = tg.CreatePoint2d(points[6][0], points[6][1])
p8 = tg.CreatePoint2d(points[7][0], points[7][1])
p9 = tg.CreatePoint2d(points[8][0], points[8][1])
p10 = tg.CreatePoint2d(points[9][0], points[9][1])
p11 = tg.CreatePoint2d(points[10][0], points[10][1])
p12 = tg.CreatePoint2d(points[11][0], points[11][1])
p13 = tg.CreatePoint2d(points[12][0], points[12][1])
p14 = tg.CreatePoint2d(points[13][0], points[13][1])
p = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14]
line1 = sketch.SketchLines.AddByTwoPoints(p[0], p[1])
line2 = sketch.SketchLines.AddByTwoPoints(p[2], p[3])
fillet1 = sketch.SketchArcs.AddByFillet(line1, line2, 0.4, p[1], p[2])
line3 = sketch.SketchLines.AddByTwoPoints(p[3], p[4])
line4 = sketch.SketchLines.AddByTwoPoints(p[4], p[5])
line5 = sketch.SketchLines.AddByTwoPoints(p[5], p[6])
line6 = sketch.SketchLines.AddByTwoPoints(p[6], p[7])
line7 = sketch.SketchLines.AddByTwoPoints(p[7], p[8])
line8 = sketch.SketchLines.AddByTwoPoints(p[8], p[9])
line9 = sketch.SketchLines.AddByTwoPoints(p[9], p[10])
line10 = sketch.SketchLines.AddByTwoPoints(p[10], p[11])
line11 = sketch.SketchLines.AddByTwoPoints(p[12], p[13])
fillet2 = sketch.SketchArcs.AddByFillet(line10, line11, 0.4, p[11], p[12])
line12 = sketch.SketchLines.AddByTwoPoints(p[13], p[0])
lines = [
line1,
line2,
line3,
line4,
line5,
line6,
line7,
line8,
line9,
line10,
line11,
line12,
]
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
l1 = pnts.Item(1)
l2 = pnts.Item(2)
l3 = pnts.Item(3)
l4 = pnts.Item(4)
l5 = pnts.Item(5)
l6 = pnts.Item(6)
l7 = pnts.Item(7)
l8 = pnts.Item(8)
l9 = pnts.Item(9)
l10 = pnts.Item(10)
l11 = pnts.Item(11)
l12 = pnts.Item(12)
l2.EndSketchPoint.Merge(l3.StartSketchPoint)
l3.EndSketchPoint.Merge(l4.StartSketchPoint)
l4.EndSketchPoint.Merge(l5.StartSketchPoint)
l5.EndSketchPoint.Merge(l6.StartSketchPoint)
l6.EndSketchPoint.Merge(l7.StartSketchPoint)
l7.EndSketchPoint.Merge(l8.StartSketchPoint)
l8.EndSketchPoint.Merge(l9.StartSketchPoint)
l9.EndSketchPoint.Merge(l10.StartSketchPoint)
l11.EndSketchPoint.Merge(l12.StartSketchPoint)
l12.EndSketchPoint.Merge(l1.StartSketchPoint)
profile = sketch.Profiles.AddForSolid(True, pnts)
co.extrude_profile(
part_com_definition,
profile,
distance="2 mm",
direction=20994,
direction_type=20482,
)
pnts2 = inv.TransientObjects.CreateObjectCollection()
yuan1 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(1.7, -1.9), 0.225
)
yuan2 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-1.7, -1.9), 0.225
)
pnts2.Add(yuan1)
pnts2.Add(yuan2)
profile2 = sketch.Profiles.AddForSolid(False, pnts2)
co.extrude_profile(
part_com_definition,
profile2,
distance="14 mm",
direction=20994,
direction_type=20481,
)
co.extrude_profile(
part_com_definition,
profile2,
distance="6 mm",
direction=20994,
direction_type=20482,
)
pnts3 = inv.TransientObjects.CreateObjectCollection()
yuan3 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(1.7, -2.7), 0.15
)
yuan4 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-1.7, -2.7), 0.15
)
yuan5 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(1.7, -3.8), 0.15
)
yuan6 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-1.7, -3.8), 0.15
)
pnts3.Add(yuan3)
pnts3.Add(yuan4)
pnts3.Add(yuan5)
pnts3.Add(yuan6)
profile3 = sketch.Profiles.AddForSolid(False, pnts3)
co.extrude_profile(
part_com_definition,
profile3,
distance="14 mm",
direction=20994,
direction_type=20482,
)
pnts4 = inv.TransientObjects.CreateObjectCollection()
yuan7 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(1.7, -3.25), 0.2
)
yuan8 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-1.7, -3.25), 0.2
)
pnts4.Add(yuan7)
pnts4.Add(yuan8)
profile4 = sketch.Profiles.AddForSolid(False, pnts4)
co.extrude_profile(
part_com_definition,
profile4,
distance="6 mm",
direction=20994,
direction_type=20482,
)
sketch = Support_sketch(part_com_definition, tg)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition,
profile,
distance="3 mm",
direction=20994,
direction_type=20481,
)
inv.ActiveView.GoHome()
extrude = part_com_definition.Features.ExtrudeFeatures.Item(1)
# 创建草图
sketch = part_com_definition.Sketches.AddWithOrientation(
extrude.EndFaces.Item(1),
part_com_definition.WorkAxes.Item(1),
True,
True,
part_com_definition.WorkPoints.Item(1),
False,
)
sketch1(sketch, tg, inv, part_com_definition)
co.chamfer_edge(inv, part_com_definition, 3, 3, 0.4)
face = part_com_definition.SurfaceBodies.Item(1).Faces.Item(14)
sketch3 = co.create_sketch_other(part_com_definition, face)
sketch2(sketch3, tg, inv, part_com_definition)
face2 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(22)
sketch4 = co.create_sketch_other(part_com_definition, face2)
part3(sketch4, tg, inv, part_com_definition)
inv.ActiveView.Fit()
Step 4: Create Forks
Fork parts are created with multiple sketches and extrusions, forming the forked structure needed for the assembly.
def create_fork(inv, part_com_definition, tg):
def base_sketch(part_com_definition, tg, inv):
sketch = co.create_sketch(part_com_definition)
if sketch:
rectangle_points = [
(0, 0),
(0, 0.7),
(0.5, 0.7),
(0.5, 3.3),
(0, 3.3),
(0, 4.0),
(1.5, 4),
(4.5, 3.5),
(6.9, 3.5),
(6.9, 3.1),
(9.3, 3.1),
(9.9, 2.8),
(9.9, 0.7),
(9.3, 0.4),
(6.9, 0.4),
(6.9, -1.0),
(4.5, -1.0),
(1.5, 0),
]
lines = co.add_rectangle(sketch, tg, rectangle_points)
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
co.close_profile(pnts)
rect_params = [
(5.7, 2.9, 1.8, 0.6),
(5.7, -0.4, 1.8, 0.6),
(8.35, 2, 2.7, 1.3),
]
for cx, cy, width, height in rect_params:
sketch.SketchLines.AddAsTwoPointCenteredRectangle(
tg.CreatePoint2d(cx, cy),
tg.CreatePoint2d(cx + width / 2, cy + height / 2),
)
profile = sketch.Profiles.AddForSolid(True, pnts)
return profile
def sketch1(part_com_definition, tg):
face1 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(19)
sketch = co.create_sketch_other(part_com_definition, face1)
if sketch:
yuan1 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(5.925, -0.35), 0.15
)
yuan2 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(5.475, -0.35), 0.15
)
yuan3 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(9.025, -0.35), 0.15
)
profile3 = sketch.Profiles.AddForSolid()
return profile3
def sketch2(part_com_definition, tg, inv):
face3 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(25)
sketch = co.create_sketch_other(part_com_definition, face3)
points = [(1.5, -0.7), (1.5, 0), (-0.35, 0), (-0.35, -0.7)]
p1 = tg.CreatePoint2d(points[0][0], points[0][1])
p2 = tg.CreatePoint2d(points[1][0], points[1][1])
p3 = tg.CreatePoint2d(points[2][0], points[2][1])
p4 = tg.CreatePoint2d(points[3][0], points[3][1])
p = [p1, p2, p3, p4]
line1 = sketch.SketchLines.AddByTwoPoints(p[0], p[1])
line2 = sketch.SketchLines.AddByTwoPoints(p[1], p[2])
line3 = sketch.SketchLines.AddByTwoPoints(p[3], p[0])
fillet1 = sketch.SketchArcs.AddByFillet(line2, line3, 0.35, p[2], p[3])
yuan4 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-0.35, -0.35), 0.15
)
lines = [line1, line2, line3]
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
l1 = pnts.Item(1)
l2 = pnts.Item(2)
l3 = pnts.Item(3)
l1.EndSketchPoint.Merge(l2.StartSketchPoint)
l3.EndSketchPoint.Merge(l1.StartSketchPoint)
profile3 = sketch.Profiles.AddForSolid()
return profile3
def sketch3(part_com_definition, tg):
face = part_com_definition.SurfaceBodies.Item(1).Faces.Item(37)
sketch = co.create_sketch_other(part_com_definition, face)
rect_params = [(-1, -2, 2, 2.6)]
for cx, cy, width, height in rect_params:
sketch.SketchLines.AddAsTwoPointCenteredRectangle(
tg.CreatePoint2d(cx, cy),
tg.CreatePoint2d(cx + width / 2, cy + height / 2),
)
profile3 = sketch.Profiles.AddForSolid()
return profile3
profile = base_sketch(part_com_definition, tg, inv)
co.extrude_profile(part_com_definition, profile, 0.7, 20993, 20481)
profile2 = sketch1(part_com_definition, tg)
co.extrude_profile(part_com_definition, profile2, 20, 20994, 20482)
profile3 = sketch2(part_com_definition, tg, inv)
co.extrude_profile(part_com_definition, profile3, 4, 20994, 20481)
profile4 = sketch3(part_com_definition, tg)
co.extrude_profile(part_com_definition, profile4, 20, 20994, 20482)
# co.find_face(part_com_definition)
edge_collection = inv.TransientObjects.CreateEdgeCollection()
edge1 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(3).Edges.Item(2)
edge_collection.Add(edge1)
# co.fillet_edge(inv,part_com_definition,0.1,edge_collecti
Step 5: Create Brackets
Brackets are generated to connect and support other parts in the assembly, using custom sketches and extrusions.
def create_brackets(inv, part_com_definition, tg):
class Zhijia:
H1 = 4.16
W1 = 2.9
H2 = 2.76
W2 = 1.2
H3 = 0.56
W3 = 2.3
def sketch1(part_com_definition, tg, inv):
sketch = co.create_sketch(part_com_definition, work_plane_index=3)
if sketch:
points = [
(-1.2, 0),
(1.2, 0),
(1.2, -1.24),
(1.6, -1.64),
(2.8003, -1.64),
(3.0878, -1.4256),
(4.7292, 4.0902),
(4.75, 4.2328),
(4.75, 4.56),
(4.15, 5.16),
(2.8856, 5.16),
(2.366, 4.86),
(1.75, 3.793),
(1.75, 3.36),
(-1.75, 3.36),
(-1.75, 3.793),
(-2.366, 4.86),
(-2.8856, 5.16),
(-4.15, 5.16),
(-4.75, 4.56),
(-4.75, 4.2328),
(-4.7292, 4.0902),
(-3.0878, -1.4256),
(-2.8003, -1.64),
(-1.6, -1.64),
(-1.2, -1.24),
]
p = [tg.CreatePoint2d(x, y) for x, y in points]
line1 = sketch.SketchLines.AddByTwoPoints(p[0], p[1])
line2 = sketch.SketchLines.AddByTwoPoints(p[1], p[2])
line4 = sketch.SketchLines.AddByTwoPoints(p[3], p[4])
sketch.SketchArcs.AddByFillet(line2, line4, 0.4, p[2], p[3])
line5 = sketch.SketchLines.AddByTwoPoints(p[5], p[6])
sketch.SketchArcs.AddByFillet(line4, line5, 0.3, p[4], p[5])
line6 = sketch.SketchLines.AddByTwoPoints(p[7], p[8])
sketch.SketchArcs.AddByFillet(line5, line6, 0.5, p[6], p[7])
line7 = sketch.SketchLines.AddByTwoPoints(p[9], p[10])
sketch.SketchArcs.AddByFillet(line6, line7, 0.6, p[8], p[9])
line8 = sketch.SketchLines.AddByTwoPoints(p[11], p[12])
sketch.SketchArcs.AddByFillet(line7, line8, 0.6, p[10], p[11])
line9 = sketch.SketchLines.AddByTwoPoints(p[12], p[13])
line10 = sketch.SketchLines.AddByTwoPoints(p[13], p[14])
line11 = sketch.SketchLines.AddByTwoPoints(p[14], p[15])
line12 = sketch.SketchLines.AddByTwoPoints(p[15], p[16])
line13 = sketch.SketchLines.AddByTwoPoints(p[17], p[18])
sketch.SketchArcs.AddByFillet(line12, line13, 0.6, p[16], p[17])
line14 = sketch.SketchLines.AddByTwoPoints(p[19], p[20])
sketch.SketchArcs.AddByFillet(line13, line14, 0.6, p[18], p[19])
line15 = sketch.SketchLines.AddByTwoPoints(p[21], p[22])
sketch.SketchArcs.AddByFillet(line14, line15, 0.5, p[20], p[21])
line16 = sketch.SketchLines.AddByTwoPoints(p[23], p[24])
sketch.SketchArcs.AddByFillet(line15, line16, 0.3, p[22], p[23])
line17 = sketch.SketchLines.AddByTwoPoints(p[25], p[0])
sketch.SketchArcs.AddByFillet(line16, line17, 0.4, p[24], p[25])
lines = [
line1,
line2,
line4,
line5,
line6,
line7,
line8,
line9,
line10,
line11,
line12,
line13,
line14,
line15,
line16,
line17,
]
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
l1 = pnts.Item(1)
l2 = pnts.Item(2)
l1.EndSketchPoint.Merge(l2.StartSketchPoint)
l3 = pnts.Item(7)
l4 = pnts.Item(8)
l3.EndSketchPoint.Merge(l4.StartSketchPoint)
l5 = pnts.Item(9)
l4.EndSketchPoint.Merge(l5.StartSketchPoint)
l6 = pnts.Item(10)
l5.EndSketchPoint.Merge(l6.StartSketchPoint)
l7 = pnts.Item(11)
l6.EndSketchPoint.Merge(l7.StartSketchPoint)
l8 = pnts.Item(16)
l8.EndSketchPoint.Merge(l1.StartSketchPoint)
circle_params = [
(Zhijia.W1, Zhijia.H1, 0.1),
(-Zhijia.W1, Zhijia.H1, 0.1),
(Zhijia.W2, Zhijia.H2, 0.15),
(-Zhijia.W2, Zhijia.H2, 0.15),
(Zhijia.W3, Zhijia.H3, 0.1),
(-Zhijia.W3, Zhijia.H3, 0.1),
(1.55, -0.31, 0.15),
(-1.55, -0.31, 0.15),
(1.55, -1.36, 0.15),
(-1.55, -1.36, 0.15),
(2.8, -1.34, 0.15),
(-2.8, -1.34, 0.15),
]
for cx, cy, r in circle_params:
sketch.SketchCircles.AddByCenterRadius(tg.CreatePoint2d(cx, cy), r)
return sketch
def sketch2(part_com_definition, tg, face):
sketch = co.create_sketch_other(part_com_definition, face)
sketch.SketchLines.AddAsTwoPointCenteredRectangle(
tg.CreatePoint2d(0, -1.2625), tg.CreatePoint2d(3.9 / 2, -1.2625 + 0.675 / 2)
)
profile2 = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile2, 1.64, direction=20994, direction_type=20482
)
rect_params = [
(1.575, -0.375, 0.75, 0.5),
(-1.575, -0.375, 0.75, 0.5),
]
for cx, cy, width, height in rect_params:
sketch.SketchLines.AddAsTwoPointCenteredRectangle(
tg.CreatePoint2d(cx, cy),
tg.CreatePoint2d(cx + width / 2, cy + height / 2),
)
profile1 = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile1, 1, direction=20994, direction_type=20482
)
def sketch3(part_com_definition, tg, face, inv):
sketch = co.create_sketch_other(part_com_definition, face)
if sketch:
points = [
(-0.685, -1.6),
(-2.36, -1.6),
(-2.36, -2.388),
(-0.105, -3.65),
(0.74, -3.65),
(0.74, -2.95),
(0.4193, -2.95),
(-0.685, -2.332),
]
p = [tg.CreatePoint2d(x, y) for x, y in points]
lines = []
for i in range(len(p) - 1):
lines.append(sketch.SketchLines.AddByTwoPoints(p[i], p[i + 1]))
lines.append(sketch.SketchLines.AddByTwoPoints(p[-1], p[0]))
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
co.close_profile(pnts)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile, 3.5, direction=20993, direction_type=20481
)
def sketch4(part_com_definition, tg, face, inv):
sketch = co.create_sketch_other(part_com_definition, face)
if sketch:
rect_params = [
(0.75, -0.96, 1.2, 2.8),
(-0.75, -0.96, 1.2, 2.8),
]
for cx, cy, width, height in rect_params:
sketch.SketchLines.AddAsTwoPointCenteredRectangle(
tg.CreatePoint2d(cx, cy),
tg.CreatePoint2d(cx + width / 2, cy + height / 2),
)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile, 2.05, direction=20994, direction_type=20482
)
def sketch5(part_com_definition, tg, face, inv):
sketch = co.create_sketch_other(part_com_definition, face)
if sketch:
points = [
(-2.0376, -1.9623),
(-2.2121, -2.06),
(-0.0268, -3.35),
(0.44, -3.35),
(0.44, -3.15),
(0.0254, -3.15),
]
p = [tg.CreatePoint2d(x, y) for x, y in points]
lines = []
for i in range(len(p) - 1):
lines.append(sketch.SketchLines.AddByTwoPoints(p[i], p[i + 1]))
lines.append(sketch.SketchLines.AddByTwoPoints(p[-1], p[0]))
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
co.close_profile(pnts)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile, 2.7, direction=20993, direction_type=20481
)
def sketch6(part_com_definition, tg, face, inv):
sketch = co.create_sketch_other(part_com_definition, face)
if sketch:
pnts1 = inv.TransientObjects.CreateObjectCollection()
yuan1 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(3.55, 0.8), 0.15
)
yuan2 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-3.55, 0.8), 0.15
)
pnts1.Add(yuan1)
pnts1.Add(yuan2)
profile1 = sketch.Profiles.AddForSolid(False, pnts1)
co.extrude_profile(
part_com_definition,
profile1,
distance="8 mm",
direction=20994,
direction_type=20482,
)
pnts2 = inv.TransientObjects.CreateObjectCollection()
yuan3 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(1, 3.4), 0.125
)
yuan4 = sketch.SketchCircles.AddByCenterRadius(
tg.CreatePoint2d(-1, 3.4), 0.125
)
pnts2.Add(yuan3)
pnts2.Add(yuan4)
profile2 = sketch.Profiles.AddForSolid(False, pnts2)
co.extrude_profile(
part_com_definition,
profile2,
distance="80 mm",
direction=20994,
direction_type=20482,
)
sketch = sketch1(part_com_definition, tg, inv)
profile = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile, 1.6, direction=20994, direction_type=20481
)
face1 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(35)
sketch2(part_com_definition, tg, face1)
face2 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(43)
sketch3(part_com_definition, tg, face2, inv)
face3 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(5)
sketch4(part_com_definition, tg, face3, inv)
face4 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(3)
sketch5(part_com_definition, tg, face4, inv)
# co.find_face(part_com_definition)
face5 = part_com_definition.SurfaceBodies.Item(1).Faces.Item(76)
sketch6(part_com_definition, tg, face5, inv)
Step 6: Create Wheel
The wheel is created with a revolve feature and additional sketches for details, then extruded for thickness.
def create_wheel(inv, part_com_definition, tg):
sketch = co.create_sketch(part_com_definition)
if sketch:
points = [
(-29.450, 1.050),
(-28.550, 1.050),
(-28.550, 1.750),
(-28.990, 1.750),
(-29.550, 2.310),
(-29.550, 4.25),
(-26.550, 4.250),
(-26.550, 4.75),
(-32.050, 4.75),
(-32.050, 4.25),
(-29.85, 4.250),
(-29.850, 1.93),
(-29.470, 1.550),
(-29.450, 1.550),
(-31.550, 4.750),
(-31.550, 4.450),
(-31.050, 4.450),
(-31.050, 4.750),
(-30.550, 4.750),
(-30.550, 4.450),
(-30.050, 4.450),
(-30.050, 4.750),
(-28.550, 4.750),
(-28.550, 4.450),
(-28.050, 4.450),
(-28.050, 4.750),
(-27.550, 4.750),
(-27.550, 4.450),
(-27.050, 4.450),
(-27.050, 4.750),
]
p = [tg.CreatePoint2d(x, y) for x, y in points]
line1 = sketch.SketchLines.AddByTwoPoints(p[0], p[1])
line2 = sketch.SketchLines.AddByTwoPoints(p[1], p[2])
line3 = sketch.SketchLines.AddByTwoPoints(p[2], p[3])
line4 = sketch.SketchLines.AddByTwoPoints(p[4], p[5])
sketch.SketchArcs.AddByFillet(line3, line4, 0.56, p[3], p[4])
line5 = sketch.SketchLines.AddByTwoPoints(p[5], p[6])
line6 = sketch.SketchLines.AddByTwoPoints(p[6], p[7])
line7 = sketch.SketchLines.AddByTwoPoints(p[8], p[9])
line8 = sketch.SketchLines.AddByTwoPoints(p[9], p[10])
line9 = sketch.SketchLines.AddByTwoPoints(p[10], p[11])
line10 = sketch.SketchLines.AddByTwoPoints(p[12], p[13])
sketch.SketchArcs.AddByFillet(line10, line9, 0.38, p[12], p[11])
line11 = sketch.SketchLines.AddByTwoPoints(p[13], p[0])
line12 = sketch.SketchLines.AddByTwoPoints(p[8], p[14])
line13 = sketch.SketchLines.AddByTwoPoints(p[14], p[15])
line14 = sketch.SketchLines.AddByTwoPoints(p[15], p[16])
line15 = sketch.SketchLines.AddByTwoPoints(p[16], p[17])
line16 = sketch.SketchLines.AddByTwoPoints(p[17], p[18])
line17 = sketch.SketchLines.AddByTwoPoints(p[18], p[19])
line18 = sketch.SketchLines.AddByTwoPoints(p[19], p[20])
line19 = sketch.SketchLines.AddByTwoPoints(p[20], p[21])
line20 = sketch.SketchLines.AddByTwoPoints(p[21], p[22])
line21 = sketch.SketchLines.AddByTwoPoints(p[22], p[23])
line22 = sketch.SketchLines.AddByTwoPoints(p[23], p[24])
line23 = sketch.SketchLines.AddByTwoPoints(p[24], p[25])
line24 = sketch.SketchLines.AddByTwoPoints(p[25], p[26])
line25 = sketch.SketchLines.AddByTwoPoints(p[26], p[27])
line26 = sketch.SketchLines.AddByTwoPoints(p[27], p[28])
line27 = sketch.SketchLines.AddByTwoPoints(p[28], p[29])
line28 = sketch.SketchLines.AddByTwoPoints(p[29], p[7])
lines = [
line1,
line2,
line3,
line4,
line5,
line6,
line7,
line8,
line9,
line10,
line11,
line12,
line13,
line14,
line15,
line16,
line17,
line18,
line19,
line20,
line21,
line22,
line23,
line24,
line25,
line26,
line27,
line28,
]
pnts = inv.TransientObjects.CreateObjectCollection()
for line in lines:
pnts.Add(line)
pnts.Item(1).EndSketchPoint.Merge(pnts.Item(2).StartSketchPoint)
pnts.Item(2).EndSketchPoint.Merge(pnts.Item(3).StartSketchPoint)
pnts.Item(4).EndSketchPoint.Merge(pnts.Item(5).StartSketchPoint)
pnts.Item(5).EndSketchPoint.Merge(pnts.Item(6).StartSketchPoint)
pnts.Item(7).EndSketchPoint.Merge(pnts.Item(8).StartSketchPoint)
pnts.Item(8).EndSketchPoint.Merge(pnts.Item(9).StartSketchPoint)
# pnts.Item(9).EndSketchPoint.Merge(pnts.Item(10).StartSketchPoint)
pnts.Item(10).EndSketchPoint.Merge(pnts.Item(11).StartSketchPoint)
pnts.Item(11).EndSketchPoint.Merge(pnts.Item(1).StartSketchPoint)
pnts.Item(7).StartSketchPoint.Merge(pnts.Item(12).StartSketchPoint)
pnts.Item(12).EndSketchPoint.Merge(pnts.Item(13).StartSketchPoint)
pnts.Item(13).EndSketchPoint.Merge(pnts.Item(14).StartSketchPoint)
pnts.Item(14).EndSketchPoint.Merge(pnts.Item(15).StartSketchPoint)
pnts.Item(15).EndSketchPoint.Merge(pnts.Item(16).StartSketchPoint)
pnts.Item(16).EndSketchPoint.Merge(pnts.Item(17).StartSketchPoint)
pnts.Item(17).EndSketchPoint.Merge(pnts.Item(18).StartSketchPoint)
pnts.Item(18).EndSketchPoint.Merge(pnts.Item(19).StartSketchPoint)
pnts.Item(19).EndSketchPoint.Merge(pnts.Item(20).StartSketchPoint)
pnts.Item(20).EndSketchPoint.Merge(pnts.Item(21).StartSketchPoint)
pnts.Item(21).EndSketchPoint.Merge(pnts.Item(22).StartSketchPoint)
pnts.Item(22).EndSketchPoint.Merge(pnts.Item(23).StartSketchPoint)
pnts.Item(23).EndSketchPoint.Merge(pnts.Item(24).StartSketchPoint)
pnts.Item(24).EndSketchPoint.Merge(pnts.Item(25).StartSketchPoint)
pnts.Item(25).EndSketchPoint.Merge(pnts.Item(26).StartSketchPoint)
pnts.Item(26).EndSketchPoint.Merge(pnts.Item(27).StartSketchPoint)
pnts.Item(27).EndSketchPoint.Merge(pnts.Item(28).StartSketchPoint)
pnts.Item(28).EndSketchPoint.Merge(pnts.Item(6).EndSketchPoint)
axis_point1 = tg.CreatePoint2d(-28.550, 0.850)
axis_point2 = tg.CreatePoint2d(-29.450, 0.850)
axis_line = sketch.SketchLines.AddByTwoPoints(axis_point1, axis_point2)
pnts = sketch.Profiles.AddForSolid()
# Add the revolve feature
pnts = part_com_definition.Features.RevolveFeatures.AddFull(pnts, line1, 20481)
# co.find_face(part_com_definition)
face = part_com_definition.SurfaceBodies.Item(1).Faces.Item(29)
sketch = co.create_sketch_other(part_com_definition, face)
numberOfSides = 6
centerPoint = tg.CreatePoint2d(1.050, 0)
circumferencePoint = tg.CreatePoint2d(1.08548, -0.61910)
inscribed = True
sketch.SketchLines.AddAsPolygon(
numberOfSides, centerPoint, circumferencePoint, inscribed
)
profile2 = sketch.Profiles.AddForSolid()
co.extrude_profile(
part_com_definition, profile2, 0.5, direction=20994, direction_type=20482
)
Step 7: Assembly
All parts are imported and assembled using constraints and joints, resulting in the final mechanical assembly.