Message 1 of 5
Fusion crashes with a certain model.
Not applicable
10-12-2015
07:15 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Fusion crashes with the model "One building near Ekaterinburg" ( https://fusion360.autodesk.com/projects/one-building-near-ekaterinburg ) after running this code in the third or the fourth time.
If it is clear, please, help with it. Otherwise, I just report this.
import adsk.core, adsk.fusion, traceback
from xml.etree.ElementTree import Element, SubElement, tostring
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
quality = adsk.fusion.TriangleMeshQualityOptions.LowQualityTriangleMesh
root = Element('geometry')
listComp = adsk.core.ObjectCollection.create()
for occ in adsk.fusion.Design.cast(app.activeProduct).rootComponent.allOccurrences:
listComp.add(occ)
listComp.add(adsk.fusion.Design.cast(app.activeProduct).rootComponent)
for l in listComp:
if l.classType() == adsk.fusion.Occurrence.classType():
bodies = l.component.bRepBodies
matrix = GetMatrix(l)
if l.classType() == adsk.fusion.Component.classType():
bodies = l.bRepBodies
matrix = adsk.core.Matrix3D.create()
for body in bodies:
if body.isVisible:
for face in body.faces:
meshCalc = face.meshManager.createMeshCalculator()
meshCalc.setQuality(quality)
triangleMesh = meshCalc.calculate()
if triangleMesh:
coordinates = triangleMesh.nodeCoordinates
indices = triangleMesh.nodeIndices
normalVectors = triangleMesh.normalVectorsAsDouble
mesh = SubElement(root, 'mesh')
materialXML = SubElement(mesh, 'material')
color = SubElement(materialXML, 'color')
materialID = SubElement(materialXML, 'id')
r = SubElement(color, 'R')
g = SubElement(color, 'G')
b = SubElement(color, 'B')
opacity = SubElement(color, 'opacity')
shininess = SubElement(materialXML, 'shininess')
reflective = SubElement(materialXML, 'reflective')
transparency = SubElement(materialXML, 'transparency')
properties = body.appearance.appearanceProperties
if properties:
colorprop = properties.itemById('metal_f0')
if not colorprop:
colorprop = properties.itemById('opaque_albedo')
if not colorprop:
colorprop = properties.itemById('transparent_color')
if not colorprop:
colorprop = properties.itemById('concrete_color')
if colorprop and colorprop.value:
materialID.text = str(body.appearance.id)+"-"+str(colorprop.value.red)+str(colorprop.value.green)+str(colorprop.value.blue)
r.text = str(colorprop.value.red)
g.text = str(colorprop.value.green)
b.text = str(colorprop.value.blue)
opacity.text = str(colorprop.value.opacity)
shinprop = properties.itemById('surface_roughness')
if shinprop and shinprop.value:
shininess.text = str(shinprop.value)
reflprop = properties.itemById('opaque_f0')
if reflprop and reflprop.value:
reflective.text = str(reflprop.value)
transpprop = properties.itemById('transparent_ior')
if transpprop and transpprop.value:
transparency.text = str(transpprop.value)
vertices = SubElement(mesh, 'vertices')
for c in coordinates:
vertice = SubElement(vertices, 'vertice')
c.transformBy(matrix)
x = SubElement(vertice, 'x')
x.text = str(c.x/-100)
y = SubElement(vertice, 'y')
y.text = str(c.z/100)
z = SubElement(vertice, 'z')
z.text = str(c.y/100)
normals = SubElement(mesh, 'normals')
nNormals = 0
for c in coordinates:
normal = SubElement(normals, 'normal')
x = SubElement(normal, 'x')
x.text = str(normalVectors[nNormals*3])
y = SubElement(normal, 'y')
y.text = str(normalVectors[nNormals*3+1])
z = SubElement(normal, 'z')
z.text = str(normalVectors[nNormals*3+2])
nNormals+=1
triangles = SubElement(mesh, 'triangles')
for t in range(triangleMesh.triangleCount):
triangle = SubElement(triangles, 'triangle')
a = SubElement(triangle, 'A')
a.text = str(indices[t*3])
b = SubElement(triangle, 'B')
b.text = str(indices[t*3+1])
c = SubElement(triangle, 'C')
c.text = str(indices[t*3+2])
xml_str = tostring(root)
with open('D:\\ctetemptriangle.xml', 'wb') as f:
f.write(xml_str)
ui.messageBox('Done')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def GetMatrix(occurrence):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
matrix = occurrence.transform
while occurrence.assemblyContext:
matrix.transformBy(occurrence.assemblyContext.transform)
occurrence = occurrence.assemblyContext
return matrix
except:
if ui:
ui.messageBox('Get Matrix failed:\n{}'.format(traceback.format_exc()))
