- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Howdy,
I have a script to generate a half sphere and place holes in its surface randomly. It works fine for the first 10-20 holes then throws an error.
Failed:
Traceback (most recent call last):
File "C:/Users/Tim/AppData/Roaming/Autodesk/Autodesk Fusion 360/API/Scripts/Star_form/Star_form.py", line 111, in run
workplane = rootComp.constructionPlanes.add(workplaneinput)
File "C:/Users/Tim/AppData/Local/Autodesk/webdeploy/production/6783e6b71d33852e05099c507cf8d926394ea32c/Api/Python/packages\adsk\fusion.py", line 12795, in add
return _fusion.ConstructionPlanes_add(self, input)
RuntimeError: 2 : InternalValidationError : data_->execute(&obj, apiName) && obj
I'd appreciate any help, the full code is below.
#Author-
#Description-
from tkinter.messagebox import NO
import adsk.core, adsk.fusion, adsk.cam, traceback
import math
import random
id = 300*0.1
wall_thickness = 2 * 0.1
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = app.activeProduct
# Get the root component of the active design.
rootComp = design.rootComponent
# Create a new sketch on the xy plane.
sketches = rootComp.sketches
xyPlane = rootComp.xYConstructionPlane
sketch = sketches.add(xyPlane)
# Draw arcs.
arc = sketch.sketchCurves.sketchArcs
arc1 = arc.addByCenterStartSweep(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(id/2, 0, 0),math.pi)
arc2 = arc.addByCenterStartSweep(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(id/2 + wall_thickness, 0, 0),math.pi)
# Draw lines to close arcs.
lines = sketch.sketchCurves.sketchLines
line_1 = lines.addByTwoPoints(adsk.core.Point3D.create(id/2, 0, 0), adsk.core.Point3D.create(id/2 + wall_thickness, 0, 0))
line_2 = lines.addByTwoPoints(adsk.core.Point3D.create(-id/2, 0, 0), adsk.core.Point3D.create(-id/2 - wall_thickness, 0, 0))
# Draw center line to use as the axis of revolution.
lines = sketch.sketchCurves.sketchLines
axisLine = lines.addByTwoPoints(adsk.core.Point3D.create(-1, 0, 0), adsk.core.Point3D.create(1, 0, 0))
# Get the profile defined by the circle.
prof = sketch.profiles.item(0)
# Create an revolution input to be able to define the input needed for a revolution
# while specifying the profile and that a new body is to be created
revolves = rootComp.features.revolveFeatures
revInput = revolves.createInput(prof, axisLine, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
# Define that the extent is an angle of pi to get half of a sphere.
angle = adsk.core.ValueInput.createByReal(math.pi)
revInput.setAngleExtent(False, angle)
# Create the extrusion.
ext = revolves.add(revInput)
body = ext.bodies[0]
target_face = None
# Create hole on body
basesketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
basesketch.isVisible = False
r = id/2 + wall_thickness
# randomply place pt on half sphere
for i in range(100):
# Get face
for face in body.faces:
surface = adsk.core.Sphere.cast(face.geometry)
if (surface and math.fabs(surface.radius - (id/2 + wall_thickness)) < 1.0e-6 ):
target_face = face
phi = random.random() * math.pi * 2.0
theta = random.random() * math.pi*0.5
x = r * math.sin(theta) * math.cos(phi)
y = r * math.sin(theta) * math.sin(phi)
z = r * math.cos(theta)
# Create hole based on a point
pt = adsk.core.Point3D.create(x, y, z)
basesketchpoint = basesketch.sketchPoints.add(pt)
if(target_face):
# Create plane tangent to sphere
workplaneinput = rootComp.constructionPlanes.createInput()
workplaneinput.setByTangentAtPoint(target_face, basesketchpoint)
workplane = rootComp.constructionPlanes.add(workplaneinput)
# Create hole
holeinput = rootComp.features.holeFeatures.createSimpleInput(adsk.core.ValueInput.createByString('1 cm'))
holeinput.setPositionByPoint(workplane, pt)
holeinput.setDistanceExtent(adsk.core.ValueInput.createByReal(10))
rootComp.features.holeFeatures.add(holeinput)
# Hide workplane
workplane.isLightBulbOn = False
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.