#Author-Sam DeRose
#Description-Auto-generates a greenhouse design based on your specific environment and needs. 

import adsk.core, adsk.fusion
import urllib.request
#from html.parser import HTMLParser
import xml.etree.ElementTree as ET


############### PULL INPUTS FROM XML DATABASE 

response = urllib.request.urlopen('http://samderose.netau.net/greenhouse_database.xml')
pageContentsBytes = response.read()
pageContents = pageContentsBytes.decode("utf-8") # convert byte object to string

tree = ET.fromstring(pageContents)
locationName = tree.find('name1').text
outsideTemp = int(tree.find('temp1').text)
flux = int(tree.find('flux1').text) 

print('Location: ', locationName)
print('Outside Temp: ', outsideTemp, ' F')
print('Solar Heat Flux: ', flux, ' Btu/hr/ft^2')


############### VARIABLE DEFINITIONS

#### PREFERED TEMP
# Fahrenheit - F 
insideTemp = 100

# units in feet
wallHeight = 6
roofHeight = 9
width = 10
length = 10

# default insulation thickness, in cm
insulation = 10 # cm!!!!
efficiency = 0.1
# calculate the areas, in ft^2
floorArea = (width*length)
sideArea = 2*(length*wallHeight)
frontBackArea = 2*(width*wallHeight) + ((roofHeight-wallHeight)*width)
roofArea = 2*(((roofHeight-wallHeight)**2 + (width/2)**2)**0.5)*length

totalArea = floorArea + sideArea + frontBackArea + roofArea
sunArea = roofArea  #### Area in the sun is assumed to be the roof area!!


print('Floor Area: ', floorArea, ' ft^2')
print('Total Area: ', totalArea, ' ft^2')
print('Sun Area: ', sunArea, ' ft^2')

# convert ft to cm
wallHeight = wallHeight * 30.48
roofHeight = roofHeight * 30.48
width = width * 30.48
length = length * 30.48


# flux is measured in Btu/hr/ft^2
insulation = (totalArea * (insideTemp - outsideTemp)) / (sunArea * efficiency * flux * 6.5)
print('Insulation Thickness: ', insulation, ' inches')

insulation = insulation * 2.54 # convert inches to cm!!



############### SETUP

app = adsk.core.Application.get()
ui = app.userInterface

# this line makes a new document file, uncomment to allow script to run in current file
#doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
design = app.activeProduct

# Get the root component of the active design.
rootComp = design.rootComponent


############### SKETCH 1

# Create a new sketch on the xy plane.
sketches = rootComp.sketches;
xyPlane1 = rootComp.xYConstructionPlane
sketch1 = sketches.add(xyPlane1)

# Draw two connected lines.
lines = sketch1.sketchCurves.sketchLines;
line1 = lines.addByTwoPoints(adsk.core.Point3D.create(0, 0, 0), adsk.core.Point3D.create(width, 0, 0))
line2 = lines.addByTwoPoints(line1.endSketchPoint, adsk.core.Point3D.create(width, wallHeight, 0))
line3 = lines.addByTwoPoints(line2.endSketchPoint, adsk.core.Point3D.create(width/2, roofHeight, 0))
line4 = lines.addByTwoPoints(line3.endSketchPoint, adsk.core.Point3D.create(0, wallHeight, 0))
line5 = lines.addByTwoPoints(line4.endSketchPoint, adsk.core.Point3D.create(0, 0, 0))



############### EXTRUDE SKETCH 1


# Get the profile defined by the circle
prof1 = sketch1.profiles.item(0)

# Create an extrusion input
extrudes1 = rootComp.features.extrudeFeatures
extInput = extrudes1.createInput(prof1, adsk.fusion.FeatureOperations.NewBodyFeatureOperation) # extInput defines the parameters for the extrude

# Define that the extent is a distance extent of LENGTH
distance = adsk.core.ValueInput.createByReal(length)
# Set the distance extent to be one-sided
extInput.setDistanceExtent(False, distance)
# Set the extrude to be a solid one
extInput.isSolid = True

# Create the extrusion
ext1 = extrudes1.add(extInput) # ext is the extruded feature




############### SHELL EXTRUSION 1

entities1 = adsk.core.ObjectCollection.create()
entities1.add(ext1.bodies.item(0)) # grab the body to do a body shell
 
features = rootComp.features
shellFeats = features.shellFeatures
isTangentChain = False
shellFeatureInput = shellFeats.createInput(entities1, isTangentChain)
thickness = adsk.core.ValueInput.createByReal(insulation)
shellFeatureInput.insideThickness = thickness
shellFeats.add(shellFeatureInput)








#################### DEFAULT STARTER CODE ##########################

#def run(context):
#    ui = None
#    try:
#        app = adsk.core.Application.get()
#        ui  = app.userInterface
#        ui.messageBox('Hello script')
#
#    except:
#        if ui:
#            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
