-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicScript.py
More file actions
175 lines (140 loc) · 8.13 KB
/
BasicScript.py
File metadata and controls
175 lines (140 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Create a simple box"""
from dataclasses import dataclass
import traceback
import adsk.core
import adsk.fusion
epsilon = 1e-6
# import adsk.cam
# Initialize the global variables for the Application and UserInterface objects.
app = adsk.core.Application.get()
ui = app.userInterface
@dataclass
class BoxParameters:
border_thickness : float
bottom_thickness : float
inner_width : float
inner_length : float
inner_height : float
fillet_size : float
def __init__(self, border_thickness: float, bottom_thickness:float, inner_width:float, inner_length:float, inner_height:float, fillet_size:float):
self.border_thickness = border_thickness
self.bottom_thickness = bottom_thickness
self.inner_width = inner_width
self.inner_length = inner_length
self.inner_height = inner_height
self.fillet_size = fillet_size
def create_box(design: adsk.fusion.Design, parameter: BoxParameters, margin: float, body_name:str, component_name:str)->adsk.fusion.Component:
input_units = 'mm'
border_thickness = parameter.border_thickness
bottom_thickness = parameter.bottom_thickness
inner_width = parameter.inner_width
inner_length = parameter.inner_length
inner_height = parameter.inner_height
fillet_size = parameter.fillet_size
# We may take different input units than default units in Fusion. We'll need to convert them first.
units_manager = design.fusionUnitsManager
default_units = units_manager.internalUnits
border_thickness = units_manager.convert(border_thickness, input_units, default_units)
bottom_thickness = units_manager.convert(bottom_thickness, input_units, default_units)
inner_width = units_manager.convert(inner_width, input_units, default_units)
inner_length = units_manager.convert(inner_length, input_units, default_units)
inner_height = units_manager.convert(inner_height, input_units, default_units)
fillet_size = units_manager.convert(fillet_size, input_units, 'mm')
margin = units_manager.convert(margin, input_units, default_units)
# Get root component
rootComponent = design.rootComponent
# Get references to the sketches and plane
sketches = rootComponent.sketches
xzPlane = rootComponent.xZConstructionPlane
# Create a new sketch and get lines reference
sketch_outer_base = sketches.add(xzPlane)
base_width_total = border_thickness * 2.0 + inner_width
base_length_total = border_thickness * 2.0 + inner_length
pointOne = adsk.core.Point3D.create(margin + -base_width_total / 2.0,-base_length_total / 2.0, 0)
pointTwo = adsk.core.Point3D.create(margin + base_width_total / 2.0, base_length_total / 2.0, 0)
# Create a rectangle sketch
rectangles = sketch_outer_base.sketchCurves.sketchLines
rectangles.addTwoPointRectangle(pointOne, pointTwo)
# Create extrusion input
base_sketch_profile = sketch_outer_base.profiles.item(0)
extrudes = rootComponent.features.extrudeFeatures
base_extrude_newbody = extrudes.createInput(base_sketch_profile, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
# Define that the extent is a distance of bottom thickness + inner height.
# Our input unit is mm, we'll need to convert it into cm.
base_extrude_distance = adsk.core.ValueInput.createByReal((bottom_thickness + inner_height))
base_object_extend_definition = adsk.fusion.DistanceExtentDefinition.create(base_extrude_distance)
base_extrude_newbody.setOneSideExtent(base_object_extend_definition, adsk.fusion.ExtentDirections.PositiveExtentDirection)
base_extrude_newbody.isSolid = True
new_extrude_feature = extrudes.add(base_extrude_newbody)
# Let's dig a hole in the new created box.
# We need two points to find a face. We want to find top and bottom face as beginning and end here.
base_top_point1 = adsk.core.Point3D.create(margin + -base_width_total / 2.0, bottom_thickness + inner_height, -base_length_total / 2.0)
base_top_point2 = adsk.core.Point3D.create(margin + base_width_total / 2.0, bottom_thickness + inner_height, base_length_total / 2.0)
base_bottom_point1 = adsk.core.Point3D.create(margin + -base_width_total / 2.0, 0.0, -base_length_total / 2.0)
base_bottom_point2 = adsk.core.Point3D.create(margin + base_width_total / 2.0, 0.0, base_length_total / 2.0)
# Let's get the new body
base_extrude_item = new_extrude_feature
base_extrude_body = base_extrude_item.bodies.item(0)
# Find the faces.
bottom_face = None
sketch_inner_base = None
for face in base_extrude_body.faces:
top_point1_flag = face.isPointOnFace(base_top_point1, epsilon)
top_point2_flag = face.isPointOnFace(base_top_point2, epsilon)
bottom_point1_flag = face.isPointOnFace(base_bottom_point1, epsilon)
bottom_point2_flag = face.isPointOnFace(base_bottom_point2, epsilon)
if top_point1_flag and top_point2_flag:
sketch_inner_base = sketches.add(face)
pointOne = adsk.core.Point3D.create(-inner_length / 2.0,margin -inner_width / 2.0, 0)
pointTwo = adsk.core.Point3D.create(inner_length / 2.0, margin + inner_width / 2.0, 0)
rectangles = sketch_inner_base.sketchCurves.sketchLines
rectangles.addTwoPointRectangle(pointOne, pointTwo)
if bottom_point1_flag and bottom_point2_flag:
bottom_face = face
# Have we found top face and bottom face?
if (bottom_face is not None) and (sketch_inner_base is not None):
# There are two profiles
# The first one is the border between the new sketch and first sketch
# The second one is the sketch we've just drawn.
# We'll need the second one here.
base_inner_profile = sketch_inner_base.profiles.item(1)
bottom_object_extend_definition = adsk.fusion.ToEntityExtentDefinition.create(bottom_face, False, adsk.core.ValueInput.createByReal(-bottom_thickness))
base_extrude_cut = extrudes.createInput(base_inner_profile, adsk.fusion.FeatureOperations.CutFeatureOperation)
base_extrude_cut.setOneSideExtent(bottom_object_extend_definition, adsk.fusion.ExtentDirections.NegativeExtentDirection)
base_extrude_cut.isSolid = True
extrudes.add(base_extrude_cut)
# Let's add fillet feature
fillet_features = rootComponent.features.filletFeatures
fillet_input = fillet_features.createInput()
fillet_edges = adsk.core.ObjectCollection.create()
for edge in base_extrude_body.edges:
fillet_edges.add(edge)
fillet_size_value_input = adsk.core.ValueInput.createByString(f'{fillet_size} mm')
fillet_input.edgeSetInputs.addConstantRadiusEdgeSet(fillet_edges, fillet_size_value_input, False)
fillet_features.add(fillet_input)
base_extrude_body.name = body_name
new_body = base_extrude_body.createComponent()
component = new_body.parentComponent
component.name = component_name
return component
def run(_context: str):
# Units are in mm
bottom_box_parameters = BoxParameters(4.0, 4.0, 190.0, 390.0, 110.0, 0.5)
# The bottom, I need to add the border thickness to the inner width/length
top_box_parameters = BoxParameters(4.0, 4.0, 198.0, 398.0, 30.0, 0.5)
# I want to put it a base margin of 50mm between each box in the design view.
base_margin = 50.0
"""This function is called by Fusion when the script is run."""
default_units = None
try:
# Get the design through active product
design = adsk.fusion.Design.cast(app.activeProduct)
if not design:
ui.messageBox("The DESIGN workspace must be active when running this command.")
return
create_box(design, bottom_box_parameters, (-bottom_box_parameters.inner_width - bottom_box_parameters.border_thickness) / 2.0 - base_margin, "bottom_body", "box")
create_box(design, top_box_parameters, base_margin + (top_box_parameters.border_thickness + top_box_parameters.inner_width) / 2.0, "top_body", "lid")
app.log(app.data.activeProject.rootFolder.name)
except: #pylint:disable=bare-except
# Write the error message to the TEXT COMMANDS window.
app.log(f'Failed:\n{traceback.format_exc()}')