- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi I've developde a scrip based on @BrianEkins CenterOfRange.
It creates points on each middle face of the bounding box.
I'm trying to transform it into one add-In but I always get fusion to crash.
Could some one look at this and figure out what is wrong?
the code:
# TODO ******************************** Your code here ********************************
# Get all components in the active design.
doc = app.activeDocument
des = doc.products[0]
_ui = app.userInterface
if not des:
_ui.messageBox('No active Fusion design', 'Center of Bounds')
return
# Have a body selected.
bodySelection = _ui.selectEntity('Select the body', 'MeshBodies,Bodies')
if not bodySelection:
return False
drawBox = False
if _ui.messageBox('Draw the bounding box points?', 'Bounding Box Center Points', adsk.core.MessageBoxButtonTypes.YesNoButtonType, adsk.core.MessageBoxIconTypes.QuestionIconType) == adsk.core.DialogResults.DialogYes:
drawBox = True
# Check to see if a b-rep or mesh body was selected.
triMesh = adsk.fusion.TriangleMesh.cast(None)
if isinstance(bodySelection.entity, adsk.fusion.BRepBody):
brepBody = adsk.fusion.BRepBody.cast(bodySelection.entity)
triMesh = brepBody.meshManager.displayMeshes.bestMesh
body = brepBody
elif isinstance(bodySelection.entity, adsk.fusion.MeshBody):
meshBody = adsk.fusion.MeshBody.cast(bodySelection.entity)
triMesh = meshBody.displayMesh
body = meshBody
# Calculate the bounding box of the mesh.
smallPnt = adsk.core.Point3D.cast(triMesh.nodeCoordinates[0])
largePnt = adsk.core.Point3D.cast(triMesh.nodeCoordinates[0])
vertex = adsk.core.Point3D.cast(None)
for vertex in triMesh.nodeCoordinates:
if vertex.x < smallPnt.x:
smallPnt.x = vertex.x
if vertex.y < smallPnt.y:
smallPnt.y = vertex.y
if vertex.z < smallPnt.z:
smallPnt.z = vertex.z
if vertex.x > largePnt.x:
largePnt.x = vertex.x
if vertex.y > largePnt.y:
largePnt.y = vertex.y
if vertex.z > largePnt.z:
largePnt.z = vertex.z
#center Points.
centerPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, (smallPnt.y+largePnt.y)/2, (smallPnt.z+largePnt.z)/2)
centerXMinusPnt = adsk.core.Point3D.create(smallPnt.x, (smallPnt.y+largePnt.y)/2, (smallPnt.z+largePnt.z)/2)
centerXPlusPnt = adsk.core.Point3D.create(largePnt.x, (smallPnt.y+largePnt.y)/2, (smallPnt.z+largePnt.z)/2)
centerYMinusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, smallPnt.y, (smallPnt.z+largePnt.z)/2)
centerYplusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, largePnt.y, (smallPnt.z+largePnt.z)/2)
#center Corner Points.
centerCornerXPlusYPlusPnt = adsk.core.Point3D.create(largePnt.x, largePnt.y, (smallPnt.z+largePnt.z)/2)
centerCornerXPlusYMinusPnt = adsk.core.Point3D.create(largePnt.x, smallPnt.y, (smallPnt.z+largePnt.z)/2)
centerCornerXMinusYPlusPnt = adsk.core.Point3D.create(smallPnt.x, largePnt.y, (smallPnt.z+largePnt.z)/2)
centerCornerXMinusYminusPnt = adsk.core.Point3D.create(smallPnt.x, smallPnt.y, (smallPnt.z+largePnt.z)/2)
#center Top Points.
centerTopPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, (smallPnt.y+largePnt.y)/2,largePnt.z)
centerTopXMinusPnt = adsk.core.Point3D.create(smallPnt.x, (smallPnt.y+largePnt.y)/2, largePnt.z)
centerTopXPlusPnt = adsk.core.Point3D.create(largePnt.x, (smallPnt.y+largePnt.y)/2, largePnt.z)
centerTopYMinusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, smallPnt.y, largePnt.z)
centerTopYplusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, largePnt.y, largePnt.z)
#center Bottom Points.
centerBotomPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, (smallPnt.y+largePnt.y)/2, smallPnt.z)
centerBotomXMinusPnt = adsk.core.Point3D.create(smallPnt.x, (smallPnt.y+largePnt.y)/2, smallPnt.z)
centerBotomXPlusPnt = adsk.core.Point3D.create(largePnt.x, (smallPnt.y+largePnt.y)/2, smallPnt.z)
centerBotomYMinusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, smallPnt.y, smallPnt.z)
centerBotomYplusPnt = adsk.core.Point3D.create((smallPnt.x+largePnt.x)/2, largePnt.y, smallPnt.z)
# Get the root component of the active design
rootComp = des.rootComponent
# Special case for parametric or direct edit design.
if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:
# Check to see if a base feature named "Center of Bounds" exists.
baseFeature = rootComp.features.itemByName('Center of Block')
if not baseFeature:
# Create a new base feature.
baseFeature = rootComp.features.baseFeatures.add()
baseFeature.name = 'Center of Block'
# Begin editing the base feature.
baseFeature.startEdit()
inEdit = True
# Get the full path of the body.
bodyName = getPath(body) + '/' + body.name
# Check to see if there's already a sketch for this body.
skAttribs = des.findAttributes('CenterOfBounds', bodyName)
if len(skAttribs) > 0:
skAttrib = skAttribs[0]
if skAttrib.parent:
sketch = adsk.fusion.Sketch.cast(skAttrib.parent)
# Delete the contents of the sketch.
ents = adsk.core.ObjectCollection.create()
for line in sketch.sketchCurves.sketchLines:
ents.add(line)
for pnt in sketch.sketchPoints:
ents.add(pnt)
des.deleteEntities(ents)
else:
skAttrib.deleteMe()
sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
sketch.baseOrFormFeature = baseFeature
sketch.attributes.add('CenterOfBounds', bodyName, '')
else:
sketch = rootComp.sketches.add(rootComp.xYConstructionPlane)
sketch.name = 'Centre of Block'
if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:
sketch.baseOrFormFeature = baseFeature
sketch.attributes.add('CenterOfBounds', bodyName, '')
sketch.areProfilesShown = False
sketch.areDimensionsShown = True
#center Points.
sketch.sketchPoints.add(centerPnt)
sketch.sketchPoints.add(centerXMinusPnt)
sketch.sketchPoints.add(centerXPlusPnt)
sketch.sketchPoints.add(centerYMinusPnt)
sketch.sketchPoints.add(centerYplusPnt)
#center Corner Points.
sketch.sketchPoints.add(centerCornerXPlusYPlusPnt)
sketch.sketchPoints.add(centerCornerXPlusYMinusPnt)
sketch.sketchPoints.add(centerCornerXMinusYPlusPnt)
sketch.sketchPoints.add(centerCornerXMinusYminusPnt)
#center Top Points.
sketch.sketchPoints.add(centerTopPnt)
sketch.sketchPoints.add(centerTopXMinusPnt)
sketch.sketchPoints.add(centerTopXPlusPnt)
sketch.sketchPoints.add(centerTopYMinusPnt)
sketch.sketchPoints.add(centerTopYplusPnt)
#center Bottom Points.
sketch.sketchPoints.add(centerBotomPnt)
sketch.sketchPoints.add(centerBotomXMinusPnt)
sketch.sketchPoints.add(centerBotomXPlusPnt)
sketch.sketchPoints.add(centerBotomYMinusPnt)
sketch.sketchPoints.add(centerBotomYplusPnt)
if drawBox:
lines = sketch.sketchCurves.sketchLines
pnt1 = adsk.core.Point3D.create(smallPnt.x, largePnt.y, smallPnt.z)
line = lines.addByTwoPoints(smallPnt, pnt1)
line.isConstruction = True
pnt1 = line.endSketchPoint
smallPntSKT =line.startSketchPoint
pnt2 = adsk.core.Point3D.create(smallPnt.x, smallPnt.y, largePnt.z)
line = lines.addByTwoPoints(smallPnt, pnt2)
line.isConstruction = True
pnt2 = line.endSketchPoint
pnt3 = adsk.core.Point3D.create(largePnt.x, smallPnt.y, smallPnt.z)
line = lines.addByTwoPoints(smallPnt, pnt3)
line.isConstruction = True
pnt3 = line.endSketchPoint
pnt4 = adsk.core.Point3D.create(smallPnt.x, largePnt.y, largePnt.z)
line = lines.addByTwoPoints(largePnt, pnt4)
line.isConstruction = True
pnt4 = line.endSketchPoint
pnt5 = adsk.core.Point3D.create(largePnt.x, largePnt.y, smallPnt.z)
line = lines.addByTwoPoints(largePnt, pnt5)
line.isConstruction = True
pnt5 = line.endSketchPoint
pnt6 = adsk.core.Point3D.create(largePnt.x, smallPnt.y, largePnt.z)
line = lines.addByTwoPoints(largePnt, pnt6)
line.isConstruction = True
pnt6 = line.endSketchPoint
line = lines.addByTwoPoints(pnt1, pnt4)
line.isConstruction = True
line = lines.addByTwoPoints(pnt1, pnt5)
line.isConstruction = True
line = lines.addByTwoPoints(pnt2, pnt4)
line.isConstruction = True
line = lines.addByTwoPoints(pnt2, pnt6)
line.isConstruction = True
line = lines.addByTwoPoints(pnt3, pnt5)
line.isConstruction = True
line = lines.addByTwoPoints(pnt3, pnt6)
line.isConstruction = True
if des.designType == adsk.fusion.DesignTypes.ParametricDesignType:
# End the base feature edit.
baseFeature.finishEdit()
def getPath(ent):
path = ''
if ent.assemblyContext:
occ = ent.assemblyContext
while occ:
if path == '':
path = occ.name
else:
path = occ.name + '/' + path
occ = occ.assemblyContext
path = 'Root/' + path
else:
path = 'Root'
return path
Solved! Go to Solution.