Skip to content

Instantly share code, notes, and snippets.

@wangjiezhe
Last active November 18, 2024 11:01
Show Gist options
  • Save wangjiezhe/300f0d8483e4c2de927976248121d28e to your computer and use it in GitHub Desktop.
Save wangjiezhe/300f0d8483e4c2de927976248121d28e to your computer and use it in GitHub Desktop.
使用 Python 在 Fusion 360 中创建 Wifi 标志
import math
import traceback
import adsk.cam
import adsk.core
import adsk.fusion
heightDefault = "100 mm"
thickDefault = "5 mm"
## 草图 - 圆心圆弧槽
def addByCenterStartSweepSlot(sketch, centerPoint, startPoint, sweepAngle, slotWidth):
arcs = sketch.sketchCurves.sketchArcs
helparc = arcs.addByCenterStartSweep(centerPoint, startPoint, sweepAngle)
helparc.isConstruction = True
helparc_radius = centerPoint.distanceTo(startPoint)
half_width = slotWidth / 2
slotVec = centerPoint.vectorTo(startPoint)
slotVec.scaleBy(half_width / helparc_radius)
startPoint.translateBy(slotVec)
outerarc = arcs.addByCenterStartSweep(centerPoint, startPoint, sweepAngle)
slotVec.scaleBy(-2)
startPoint.translateBy(slotVec)
innerarc = arcs.addByCenterStartSweep(centerPoint, startPoint, sweepAngle)
arcs.addByCenterStartEnd(
helparc.startSketchPoint,
innerarc.startSketchPoint,
outerarc.startSketchPoint,
)
arcs.addByCenterStartEnd(
helparc.endSketchPoint,
outerarc.endSketchPoint,
innerarc.endSketchPoint,
)
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design = adsk.fusion.Design.cast(app.activeProduct)
unitsMgr = design.unitsManager
## 输入参数:总高度
(res, isCancelled) = ui.inputBox(
"Enter the height value", "Parameter - Height", heightDefault
)
if isCancelled:
return
else:
height = unitsMgr.evaluateExpression(res)
## 输入参数:厚度
(res, isCancelled) = ui.inputBox(
"Enter the thick value", "Parameter - Thick", thickDefault
)
if isCancelled:
return
else:
thickness = unitsMgr.evaluateExpression(res)
## 根零部件
rootComp = design.rootComponent
## 新建零部件
newComp = rootComp.occurrences.addNewComponent(adsk.core.Matrix3D.create()).component
## 草图
sketches = newComp.sketches
## 新建平面
xyPlane = newComp.xYConstructionPlane
## 新建草图
sketch = sketches.add(xyPlane)
## 中心圆
originPoint = adsk.core.Point3D.create(0, 0, 0)
half_width = height / 16
radius = half_width * 2
sketch.sketchCurves.sketchCircles.addByCenterRadius(originPoint, radius)
## 三个弧形槽
for i in range(1, 4):
radius = half_width * (1 + 4 * i)
startAngle = math.radians(45)
sweepAngle = math.radians(90)
x = radius * math.cos(startAngle)
y = radius * math.sin(startAngle)
startPoint = adsk.core.Point3D.create(x, y, 0)
addByCenterStartSweepSlot(sketch, originPoint, startPoint, sweepAngle, half_width * 2)
for prof in sketch.profiles:
extrudes = newComp.features.extrudeFeatures
extrudes.addSimple(
prof,
adsk.core.ValueInput.createByReal(thickness),
adsk.fusion.FeatureOperations.NewBodyFeatureOperation,
)
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