-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathbinBodyGenerator.py
More file actions
279 lines (250 loc) · 12.2 KB
/
Copy pathbinBodyGenerator.py
File metadata and controls
279 lines (250 loc) · 12.2 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import adsk.core, adsk.fusion, traceback
import os
import math
import copy
from ...lib import fusion360utils as futil
from . import const, combineUtils, faceUtils, commonUtils, sketchUtils, extrudeUtils, baseGenerator, edgeUtils, filletUtils, geometryUtils
from .binBodyCutoutGenerator import createGridfinityBinBodyCutout
from .binBodyCutoutGeneratorInput import BinBodyCutoutGeneratorInput
from .baseGeneratorInput import BaseGeneratorInput
from .binBodyGeneratorInput import BinBodyGeneratorInput, BinBodyCompartmentDefinition
from .binBodyTabGeneratorInput import BinBodyTabGeneratorInput
from .binBodyTabGenerator import createGridfinityBinBodyTab
from .binBodyLipGeneratorInput import BinBodyLipGeneratorInput
from .binBodyLipGenerator import createGridfinityBinBodyLip
from ... import config
app = adsk.core.Application.get()
ui = app.userInterface
def uniformCompartments(countX, countY):
compartments: list[BinBodyCompartmentDefinition] = []
for i in range(countX):
for j in range(countY):
compartments.append(BinBodyCompartmentDefinition(i, j, 1, 1))
return compartments
def createGridfinityBinBody(
input: BinBodyGeneratorInput,
targetComponent: adsk.fusion.Component,
) -> tuple[adsk.fusion.BRepBody, adsk.fusion.BRepBody]:
actualBodyWidth = (input.baseWidth * input.binWidth) - input.xyClearance * 2.0
actualBodyLength = (input.baseLength * input.binLength) - input.xyClearance * 2.0
binHeightWithoutBase = input.binHeight - 1
binBodyTotalHeight = binHeightWithoutBase * input.heightUnit + max(0, input.heightUnit - const.BIN_BASE_HEIGHT)
features: adsk.fusion.Features = targetComponent.features
binBodyExtrude = extrudeUtils.createBox(
actualBodyWidth,
actualBodyLength,
binBodyTotalHeight,
targetComponent,
targetComponent.xYConstructionPlane
)
binBody = binBodyExtrude.bodies.item(0)
binBody.name = 'Bin body'
bodiesToMerge: list[adsk.fusion.BRepBody] = []
bodiesToSubtract: list[adsk.fusion.BRepBody] = []
# round corners
filletUtils.filletEdgesByLength(
binBodyExtrude.faces,
input.binCornerFilletRadius,
binBodyTotalHeight,
targetComponent,
).name = 'Bin body corner fillets'
if input.hasLip:
lipOriginPoint = adsk.core.Point3D.create(
0,
0,
binHeightWithoutBase * input.heightUnit + max(0, input.heightUnit - const.BIN_BASE_HEIGHT)
)
lipInput = BinBodyLipGeneratorInput()
lipInput.baseLength = input.baseLength
lipInput.baseWidth = input.baseWidth
lipInput.binLength = input.binLength
lipInput.binWidth = input.binWidth
lipInput.hasLipNotches = input.hasLipNotches
lipInput.xyClearance = input.xyClearance
lipInput.binCornerFilletRadius = input.binCornerFilletRadius
lipInput.origin = lipOriginPoint
lipBody = createGridfinityBinBodyLip(lipInput, targetComponent)
if input.wallThickness < const.BIN_LIP_WALL_THICKNESS:
lipBottomChamferSize = max(const.BIN_BODY_CUTOUT_BOTTOM_FILLET_RADIUS, input.binCornerFilletRadius - input.wallThickness)
lipBottomChamferExtrude = extrudeUtils.createBoxAtPoint(
actualBodyWidth - input.wallThickness * 2,
(actualBodyLength - input.wallThickness - const.BIN_LIP_WALL_THICKNESS + input.xyClearance) if input.hasScoop else (actualBodyLength - input.wallThickness * 2),
lipBottomChamferSize,
targetComponent,
adsk.core.Point3D.create(
input.wallThickness,
(const.BIN_LIP_WALL_THICKNESS - input.xyClearance) if input.hasScoop else input.wallThickness,
lipOriginPoint.z,
)
)
lipBottomChamferExtrude.name = 'Lip bottom chamfer extrude'
filletUtils.filletEdgesByLength(
lipBottomChamferExtrude.faces,
lipBottomChamferSize,
lipBottomChamferSize,
targetComponent,
)
lipBottomChamferExtrudeTopFace = faceUtils.getTopFace(lipBottomChamferExtrude.bodies.item(0))
scoopSideEdge = min([edge for edge in lipBottomChamferExtrudeTopFace.edges if geometryUtils.isCollinearToX(edge)], key=lambda x: x.boundingBox.minPoint.y)
edgesToChamfer = list(scoopSideEdge.tangentiallyConnectedEdges)[3:] if input.hasScoop else scoopSideEdge.tangentiallyConnectedEdges
chamferFeatures: adsk.fusion.ChamferFeatures = features.chamferFeatures
bottomLipChamferInput = chamferFeatures.createInput2()
bottomLipChamferEdges = commonUtils.objectCollectionFromList(edgesToChamfer)
bottomLipChamferInput.chamferEdgeSets.addEqualDistanceChamferEdgeSet(
bottomLipChamferEdges,
adsk.core.ValueInput.createByReal(lipBottomChamferSize),
False)
chamferFeatures.add(bottomLipChamferInput)
combineUtils.cutBody(lipBody, commonUtils.objectCollectionFromList(lipBottomChamferExtrude.bodies), targetComponent)
bodiesToMerge.append(lipBody)
if not input.isSolid:
compartmentsMinX = input.wallThickness
compartmentsMaxX = actualBodyWidth - input.wallThickness
compartmentsMinY = (const.BIN_LIP_WALL_THICKNESS - input.xyClearance) if input.hasLip and input.hasScoop else input.wallThickness
compartmentsMaxY = actualBodyLength - input.wallThickness
totalCompartmentsWidth = compartmentsMaxX - compartmentsMinX
totalCompartmentsLength = compartmentsMaxY - compartmentsMinY
compartmentWidthUnit = (totalCompartmentsWidth - (input.compartmentsByX - 1) * input.wallThickness) / input.compartmentsByX
compartmentLengthUnit = (totalCompartmentsLength - (input.compartmentsByY - 1) * input.wallThickness) / input.compartmentsByY
for compartment in input.compartments:
compartmentX = compartmentsMinX + compartment.positionX * (compartmentWidthUnit + input.wallThickness)
compartmentY = compartmentsMinY + compartment.positionY * (compartmentLengthUnit + input.wallThickness)
compartmentOriginPoint = adsk.core.Point3D.create(
compartmentX,
compartmentY,
binBodyTotalHeight
)
compartmentWidth = compartmentWidthUnit * compartment.width + (compartment.width - 1) * input.wallThickness
compartmentLength = compartmentLengthUnit * compartment.length + (compartment.length - 1) * input.wallThickness
compartmentDepth = min(binBodyTotalHeight - const.BIN_COMPARTMENT_BOTTOM_THICKNESS, compartment.depth)
compartmentTabInput = BinBodyTabGeneratorInput()
tabOriginPoint = adsk.core.Point3D.create(
compartmentOriginPoint.x + max(0, min(input.tabPosition, input.binWidth - input.tabLength)) * input.baseWidth,
compartmentOriginPoint.y + compartmentLength,
compartmentOriginPoint.z,
)
compartmentTabInput.origin = tabOriginPoint
compartmentTabInput.length = max(0, min(input.tabLength, input.binWidth)) * input.baseWidth
compartmentTabInput.width = input.tabWidth
compartmentTabInput.overhangAngle = input.tabOverhangAngle
compartmentTabInput.topClearance = const.BIN_TAB_TOP_CLEARANCE
compartmentTabInput.tabMethod = input.tabMethod
compartmentTabInput.rootThickness = input.rootThickness
compartmentTabInput.tipThickness = input.tipThickness
compartmentTabInput.tabFilletTop = input.tabFilletTop
compartmentTabInput.tabFilletBottom = input.tabFilletBottom
compartmentTabInput.tabFilletBack = input.tabFilletBack
[compartmentMerges, compartmentCuts] = createCompartment(
input.wallThickness,
compartmentOriginPoint,
compartmentWidth,
compartmentLength,
compartmentDepth,
input.binCornerFilletRadius - input.wallThickness,
input.hasScoop,
input.scoopMaxRadius,
input.hasTab,
compartmentTabInput,
targetComponent,
)
bodiesToSubtract = bodiesToSubtract + compartmentCuts
bodiesToMerge = bodiesToMerge + compartmentMerges
if len(input.compartments) > 1:
compartmentsTopClearance = createCompartmentCutout(
input.wallThickness,
adsk.core.Point3D.create(
compartmentsMinX,
compartmentsMinY,
binBodyTotalHeight
),
actualBodyWidth - input.wallThickness * 2,
actualBodyLength - input.wallThickness - compartmentsMinY,
const.BIN_TAB_TOP_CLEARANCE,
input.binCornerFilletRadius - input.wallThickness,
False,
0,
False,
targetComponent,
)
bodiesToSubtract.append(compartmentsTopClearance)
if len(bodiesToSubtract) > 0:
combineUtils.cutBody(
binBody,
commonUtils.objectCollectionFromList(bodiesToSubtract),
targetComponent
)
if len(bodiesToMerge) > 0:
combineUtils.joinBodies(
binBody,
commonUtils.objectCollectionFromList(bodiesToMerge),
targetComponent
)
return binBody
def createCompartmentCutout(
wallThickness: float,
originPoint: adsk.core.Point3D,
width: float,
length: float,
depth: float,
cornerFilletRadius: float,
hasScoop: bool,
scoopMaxRadius: float,
hasBottomFillet: bool,
targetComponent: adsk.fusion.Component,
) -> adsk.fusion.BRepBody:
innerCutoutFilletRadius = max(const.BIN_BODY_CUTOUT_BOTTOM_FILLET_RADIUS, cornerFilletRadius)
innerCutoutInput = BinBodyCutoutGeneratorInput()
innerCutoutInput.origin = originPoint
innerCutoutInput.width = width
innerCutoutInput.length = length
innerCutoutInput.height = depth
innerCutoutInput.hasScoop = hasScoop
innerCutoutInput.scoopMaxRadius = scoopMaxRadius
innerCutoutInput.filletRadius = innerCutoutFilletRadius
innerCutoutInput.hasBottomFillet = hasBottomFillet
return createGridfinityBinBodyCutout(innerCutoutInput, targetComponent)
def createCompartment(
wallThickness: float,
originPoint: adsk.core.Point3D,
width: float,
length: float,
depth: float,
cornerFilletRadius: float,
hasScoop: bool,
scoopMaxRadius: float,
hasTab: bool,
tabInput: BinBodyTabGeneratorInput,
targetComponent: adsk.fusion.Component,
) -> tuple[list[adsk.fusion.BRepBody], list[adsk.fusion.BRepBody]]:
bodiesToMerge: list[adsk.fusion.BRepBody] = []
bodiesToSubtract: list[adsk.fusion.BRepBody] = []
innerCutoutBody = createCompartmentCutout(
wallThickness,
originPoint,
width,
length,
depth,
cornerFilletRadius,
hasScoop,
scoopMaxRadius,
True,
targetComponent,
)
bodiesToSubtract.append(innerCutoutBody)
# label tab
if hasTab:
tabBody = createGridfinityBinBodyTab(tabInput, targetComponent)
try:
intersectTabInput = targetComponent.features.combineFeatures.createInput(
tabBody,
commonUtils.objectCollectionFromList([innerCutoutBody]),
)
intersectTabInput.operation = adsk.fusion.FeatureOperations.IntersectFeatureOperation
intersectTabInput.isKeepToolBodies = True
intersectTabFeature = targetComponent.features.combineFeatures.add(intersectTabInput)
bodiesToMerge = bodiesToMerge + [body for body in list(intersectTabFeature.bodies) if not body.revisionId == innerCutoutBody.revisionId]
except:
# Fallback if intersection fails (e.g. Dimensions method might not overlap cutout exactly or logic differs)
# Just add the tab body itself
bodiesToMerge.append(tabBody)
return (bodiesToMerge, bodiesToSubtract)