Skip to content

Instantly share code, notes, and snippets.

@RyoKosaka
Last active December 23, 2017 16:45
Show Gist options
  • Save RyoKosaka/861a147c305d57637311c47ebc60e522 to your computer and use it in GitHub Desktop.
Save RyoKosaka/861a147c305d57637311c47ebc60e522 to your computer and use it in GitHub Desktop.
#Author-Ryo Kosaka
#Description-Create Pad
import adsk.core, adsk.fusion, traceback
import math
defaultPadName = 'Pad'
defaultFilletRadius = 2
defaultShellHeight = 5
defaultRimOut = 23.6
defaultRimIn = 20.7
defaultNumHole = 6
defaultRimHeight = 1.1
# 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 createNewComponent():
# Get the active design.
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
rootComp = design.rootComponent
allOccs = rootComp.occurrences
newOcc = allOccs.addNewComponent(adsk.core.Matrix3D.create())
return newOcc.component
class PadCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
unitsMgr = app.activeProduct.unitsManager
command = args.firingEvent.sender
inputs = command.commandInputs
pad = Pad()
for input in inputs:
if input.id == 'padName':
pad.boltName = input.value
elif input.id == 'rimOut':
pad.rimOut = unitsMgr.evaluateExpression(input.expression, "cm")
elif input.id == 'rimIn':
pad.rimIn = unitsMgr.evaluateExpression(input.expression, "cm")
elif input.id == 'rimHeight':
pad.rimHeight = unitsMgr.evaluateExpression(input.expression, "cm")
elif input.id == 'numHole':
pad.numHole = unitsMgr.evaluateExpression(input.expression)
elif input.id == 'filletRadius':
pad.filletRadius = adsk.core.ValueInput.createByString(input.expression)
pad.buildPad();
args.isValidResult = True
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class PadCommandDestroyHandler(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()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class PadCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
cmd.isRepeatable = False
onExecute = PadCommandExecuteHandler()
cmd.execute.add(onExecute)
onExecutePreview = PadCommandExecuteHandler()
cmd.executePreview.add(onExecutePreview)
onDestroy = PadCommandDestroyHandler()
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('padName', 'Pad Name', defaultPadName)
initRimIn = adsk.core.ValueInput.createByReal(defaultRimIn)
inputs.addValueInput('rimIn', 'Rim Inner', 'cm', initRimIn)
initRimOut = adsk.core.ValueInput.createByReal(defaultRimOut)
inputs.addValueInput('rimOut', 'Rim Outer', 'cm', initRimOut)
initRimHeight = adsk.core.ValueInput.createByReal(defaultRimHeight)
inputs.addValueInput('rimHeight', 'Rim Height', 'cm', initRimHeight)
initShellHeight = adsk.core.ValueInput.createByReal(defaultShellHeight)
inputs.addValueInput('shellHeight', 'Shell Height', 'cm', initShellHeight)
initFilletRadius = adsk.core.ValueInput.createByReal(defaultFilletRadius)
inputs.addValueInput('filletRadius', 'Fillet Radius', 'cm', initFilletRadius)
initNumHole = adsk.core.ValueInput.createByReal(defaultNumHole)
inputs.addValueInput('numHole', 'Number of Hole', 'cm', initNumHole)
#ここのcm消したい
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class Pad:
def __init__(self):
self._padName = defaultPadName
self._filletRadius = adsk.core.ValueInput.createByReal(defaultFilletRadius)
self._rimOut = defaultRimOut
self._rimIn = defaultRimIn
self._rimHeight = defaultRimHeight
self._numHole = defaultNumHole
#properties
@property
def padName(self):
return self._padName
@padName.setter
def padName(self, value):
self._padName = value
@property
def rimOut(self):
return self._rimOut
@rimOut.setter
def rimOut(self, value):
self._rimOut = value
@property
def rimIn(self):
return self._rimIn
@rimIn.setter
def rimIn(self, value):
self._rimIn = value
@property
def rimHeight(self):
return self._rimHeight
@rimHeight.setter
def rimHeight(self, value):
self._rimHeight = value
@property
def numHole(self):
return self._numHole
@numHole.setter
def numHole(self, value):
self._numHole = value
@property
def filletRadius(self):
return self._filletRadius
@filletRadius.setter
def filletRadius(self, value):
self._filletRadius = value
def buildPad(self):
global newComp
newComp = createNewComponent()
if newComp is None:
ui.messageBox('New component failed to create', 'New Component Failed')
return
# Create a new sketch.
sketches = newComp.sketches
xyPlane = newComp.xYConstructionPlane
xzPlane = newComp.xZConstructionPlane
#リムの内径、外径の円、でっぱり部分の円スケッチを描く
rimSketch = sketches.add(xyPlane)
center = adsk.core.Point3D.create(0, 0, 0)
rimSketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.rimOut/2)
rimSketch.sketchCurves.sketchCircles.addByCenterRadius(center, self.rimIn/2)
smallCenter = adsk.core.Point3D.create(0, self.rimOut/2, 0)
rimSketch.sketchCurves.sketchCircles.addByCenterRadius(smallCenter, 0.75)
#リムのスケッチ選択して押し出し
extrudes = newComp.features.extrudeFeatures
prof1 = rimSketch.profiles[1]
prof2 = rimSketch.profiles[3]
extInput1 = extrudes.createInput(prof1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
extInput2 = extrudes.createInput(prof2, adsk.fusion.FeatureOperations.JoinFeatureOperation)
distance = adsk.core.ValueInput.createByReal(self.rimHeight)
extInput1.setDistanceExtent(False, distance)
extInput2.setDistanceExtent(False, distance)
headExt1 = extrudes.add(extInput1)
headExt2 = extrudes.add(extInput2)
#でっぱり部分の押し出し
prof3 = rimSketch.profiles[2]
extInput3 = extrudes.createInput(prof3, adsk.fusion.FeatureOperations.JoinFeatureOperation)
extInput3.setDistanceExtent(False, distance)
headExt3 = extrudes.add(extInput3)
#でっぱり部分のフィレット
faceEdges = headExt3.sideFaces.item(0)
if faceEdges.edges.count == 2:
faceEdges = headExt3.sideFaces.item(1)
edgeCol = adsk.core.ObjectCollection.create()
for edge in faceEdges.edges:
if isinstance(edge.geometry, adsk.core.Line3D):
edgeCol.add(edge)
filletFeats = newComp.features.filletFeatures
filletInput = filletFeats.createInput()
filletInput.addConstantRadiusEdgeSet(edgeCol, self.filletRadius, True)
baseFillet = filletFeats.add(filletInput)
#穴あけ
holeSketch = sketches.add(xyPlane)
holeCenter = adsk.core.Point3D.create(0, self.rimIn/2 + 1.1, 0)
holeSketch.sketchCurves.sketchCircles.addByCenterRadius(holeCenter, 0.65/2)
hole = holeSketch.profiles[0]
extInput4 = extrudes.createInput(hole, adsk.fusion.FeatureOperations.CutFeatureOperation)
extInput4.setDistanceExtent(False, distance)
headExt4 = extrudes.add(extInput4)
#でっぱりと穴のパターン配置
circularPatterns = newComp.features.circularPatternFeatures
entities = adsk.core.ObjectCollection.create()
entities.add(headExt3)
if baseFillet:
entities.add(baseFillet)
entities.add(headExt4)
face = headExt2.sideFaces.item(0)
patternInput = circularPatterns.createInput(entities, face)
numHoleInput = adsk.core.ValueInput.createByString(str(self.numHole))
patternInput.quantity = numHoleInput
pattern = circularPatterns.add(patternInput)
def run(context):
try:
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
if not design:
ui.messageBox('It is not supported in current workspace, please change to MODEL workspace and try again.')
return
commandDefinitions = ui.commandDefinitions
#check the command exists or not
cmdDef = commandDefinitions.itemById('Pad')
if not cmdDef:
cmdDef = commandDefinitions.addButtonDefinition('Pad',
'Create Pad',
'Create a pad.') # relative resource file path is specified
onCommandCreated = PadCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
# keep the handler referenced beyond this function
handlers.append(onCommandCreated)
inputs = adsk.core.NamedValues.create()
cmdDef.execute(inputs)
# prevent this module from being terminate when the script returns, because we are waiting for event handlers to fire
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment