Changing report names in Python tutorial 12

268
3
11-29-2019 01:45 PM
KennethLindhardt1
Occasional Contributor

Hi, I’m stuck in the CE Python stuff. I need to create a semi colon separated list in a text file summed by the start shape.

So exactly like in Tutorial 12, I’m not getting any errors anymore on export, but as soon as I replace the names from the tutorial 12 to my own names in the reports, it’s not writing the text file any more. As soon as I misspell something in my report, it fails, so I guess I’m getting my strings correct. I also did changed the report names in the tutorial 12, to match my names, and as soon as I do that, it runs, but it don’t write the file.

Anyone knows what going on? Thanks

'''
Created on 29/11/2019

@author: Kenneth Lindhardt
'''
from scripting import *

# Get a CityEngine instance
ce = CE()

# Globals
gInstanceData = "" # global string that collects all data to be written
gInstanceCount = 0 # global count to enumerate all instances

# Called for each initial shape after generation.
def finishModel(exportContextOID, shapeOID, modelOID:(
global gInstanceData, gInstanceCount
model = Model(modelOID)
if(model.getReports().has_key('Bygningsanvendelse')): # only write entry if report data available
l = len(model.getReports()['Bygningsanvendelse']) # there might be more than one asset per model, therefore loop
for i in range(0,l):
instanceData = processInstance(model.getReports(),gInstanceCount, i-1)
gInstanceData = gInstanceData+instanceData
gInstanceCount = gInstanceCount+1
# Called after all initial shapes are generated.
def finishExport(exportContextOID:(
global gInstanceData, gInstanceCount
## path of the output file
file = ce.toFSPath("aseets")+"/instanceMapHaps.txt"
## write collected data to file
# writeFile(file, gInstanceData)
# print str(gInstanceCount)+" instances written to "+file +"\n"
''' Collect the instance information in a tab-separated string'''
def processInstance(reports, count, index:(
## remove path from asset string
asset = reports['Bygningsanvendelse'][index] # Overordnet rapport
asset = asset.rpartition("/")[2]
## prepare the string for the instance map
text = "%d\t" % count;
text += "%s\t" % asset;
text += "%s\t%s\t%s\n" % (reports['Bruttoetageareal (BGF)'][index], reports['Bygningens grundareal'][index], reports['Antal etager (bygninger)'][index])
#text += "%.3f\t%.3f\t%.3f\t" % (reports['xrot'][index], reports['yrot'][index], reports['zrot'][index])
# text += "%.3f\t%.3f\n" % (reports['Tag'][index], reports['Bygningens grundareal'][index]) #Husk at fjerne efter text += Hvis der ændres
return text

''' combining data (header and content) and writing file '''
def writeFile(file, content:(

## prepare the head string
head = "nr\tasset\tBruttoetageareal (BGF)\tBygningens grundareal\tAntal etager (bygninger)\n"
## combine header and content
content = head + content
# write data to the file
report = open(file, "w")
report.write(content)
report.close()

0 Kudos
3 Replies
KennethLindhardt1
Occasional Contributor

I’ve changed the way reports where created in CE, and I got it to work, more or less.

How do I access the sum in the report, right now I’m only getting the average?

0 Kudos
KennethLindhardt1
Occasional Contributor

Correction, it seems that it is using the min field from the reports.

0 Kudos
CherylLau
Esri Regular Contributor

In the code above, there is a mistake in the output filepath definition.  ce.toFSPath() needs a valid folder name.  I think you meant:

file = ce.toFSPath("assets")+"/instanceMapHaps.txt"

Once that is changed, your script seems to work.

As for the reported values, every shape in the scene reports its own value, and this is what is written to the output file.  No averages or mins are calculated.  If you run the script on three buildings, your output file will have three rows (plus the header), one row for each building.  For each building, its stats (i.e. usage, GFA, footprint area, number of floors) are written.

To get the sum, you could keep a running sum of the desired report similar to how the running count is kept with gInstanceCount.