AnsweredAssumed Answered

Changing report names in Python tutorial 12

Question asked by kelin84 on Nov 29, 2019
Latest reply on Jan 21, 2020 by CLau-esristaff

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()

 

 

Outcomes