@FusionMaxR .
I have thought of an alternative, but it is incomplete because it is an error in direct mode.
# Fusion360API Python script
import traceback
import adsk.core
import adsk.fusion
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
design: adsk.fusion.Design = app.activeProduct
rootComp = design.rootComponent
body: adsk.fusion.BRepBody = rootComp.bRepBodies[0]
splittingTool: adsk.fusion.Profile = rootComp.sketches[0].profiles[0]
exec_splitBody_by_profile_like(body, splittingTool)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def exec_splitBody_by_profile_like(
body: adsk.fusion.BRepBody,
prof: adsk.fusion.Profile,
) -> None:
def get_min_max_length(
body: adsk.fusion.BRepBody,
vector: adsk.core.Vector3D,
sktPlane: adsk.core.Plane,
) -> list[float, float]:
measMgr: adsk.core.MeasureManager = app.measureManager
bBox: adsk.core.OrientedBoundingBox3D = measMgr.getOrientedBoundingBox(
body,
vector,
get_perpendicular_vector(vector),
)
offset = bBox.length * 0.5
bBoxPoints = []
for l in [offset, -offset]:
vec: adsk.core.Vector3D = vector.copy()
vec.scaleBy(l)
pnt: adsk.core.Point3D = bBox.centerPoint.copy()
pnt.translateBy(vec)
bBoxPoints.append(pnt)
infLine: adsk.core.InfiniteLine3D = adsk.core.InfiniteLine3D.create(
bBox.centerPoint,
vector,
)
origin: adsk.core.Point3D = sktPlane.intersectWithLine(infLine)
lengths = []
for p in bBoxPoints:
dist = origin.distanceTo(p)
vec = origin.vectorTo(p)
vec.normalize()
if vector.isEqualTo(vec):
dist *= -1
lengths.append(dist)
return lengths
def get_perpendicular_vector(
vector: adsk.core.Vector3D,
) -> adsk.core.Vector3D:
v: adsk.core.Vector3D = adsk.core.Vector3D.create(1,0,0)
if vector.isParallelTo(v):
v = adsk.core.Vector3D.create(0,1,0)
return vector.crossProduct(v)
def create_extrude(
prof: adsk.fusion.Profile,
lengths: list,
)-> adsk.fusion.ExtrudeFeature:
comp: adsk.fusion.Component = prof.parentSketch.parentComponent
extrudeFeats: adsk.fusion.ExtrudeFeatures = comp.features.extrudeFeatures
extrudeIpt: adsk.fusion.ExtrudeFeatureInput = extrudeFeats.createInput(
prof,
adsk.fusion.FeatureOperations.NewBodyFeatureOperation,
)
extrudeIpt.setTwoSidesExtent(
adsk.fusion.DistanceExtentDefinition.create(
adsk.core.ValueInput.createByReal(lengths[0])
),
adsk.fusion.DistanceExtentDefinition.create(
adsk.core.ValueInput.createByReal(lengths[1])
),
)
extrudeIpt.isSolid = False
return extrudeFeats.add(extrudeIpt)
def split_body(
targetBody: adsk.fusion.BRepBody,
toolBody: adsk.fusion.BRepBody,
) -> adsk.fusion.SplitBodyFeature:
comp: adsk.fusion.Component = targetBody.parentComponent
splitBodyFeats: adsk.fusion.SplitBodyFeatures = comp.features.splitBodyFeatures
splitIpt: adsk.fusion.SplitBodyFeatureInput = splitBodyFeats.createInput(
targetBody,
toolBody,
True,
)
return splitBodyFeats.add(splitIpt)
def remove_body(
targetBody: adsk.fusion.BRepBody,
) -> adsk.fusion.RemoveFeature:
comp: adsk.fusion.Component = targetBody.parentComponent
removeFeats: adsk.fusion.RemoveFeatures = comp.features.removeFeatures
return removeFeats.add(targetBody)
# *******
app: adsk.core.Application = adsk.core.Application.get()
des: adsk.fusion.Design = app.activeProduct
if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:
tl: adsk.fusion.Timeline = des.timeline
startPos = tl.markerPosition
skt: adsk.fusion.Sketch = prof.parentSketch
minMaxLength = get_min_max_length(
body,
skt.xDirection.crossProduct(skt.yDirection),
skt.referencePlane.geometry
)
extrudeFeat: adsk.fusion.ExtrudeFeature = create_extrude(
prof,
minMaxLength,
)
split_body(
body,
extrudeFeat.bodies[0]
)
remove_body(extrudeFeat.bodies[0])
if tl:
try:
tl.timelineGroups.add(startPos, tl.markerPosition -1)
except:
pass
When I do SplitBody manually, it seems to be responding to the profile loop rather than responding to the profile.
However, the error also occurred in the profile loop.