forked from phil-nox/NiceBox-360
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNiceBox-360.py
821 lines (650 loc) · 32.9 KB
/
NiceBox-360.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#Author-Philipp Nox, CopyPasteStd
#Description- Box generator for Fusion 360
import adsk.core, adsk.fusion, adsk.cam, traceback
import os, tempfile, platform
commandId = 'adskNiceBox360AddIn'
defaultBoxName = 'Box'
defaultWall = 0.3
defaultH = 10
defaultW = 10
defaultD = 10
defaultKerf = 0.03
defaultShiftTotal = 1
defaultSheetAlpha = 0.3 #Tooth Proportion parameter
defaultMill = 0.2
defaultSaveDXF = True
# global set of event handlers to keep them referenced for the duration of the command
handlers = []
app = adsk.core.Application.get()
if app:
ui = app.userInterface
newComp = None
def showMessage(string):
appMes = adsk.core.Application.get()
ui = appMes.userInterface
ui.messageBox(str(string))
def createNewComponent(rootComp):
allOccs = rootComp.occurrences
newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
return newOcc.component
class BoxCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self, box):
super().__init__()
self._box = box
def notify(self, args):
try:
# unitsMgr = app.activeProduct.unitsManager
command = args.firingEvent.sender
inputs = command.commandInputs
box = self._box
box.boxName = inputs[0].value
# box.wall = unitsMgr.evaluateExpression(inputs[1].expression, "cm")
# box.h = unitsMgr.evaluateExpression(inputs[2].expression, "cm")
# box.w = unitsMgr.evaluateExpression(inputs[3].expression, "cm")
# box.d = unitsMgr.evaluateExpression(inputs[4].expression, "cm")
# box.kerf = unitsMgr.evaluateExpression(inputs[5].expression, "cm")
# box.mill = unitsMgr.evaluateExpression(inputs[6].expression, "cm")
# box.shiftTotal = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftTop = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftBack = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftFront = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftBottom = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.sheetAlpha = unitsMgr.evaluateExpression(inputs[8].expression, "cm")
if(not box.preview):
box.preview = True;
box.buildBox()
except:
if ui:
#ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
print('Failed:\n{}'.format(traceback.format_exc()))
class BoxCommandExecutePreviewHandler(adsk.core.CommandEventHandler):
def __init__(self, box):
super().__init__()
self._box = box
def notify(self, args):
try:
# unitsMgr = app.activeProduct.unitsManager
command = args.firingEvent.sender
inputs = command.commandInputs
box = self._box
box.boxName = inputs[0].value
# box.wall = unitsMgr.evaluateExpression(inputs[1].expression, "cm")
# box.h = unitsMgr.evaluateExpression(inputs[2].expression, "cm")
# box.w = unitsMgr.evaluateExpression(inputs[3].expression, "cm")
# box.d = unitsMgr.evaluateExpression(inputs[4].expression, "cm")
# box.kerf = unitsMgr.evaluateExpression(inputs[5].expression, "cm")
# box.mill = unitsMgr.evaluateExpression(inputs[6].expression, "cm")
# box.shiftTotal = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftTop = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftBack = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftFront = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.shiftBottom = unitsMgr.evaluateExpression(inputs[7].expression, "cm")
# box.sheetAlpha = unitsMgr.evaluateExpression(inputs[8].expression, "cm")
if(inputs[9].value):
box.preview = True;
box.buildBox()
args.isValidResult = True
else :
box.preview = False;
args.isValidResult = False
except:
if ui:
#ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
print('Failed:\n{}'.format(traceback.format_exc()))
class BoxCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# when the command is done, terminate the script
# this will release all globals which will remove all event handlers
#adsk.terminate()
pass
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class BoxCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
cmd.isRepeatable = False
onExecute = BoxCommandExecuteHandler(box)
cmd.execute.add(onExecute)
onExecutePreview = BoxCommandExecutePreviewHandler(box)
cmd.executePreview.add(onExecutePreview)
onDestroy = BoxCommandDestroyHandler()
cmd.destroy.add(onDestroy)
# keep the handler referenced beyond this function
handlers.append(onExecute)
handlers.append(onExecutePreview)
handlers.append(onDestroy)
#define the inputs
inputs = cmd.commandInputs
inputs.addStringValueInput('boxName', 'Box Name', defaultBoxName)
initWall = adsk.core.ValueInput.createByReal(defaultWall)
inputs.addValueInput('wall', 'Wall','',initWall)
initH = adsk.core.ValueInput.createByReal(defaultH)
inputs.addValueInput('height', 'Height', '', initH)
initW = adsk.core.ValueInput.createByReal(defaultW)
inputs.addValueInput('w', 'Width', '', initW)
initD = adsk.core.ValueInput.createByReal(defaultD)
inputs.addValueInput('d', 'Depth', '', initD)
#to do the thread length
initKerf = adsk.core.ValueInput.createByReal(defaultKerf)
inputs.addValueInput('kerf', 'Kerf Laser Cut', '', initKerf)
initMill = adsk.core.ValueInput.createByReal(defaultMill)
inputs.addValueInput('mill', 'Mill diameter', '', initMill)
initShiftTotal = adsk.core.ValueInput.createByReal(defaultShiftTotal)
inputs.addValueInput('shiftTotal', 'Shift', '', initShiftTotal)
initSheetAlpha = adsk.core.ValueInput.createByReal(defaultSheetAlpha)
inputs.addValueInput('sheetAlpha', 'Tooth Proportions', '', initSheetAlpha)
inputs.addBoolValueInput('button', 'Preview', True)
# Create readonly textbox input
if(platform.system() == 'Windows'):
inputs.addTextBoxCommandInput('textBox' + '_textBox', 'DXF path', '~\Desktop\\NiceBox360_DXF', 2, True)
if(platform.system() == 'Darwin'):
inputs.addTextBoxCommandInput('textBox' + '_textBox', 'DXF path', '~/NiceBox360_DXF', 2, True)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class BOX:
def __init__(self):
self._boxName = defaultBoxName
self._wall = defaultWall
self._h = defaultH
self._w = defaultW
self._d = adsk.core.ValueInput.createByReal(defaultD)
self._kerf = defaultKerf
self._mill = defaultMill
self._shiftTotal = adsk.core.ValueInput.createByReal(defaultShiftTotal)
self._sheetAlpha = adsk.core.ValueInput.createByReal(defaultSheetAlpha)
self._shiftTop = adsk.core.ValueInput.createByReal(defaultShiftTotal)
self._shiftBack = adsk.core.ValueInput.createByReal(defaultShiftTotal)
self._shiftFront = adsk.core.ValueInput.createByReal(defaultShiftTotal)
self._shiftBottom = adsk.core.ValueInput.createByReal(defaultShiftTotal)
self._saveDXF = defaultSaveDXF
self._preview = False
#properties
@property
def boxName(self):
return self._boxName
@boxName.setter
def boxName(self, value):
self._boxName = value
@property
def wall(self):
return self._wall
@wall.setter
def wall(self, value):
self._wall = value
@property
def h(self):
return self._h
@h.setter
def h(self, value):
self._h = value
@property
def w(self):
return self._w
@w.setter
def w(self, value):
self._w = value
@property
def d(self):
return self._d
@d.setter
def d(self, value):
self._d = value
@property
def kerf(self):
return self._kerf
@kerf.setter
def kerf(self, value):
self._kerf = value
@property
def mill(self):
return self._mill
@mill.setter
def mill(self, value):
self._mill = value
@property
def saveDXF(self):
return self._saveDXF
@saveDXF.setter
def saveDXF(self, value):
self._saveDXF = value
@property
def shiftTotal(self):
return self._shiftTotal
@shiftTotal.setter
def shiftTotal(self, value):
self._shiftTotal = value
@property
def shiftTop(self):
return self._shiftTop
@shiftTop.setter
def shiftTop(self, value):
self._shiftTop = value
@property
def shiftBack(self):
return self._shiftBack
@shiftBack.setter
def shiftBack(self, value):
self._shiftBack = value
@property
def shiftBottom(self):
return self._shiftBottom
@shiftBottom.setter
def shiftBottom(self, value):
self._shiftBottom = value
@property
def shiftFront(self):
return self._shiftFront
@shiftFront.setter
def shiftFront(self, value):
self._shiftFront = value
@property
def sheetAlpha(self):
return self._sheetAlpha
@sheetAlpha.setter
def sheetAlpha(self, value):
self._sheetAlpha = value
@property
def preview(self):
return self._preview
@preview.setter
def preview(self, value):
self._preview = value
def buildBox(self):
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent
root = createNewComponent(rootComp)
root.name = defaultBoxName
features = root.features
extrudes = root.features.extrudeFeatures
wall = self.wall
h = self.h #height
w = self.w
d = self.d
kerf = self.kerf
mill = self.mill
shiftTotal = self.shiftTotal
shiftTop = self.shiftTotal
shiftBack = self.shiftTotal
shiftBottom = self.shiftTotal
shiftFront = self.shiftTotal
sheetAlpha = self.sheetAlpha
sheetZ = (w-2*wall)*sheetAlpha/2 #2
sheetXBase = (d-2*wall-shiftFront-shiftBack)*sheetAlpha/2 #1
sheetXFront = (h-2*wall-shiftTop-shiftBottom)*sheetAlpha/2 #1
conerFront = d/2-wall-shiftFront #-(wall-kerf)
conerBack = d/2-wall-shiftBack
self.bot_top("Bottom",shiftBottom,root,conerFront,conerBack,sheetZ,sheetXBase)
self.bot_top("Top",h-wall-shiftTop,root,conerFront,conerBack,sheetZ,sheetXBase)
self.left_right("Right",(w-wall)/2, root,sheetXBase,sheetXFront)
self.left_right("Left",-(w-wall)/2-wall, root,sheetXBase,sheetXFront)
self.front_back("Front",conerBack, root,sheetXFront,sheetZ)
self.front_back("Back",-conerFront-wall, root,sheetXFront,sheetZ)
componentNameMap = {}
componentNameMap[root.name] = root
for occ in root.allOccurrences:
subComp = occ.component
componentNameMap[subComp.name] = subComp
allbodies = adsk.core.ObjectCollection.create()
for comp in list(componentNameMap.values()):
for body in comp.bRepBodies:
allbodies.add(body)
# print(root.bRepBodies.count)
# print(root.sketches.count)
def rectForBox(self, sketch, offset, cX,cY,w,h):
#make rectangle
rect = sketch.sketchCurves.sketchLines.addCenterPointRectangle(adsk.core.Point3D.create(cX,cY,offset),adsk.core.Point3D.create(cX+w,cY+h,offset))
#make mill's holes
yMill = True
toTrim = True
mill = self.mill
if(mill>0.0):
r=mill/2.0
if(yMill):
if(w>h):
pnt0 = adsk.core.Point3D.create(cX+w-r, cY+h, offset)
pnt1 = adsk.core.Point3D.create(cX+w-r, cY-h, offset)
pnt2 = adsk.core.Point3D.create(cX-w+r, cY-h, offset)
pnt3 = adsk.core.Point3D.create(cX-w+r, cY+h, offset)
else:
pnt0 = adsk.core.Point3D.create(cX+w, cY+h-r, offset)
pnt1 = adsk.core.Point3D.create(cX+w, cY-h+r, offset)
pnt2 = adsk.core.Point3D.create(cX-w, cY-h+r, offset)
pnt3 = adsk.core.Point3D.create(cX-w, cY+h-r, offset)
else:
if(w>h):
pnt0 = adsk.core.Point3D.create(cX+w, cY+h-r, offset)
pnt1 = adsk.core.Point3D.create(cX+w, cY-h+r, offset)
pnt2 = adsk.core.Point3D.create(cX-w, cY-h+r, offset)
pnt3 = adsk.core.Point3D.create(cX-w, cY+h-r, offset)
else:
pnt0 = adsk.core.Point3D.create(cX+w-r, cY+h, offset)
pnt1 = adsk.core.Point3D.create(cX+w-r, cY-h, offset)
pnt2 = adsk.core.Point3D.create(cX-w+r, cY-h, offset)
pnt3 = adsk.core.Point3D.create(cX-w+r, cY+h, offset)
cir0 = sketch.sketchCurves.sketchCircles.addByCenterRadius(pnt0, r)
cir1 = sketch.sketchCurves.sketchCircles.addByCenterRadius(pnt1, r)
cir2 = sketch.sketchCurves.sketchCircles.addByCenterRadius(pnt2, r)
cir3 = sketch.sketchCurves.sketchCircles.addByCenterRadius(pnt3, r)
if(toTrim):
if(w>h):
rect.item(0).trim(pnt0)
rect.item(0).trim(pnt3)
rect.item(2).trim(pnt1)
rect.item(2).trim(pnt2)
else:
rect.item(1).trim(pnt0)
rect.item(1).trim(pnt1)
rect.item(3).trim(pnt3)
rect.item(3).trim(pnt2)
cir0.trim(adsk.core.Point3D.create(cX,cY,offset))
cir1.trim(adsk.core.Point3D.create(cX,cY,offset))
cir2.trim(adsk.core.Point3D.create(cX,cY,offset))
cir3.trim(adsk.core.Point3D.create(cX,cY,offset))
def left_right(self,_name,offset,root,sheetXBase,sheetXFront):
#Component rename
side = createNewComponent(root)
side.name = _name
#Sketch rename
sketches = side.sketches
planeYZ = side.yZConstructionPlane
sketch = sketches.add(planeYZ)
sketch.name = _name
lines = sketch.sketchCurves.sketchLines
lines.addTwoPointRectangle(adsk.core.Point3D.create(self.d/2,self.h,offset),adsk.core.Point3D.create(-self.d/2,0,offset))
#axe = self.shiftBottom+self.wall/2 THIS is important
self.rectForBox(sketch,offset, cX= 0, cY= self.shiftBottom+self.wall/2, w= sheetXBase, h= (self.wall-self.kerf)/2);
#axe = self.h-self.shiftTop-self.wall/2 THIS is important
self.rectForBox(sketch,offset, cX= 0, cY= self.h-self.shiftTop-self.wall/2, w= sheetXBase, h= (self.wall-self.kerf)/2);
#axe = self.d/2-self.shiftBack-self.wall/2 THIS is important
self.rectForBox(sketch,offset, cX= self.d/2-self.shiftBack-self.wall/2, cY= self.h/2, w= (self.wall-self.kerf)/2, h= sheetXFront);
#axe = self.d/2-self.shiftBack-self.wall/2 THIS is important
self.rectForBox(sketch,offset, cX= -self.d/2+self.shiftFront+self.wall/2, cY= self.h/2, w= (self.wall-self.kerf)/2, h= sheetXFront);
extrudes = side.features.extrudeFeatures
#prof = sketch.profiles[0]
profs = adsk.core.ObjectCollection.create()
for prof in sketch.profiles:
profs.add(prof)
#print(profs.count)
extrudeInput = extrudes.createInput(profs[0], adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distExtrude = adsk.core.ValueInput.createByReal(self.wall)
extrudeInput.setDistanceExtent(False, distExtrude)
#Extrude rename
sideExtrude = extrudes.add(extrudeInput)
sideExtrude.name = _name
#Body rename
sideBody = side.bRepBodies.item(side.bRepBodies.count-1)
sideBody.name = _name
if(self.saveDXF):
saveToDXF(sketch, _name)
return sideExtrude
def front_back(self,_name,offset,root,sheetXFront,sheetZ):
#Component rename
toTrim = True
side = createNewComponent(root)
side.name = _name
#Sketch rename
sketches = side.sketches
planeXY = side.xYConstructionPlane
sketch = sketches.add(planeXY)
sketch.name = _name
lines = sketch.sketchCurves.sketchLines
# Main rectangle
mainRectangle = lines.addTwoPointRectangle(adsk.core.Point3D.create(-(self.w-self.wall)/2,self.h,offset),adsk.core.Point3D.create((self.w-self.wall)/2,0,offset))
# sheetXFront for left
point1 = adsk.core.Point3D.create(-(self.w-self.wall)/2,self.h/2,offset)
point2 = adsk.core.Point3D.create(-(self.w-self.wall)/2-self.wall,self.h/2+sheetXFront,offset)
point3 = adsk.core.Point3D.create(-(self.w-self.wall)/2+self.wall,self.h/2+sheetXFront,offset)
point4 = adsk.core.Point3D.create(-(self.w-self.wall)/2+self.wall,self.h/2-sheetXFront,offset)
rectangleToCut = lines.addCenterPointRectangle(point1,point2)
rectangleToCut.item(1).deleteMe()
rectangleToCut.item(0).trim(point4, False) #Top
rectangleToCut.item(2).trim(point3, False) #Bottom
leftLine = mainRectangle.item(3).trim(point1, False) #Left
# sheetXFront for Right
point1 = adsk.core.Point3D.create((self.w-self.wall)/2,self.h/2,offset)
point2 = adsk.core.Point3D.create((self.w-self.wall)/2+self.wall,self.h/2+sheetXFront,offset)
point3 = adsk.core.Point3D.create((self.w-self.wall)/2-self.wall,self.h/2+sheetXFront,offset)
point4 = adsk.core.Point3D.create((self.w-self.wall)/2-self.wall,self.h/2-sheetXFront,offset)
rectangleToCut = lines.addCenterPointRectangle(point1,point2)
rectangleToCut.item(1).deleteMe()
rectangleToCut.item(0).trim(point3, False) #Top
rectangleToCut.item(2).trim(point4, False) #Bottom
rightLine = mainRectangle.item(1).trim(point1, False) #Right
# hole for the top
#axe = self.shiftBottom+self.wall/2 THIS is important
self.rectForBox(sketch,offset, cX= 0, cY= self.shiftBottom+self.wall/2, w= sheetZ, h= (self.wall-self.kerf)/2);
#axe = self.h-self.shiftTop-self.wall/2 THIS is important
self.rectForBox(sketch,offset, cX= 0, cY= self.h-self.shiftTop-self.wall/2, w= sheetZ, h= (self.wall-self.kerf)/2);
mill = self.mill
if(mill > 0.0):
r=mill/2.0
tmpCenter = adsk.core.Point3D.create(-(self.w-self.wall)/2,self.h/2+sheetXFront+r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(-(self.w-self.wall)/2-r,self.h/2+sheetXFront+r,offset))
leftLine.item(1).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create(-(self.w-self.wall)/2,self.h/2-sheetXFront-r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(-(self.w-self.wall)/2-r,self.h/2-sheetXFront-r,offset))
leftLine.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create((self.w-self.wall)/2,self.h/2+sheetXFront+r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create((self.w-self.wall)/2+r,self.h/2+sheetXFront+r,offset))
rightLine.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create((self.w-self.wall)/2,self.h/2-sheetXFront-r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create((self.w-self.wall)/2+r,self.h/2-sheetXFront-r,offset) )
rightLine.item(1).trim(tmpCenter)
extrudes = side.features.extrudeFeatures
#prof = sketch.profiles[0]
profs = adsk.core.ObjectCollection.create()
for prof in sketch.profiles:
profs.add(prof)
print(profs.count)
extrudeInput = extrudes.createInput(profs[0], adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distExtrude = adsk.core.ValueInput.createByReal(self.wall)
extrudeInput.setDistanceExtent(False, distExtrude)
#Extrude rename
sideExtrude = extrudes.add(extrudeInput)
sideExtrude.name = _name
#Body rename
sideBody = side.bRepBodies.item(side.bRepBodies.count-1)
sideBody.name = _name
if(self.saveDXF):
saveToDXF(sketch, _name)
return sideExtrude
def bot_top(self,_name,offset,root,conerFront,conerBack,sheetZ,sheetXBase):
#Component rename
toTrim = True
side = createNewComponent(root)
side.name = _name
#Sketch rename
sketches = side.sketches
planeXZ = side.xZConstructionPlane
sketch = sketches.add(planeXZ)
sketch.name = _name
lines = sketch.sketchCurves.sketchLines
# half of base from origin to back
baseBackPoint0 = adsk.core.Point3D.create(0,conerBack,offset) #MidPoint
baseBackPoint1 = adsk.core.Point3D.create(-(self.w-self.wall)/2,0,offset)
baseBackPoint2 = adsk.core.Point3D.create((self.w-self.wall)/2,conerBack,offset)
baseBackToCut = lines.addTwoPointRectangle(baseBackPoint1,baseBackPoint2)
baseBackToCut.item(0).deleteMe()
# half of base from origin to front
baseFrontPoint0 = adsk.core.Point3D.create(0,-conerFront,offset) #MidPoint
baseFrontPoint1 = adsk.core.Point3D.create(-(self.w-self.wall)/2,0,offset)
baseFrontPoint2 = adsk.core.Point3D.create((self.w-self.wall)/2,-conerFront,offset)
baseFrontToCut = lines.addTwoPointRectangle(baseFrontPoint1,baseFrontPoint2)
baseFrontToCut.item(0).deleteMe()
# sheetZ for back
point1 = adsk.core.Point3D.create(0,conerBack,offset)
point2 = adsk.core.Point3D.create(sheetZ,conerBack-self.wall,offset) #ToRight
point3 = adsk.core.Point3D.create(-sheetZ,conerBack-self.wall,offset) #ToLeft
rectangleToCut = lines.addCenterPointRectangle(point1,point2)
rectangleToCut.item(0).deleteMe() #ToFront
rectangleToCut.item(1).trim(point3, False) #Correct ToLeft
rectangleToCut.item(3).trim(point2, False) #Correct ToRight
# sheetZ for front
point1 = adsk.core.Point3D.create(0,-conerBack,offset) #MidPoint
point2 = adsk.core.Point3D.create(sheetZ,-conerBack-self.wall,offset)
point3 = adsk.core.Point3D.create(sheetZ,-conerBack+self.wall,offset) #ToRight
point4 = adsk.core.Point3D.create(-sheetZ,-conerBack+self.wall,offset) #ToLeft
rectangleToCut = lines.addCenterPointRectangle(point1,point2)
rectangleToCut.item(2).deleteMe()
rectangleToCut.item(1).trim(point3, False) #Correct
rectangleToCut.item(3).trim(point4, False) #Correct
# sheetXBase for left
point1 = adsk.core.Point3D.create(-(self.w-self.wall)/2,0,offset)
point2 = adsk.core.Point3D.create((-(self.w-self.wall)/2)-self.wall,sheetXBase,offset)
point3 = adsk.core.Point3D.create(0,-(self.w-self.wall)/2,offset)
rectangleToCut = lines.addCenterPointRectangle(point1,point2)
rectangleToCut.item(1).deleteMe()
rectangleToCut.item(0).trim(point1, False)
rectangleToCut.item(2).trim(point3, False)
# sheetXBase for Right
point1 = adsk.core.Point3D.create((self.w-self.wall)/2,0,offset)
point2 = adsk.core.Point3D.create(((self.w-self.wall)/2)+self.wall,sheetXBase,offset)
point3 = adsk.core.Point3D.create(0,(self.w-self.wall)/2,offset)
rectangleToCut = lines.addCenterPointRectangle(point1,point2)
rectangleToCut.item(1).deleteMe()
rectangleToCut.item(0).trim(point1, False) #Correct
rectangleToCut.item(2).trim(point3, False)
# Trim rest of lines
rightBack = baseBackToCut.item(1).trim(baseBackPoint1) #Correct RIGHT
leftBack = baseBackToCut.item(3).trim(baseBackPoint1) #Correct LEFT
rightFront = baseFrontToCut.item(1).trim(baseFrontPoint1) #Correct RIGHT
leftFront = baseFrontToCut.item(3).trim(baseFrontPoint1) #Correct LEFT
backLine = baseBackToCut.item(2).trim(baseBackPoint0) #Correct BACK
frontLine = baseFrontToCut.item(2).trim(baseFrontPoint0) #Correct FRONT
mill = self.mill
#sketch.sketchPoints.add(baseBackPoint0) #Draw a test dot
if(mill > 0.0):
r=mill/2.0
tmpCenter = adsk.core.Point3D.create(sheetZ+r,conerFront,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(sheetZ+r,conerFront+r,offset))
backLine.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create(-sheetZ-r,conerFront,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(-sheetZ-r,conerFront+r,offset))
backLine.item(1).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create(sheetZ+r,-conerBack,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(sheetZ+r,-conerBack-r,offset))
frontLine.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create(-sheetZ-r,-conerBack,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(-sheetZ-r,-conerBack-r,offset))
frontLine.item(1).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create((self.w-self.wall)/2,sheetXBase+r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create((self.w-self.wall)/2+r,sheetXBase+r,offset))
rightBack.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create((self.w-self.wall)/2,-sheetXBase-r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create((self.w-self.wall)/2+r,-sheetXBase-r,offset))
rightFront.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create(-(self.w-self.wall)/2,sheetXBase+r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(-(self.w-self.wall)/2-r,sheetXBase+r,offset))
leftBack.item(0).trim(tmpCenter)
tmpCenter = adsk.core.Point3D.create(-(self.w-self.wall)/2,-sheetXBase-r,offset)
tmp = sketch.sketchCurves.sketchCircles.addByCenterRadius(tmpCenter,r)
if(toTrim) :
tmp.trim(adsk.core.Point3D.create(-(self.w-self.wall)/2-r,-sheetXBase-r,offset))
leftFront.item(0).trim(tmpCenter)
extrudes = side.features.extrudeFeatures
#prof = sketch.profiles[0]
profs = adsk.core.ObjectCollection.create()
for prof in sketch.profiles:
profs.add(prof)
extrudeInput = extrudes.createInput(profs, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
distExtrude = adsk.core.ValueInput.createByReal(self.wall)
extrudeInput.setDistanceExtent(False, distExtrude)
#Extrude rename
sideExtrude = extrudes.add(extrudeInput)
sideExtrude.name = _name
#Body rename
sideBody = side.bRepBodies.item(side.bRepBodies.count-1)
sideBody.name = _name
if(self.saveDXF):
saveToDXF(sketch, _name)
return sideExtrude
#Save to DXF
def saveToDXF(sketch, name):
# Save to DXF
# For Windows
if(platform.system() == 'Windows'):
#Path to needed folder
path = os.path.expanduser("~\Desktop\\NiceBox360_DXF")
#Check that folder
if not os.path.exists(path):
os.makedirs(path)
# Save sketch to the folder
dxf_path = os.path.join(os.environ['USERPROFILE'], path, name + ".dxf")
sketch.saveAsDXF(dxf_path)
# For Mac
if(platform.system() == 'Darwin'):
#Path to needed folder
path = os.path.expanduser("~/NiceBox360_DXF")
#Check that folder
if not os.path.exists(path):
os.makedirs(path)
# Save sketch to the folder
dxf_path = os.path.join("", path, name + ".dxf")
sketch.saveAsDXF(dxf_path)
def run(context):
ui = None
try:
global app, ui, box
app = adsk.core.Application.get()
ui = app.userInterface
box = BOX()
# Create command defintion
cmdDef = ui.commandDefinitions.itemById(commandId)
if not cmdDef:
cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, 'NiceBox-360', 'Creates a box by your parameters', 'Resources/NiceBox')
createPanel = ui.allToolbarPanels.itemById('SolidCreatePanel')
niceBoxBtn = createPanel.controls.addCommand(cmdDef)
# Connect to the command created event.
onCommandCreated = BoxCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
handlers.append(onCommandCreated)
if context['IsApplicationStartup'] == False:
ui.messageBox('The "NiceBox-360" button has been added\nto the CREATE panel of the MODEL workspace.')
pass
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
#ui = None
try:
createPanel = ui.allToolbarPanels.itemById('SolidCreatePanel')
niceBoxBtn = createPanel.controls.itemById(commandId)
if niceBoxBtn:
niceBoxBtn.deleteMe()
cmdDef = ui.commandDefinitions.itemById(commandId)
if cmdDef:
cmdDef.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))