#Rhino script for finding weight of object and adding File information, mostly by Mitch Heynick
# -*- coding: utf-8 -*-

import rhinoscriptsyntax as rs
import scriptcontext as sc
import datetime, time;  # This is required to include time module.
import Rhino

def CalculateObjectPrice():
	#constants
	unitsFactor = .001
	tHt = 1.25 #This is to set text height

	objs=rs.GetObjects("Select Objects for volume", filter=16+1073741824, group=True, preselect=True, select=True)
	if not objs: return
	totalV=0
	for obj in objs:
		if rs.IsObjectSolid(obj):
			vol=rs.SurfaceVolume(obj)
			if vol:
				totalV+=vol[0]
	#bail if there is 0 volume
	if totalV==0: return

	#create an empty dictionary
	densDict={}
	#fill the dictionary with entries
	#NO SPACES in material names for GetString!
	#can add as many more as you want
	densDict['Gold_24K']=19.3
	densDict['Gold_18K']=15.5
	densDict['Gold_14K']=13.5
	densDict['Silver']=10.3
	densDict['Platinum']=21.45
	densDict['Plutonium']=19.8
	densDict['Cubic_Zirconia']=5.8
	densDict['White_Bronze']=8.166
	densDict['Diamond']=3.52
	densDict["Unobtanium"]=100.00
	densDict['Agate_Turquoise']=2.60
	densDict['Amber']=1.07

	#gets a list of all the dictionary keys (not ordered!)
	metaList=densDict.keys()
	#sorts the list for easier reading in next step
	metaList.sort()

	#Get a string from the command line
	mType=rs.GetString("Select Metal Type",strings=metaList)
	if not mType: return
	if not densDict.has_key(mType):
		print "Invalid material type!"
		return

	#get the density corresponding to chosen metal from dictionary
	density=densDict[mType]
	#Calculate total weight of object(s)
	grams = totalV * unitsFactor * density
	# material percentage to remove
	remove = raw_input('Enter Percentage to Remove : ')
	toRemove = float(remove)
	finished = grams - (grams * toRemove)

	#Price of materials is set here
	price = (finished * 40.35)

	text = rs.EditBox(message="Enter Information")

	designer = raw_input('Enter Designers Initials: ')
	fileBy = raw_input('Enter Rhino Done By Initials: ')
	build_for = raw_input('Enter Factory Name: ')

	point = rs.GetPoint("Point for text insertion")
	if point:
	    tObjs=[]
	    #create your text string
	textString="Calculated {} Weight\r\n".format(mType)
	textString+="{:.3f} grams in Rhino\r\n".format(grams)
	textString+="{:.3f} grams finished\r\n".format(finished)
	textString+=" \r\n"
	textString+="Cost for {} is ${:.2f} US,\r\n".format(mType,price)
	textString+="based on 40.35$ per gram.\r\n"
	textString+=" \r\n"
	textString+="{}\r\n".format(text)
	textString+=" \r\n"
	textString+="Designer: {}\r\n".format(designer)
	textString+="Rhino: {}\r\n".format(fileBy)
	textString+="Factory: {}\r\n".format(build_for)
	textString+="© CompanyName Inc. {}".format(datetime.date.today())

	tObjs.append(rs.AddText(textString,point,tHt))

	notes=textString
	sc.doc.Notes=notes #Copies text string to Notes Panel

CalculateObjectPrice()
