# 2006-03-23
# Name :		Door Maker
# Description :		Makes doors with 4 copies of the leaf on 4 layers
#			Optional Kicker(s),Name Plate(s), Handles(s)
# Author :		PKR
# Usage :		Pick 'Door Maker' from 'Plug in Menu' menu,fill in the details,
#			and click anywhere over & over & over.....
#			
# Notes :
#			All variable specified at begining of script
#			Material colours and opacity specified in dm_add_paint at end of script
#			All Dims in mm
#
# Date :		2006-03-28
#
# To Be Done :
#			1. Door & a half
#			2. error checking, i.e. no Vision panel of leaf is too narrow
#
# History:
#			V 1.0	2006-03-28
#				First release - Only produces single doors
#			V 1.1	2006-03-29
#				Renamed all routines to start with 'dm_'
#				Removed unnecessary '_' from variable names
#				Groups have names assigned as they are created
#				Store & Restore users current layer at begining & end of main routine
#				Replaced If's with Case/When's
#				Turned the whole thing in to a class
#				Place door(s) anywhere
#			v 1.2	2006-03-31
#				Vision panel added
#				Individual materials only created if needed
#				Redesigned the panic bar to be more representational
#			v 1.3	2006-04-02
#				Longbar and vision panel no longer over lap
#				Changed the valables to non persistant (@'s) apart from the ones the user
#				picks in the dialog, these remain persistant (@'s)
#				Leaf now has its own origin and size variables ( step one to double doors)
#				Fixed 'Inward and 'Outward' descriptions - they were all over the place!
#				Double door option added to dialog, script now creates a leaf HALF the opening width ( step two to double doors)
#				Nested grouping working (thanks TIG)
#				Double doors working!!!
#			v 1.4	2006-04-03
#				Fixed a problem with the copied leaf's group being on the wrong layer
#			v 1.5	2006-04-03
#				Vision panel now disabled friendly, the bottom of the VP is the set to the bottom of the pushplate value (thanks TIG)
#			v 1.6	2006-04-03
#				Changed a couple of the default values
#				Fixed Major fault in the panic bolt 
#			v 1.7	2006-04-03
#				Changed dialog drop down list "Vision Panel" to "Panel"
#				Added 1x1 and 1x2 full size glass panels
#			v 1.8	2006-04-05
#				Renamed some variable AGAIN!!
#				Added some error handling Nameplate only if.... etc
#				Added solid panels to the 1x1 and 1x2 glass routines by NOT punching the holes
#				Door & a Half added - 4 sizes of master leaf
#			v 1.9	2006-04-07
#				Renamed variables  - Arrays = a_   Strings = s_    Values = v_    Dialog Items = d_
#				Restructured the code to replace the long 'x=a+b+c+d+e, y=a+b+c+d+e, z=a+b+c+d+e' with the array/clone/+= arrangement (Thanks Remi)
#				Added option to display 'Double Swing' to Swing Arc Style
#			v 2.0	2006-04-14
#				Added new profile to frame - 'Rebated + Architrave'
#				Added settable offset for leaf(s) - centred or flush with frame - default is flush
#				Added 'Port Hole' to the available panels
#
# v2.1 20140312 TIG: made compliant with v2013 Ruby2.0
#
#	INWARD OPENING SIDE
#	  ++ +
#	  ++  ++
#	  ++    ++
#	  ++      ++
#	  ++        +
#	  ++         ++
#	  ++          +
#	  ++          ++
#	++              ++
#	+++            +++
#	+++            +++
#       OUTWARD OPENING SIDE
#
#
#	WHEN DEFINING POINT (IN PLAN OR ELEVATION]
#	THEY ARE DESCRIBED CLOCKWISE STARTING FROM BOTTOM LEFT
#
#		1++++++2	1+
#		+      +	+ ++
#	E.G.	+      +	+   ++
#		+      +	+     ++
#		0++++++3	0+++++++2
#

#-----------------------
require 'sketchup.rb'
#-----------------------
module PaulRussam
#-----------------------------------------------------------------------------------------------
class DM_Create		#### START OF CLASS ####
#-----------------------------------------------------------------------------------------------

@@s_LayerRoot=nil
@@s_DoorType=nil
@@v_FWidth=nil
@@v_FHeight=nil
@@v_FDepth=nil
@@v_FThick=nil
@@s_FStyle=nil
@@v_FRebate=nil
@@v_LDepth=nil
@@s_DamdH_W1=nil
@@s_DamdH_W2=nil
@@s_DamdH_W3=nil
@@s_DamdH_W4=nil
@@s_LOffset=nil
@@s_PanelStyle=nil
@@s_KOutOn=nil
@@s_KInOn=nil
@@s_NOutOn=nil
@@s_NInOn=nil
@@s_HOutStyle=nil
@@s_HInStyle=nil
@@s_SwingStyle=nil
@@s_UseMaterials=nil

	def initialize #initialize or activate - which one ???
	#-----------------------------------------------------------------------------------------------
	#Set default values
	#-----------------------------------------------------------------------------------------------
		@vp = Sketchup::InputPoint.new			#used in mouse routines to get/set/use the picked position
		@a_FOrigintemp = Sketchup::InputPoint.new	#used in mouse routines to get/set/use the picked position
		@displayed = false				#used in mouse routines to get/set/use the picked position
		@a_FOrigin=nil					#used in mouse routines to get/set/use the picked position

		#Layernames
		@@s_LayerRoot = "05-" if not @@s_LayerRoot	##Root name of created layers e.g. Fred-Frame,Bill-Frame etc
		@s_LayerDoorSet = "D-Doors"			#Layer for the frame and master group
		@s_LayerLeaf00 = "D-Leaf00"			#layer for the closed (0deg)leaf
		@s_LayerLeaf30 = "D-Leaf30"			#layer for the 30deg leaf
		@s_LayerLeaf60 = "D-Leaf60"			#layer for the 60deg leaf
		@s_LayerLeaf90 = "D-Leaf90"			#layer for the 90deg leaf
		@s_LayerFittings = "D-Fittings"		#layer for the handle, kicker, nameplate etc
		@s_LayerSwing = "D-Swing"			#layer for the arc showing the swing
		
		#Angle in rad's
		@v_Rad180 = 3.141592653589793		#180 degree angle = pi radians
		@v_Rad90 = @v_Rad180/2				#90 deg
		@v_Rad60 = @v_Rad180/3				#60 deg
		@v_Rad30 = @v_Rad180/6				#30 deg
		
		#DoorSet Defaults
		@@s_DoorType = "Single" if not @@s_DoorType	##Setable Door type - Single, Double etc
		@s_DSGrpName = "DM DoorSet"
		
		#Frame Defaults
		@@v_FWidth = 910.mm if not @@v_FWidth		##setable overall width of the frame
		@@v_FHeight = 2100.mm if not @@v_FHeight		##setable overall height of the frame
		@@v_FDepth = 100.mm if not @@v_FDepth		##setable depth of the frame (wall thickness?)
		@@v_FThick = 50.mm if not @@v_FThick		##setable width of the frame member
		@s_FMaterial = "Door-Frame"			#default colour fot the frame
		@@s_FStyle = "Simple" if not @@s_FStyle		##setable style for the frame
		@@v_FRebate = 15.mm				#default rebate for the frame
		@s_FGrpName = "DM Frame"
		
		#Leaf Defaults
		@@v_LDepth = 50.mm if not @@v_LDepth		##setable thickness of the leaf
		@s_LMaterial = "Door-Leaf"			#default colour for the leaf
		@@s_DamdH_W1 = 830.mm				#default width of the leaf for Door & Half Size 1
		@@s_DamdH_W2 = 930.mm				#default width of the leaf for Door & Half Size 2
		@@s_DamdH_W3 = 1030.mm				#default width of the leaf for Door & Half Size 3
		@@s_DamdH_W4 = 1130.mm				#default width of the leaf for Door & Half Size 4
		@s_LGrpName = "DM Leaf"
		@@s_LOffset = "Flush" if not @@s_LOffset		#setable position for leaf
		
		#Kicker Defaults
		@v_KFromEdge = 25.mm				#gap between kicker and the leaf edge
		@v_KHeight = 150.mm				#height of the kicker
		@v_KThick = 5.mm				#thickness of the plate
		@s_KMaterial = "Door-Fittings"			#default colour for the kicker
		@@s_KInOn = "NO" if not @@s_KInOn			##setable kicker on or off
		@@s_KOutOn = "YES" if not @@s_KOutOn		##setable kicker on or off
		
		#Name Plate Defaults
		@v_NWidth = 250.mm				#default nameplate width
		@v_NHeight = 75.mm				#default nameplate height
		@v_NFromTop = 300.mm				#default vertical posion from top of leaf
		@v_NThick = 5.mm				#default thickness of plate
		@s_NMaterial = "Door-Nameplate"			#default colour of plate
		@@s_NInOn = "NO" if not @@s_NInOn			##setable nameplate on or off
		@@s_NOutOn = "NO" if not @@s_NOutOn		##setable nameplate on or off
		
		#Handle Defaults
		@@s_HInStyle = "Handle" if not @@s_HInStyle	##setable handle style
		@@s_HOutStyle = "Handle" if not @@s_HOutStyle	##setable handle style
		@s_HMaterial = "Door-Fittings"			#default material for handle
		@v_HShaft1D = 50.mm				#default dia of collar/coverplate
		@v_HShaft1L = 10.mm				#default lenght/depth of collar
		@v_HShaft2D = 20.mm				#default dia of handle shaft
		@v_HShaft2L = 35.mm				#default lenght/depth of handle shaft
		@v_HHeight = 20.mm				#default height of the handle itsself
		@v_HLength = 150.mm				#default lenght of the handle itsself
		@v_HThick = 10.mm				#default thickness of the handle itsself
		@v_HFromEdge = 50.mm				#default distance of handle centre from edge of leaf
		@v_HFromG = 1000.mm				#default height from ground of handel
		@v_HPplateH = 350.mm				#default pushplate height
		@v_HPplateW = 85.mm				#default pushplate width
		@v_HPplateT = 5.mm				#default pushplate thickness
		@v_HLBarL = 1000.mm				#default longbar height
		@v_HLBarD = 35.mm				#default longbar dia
		@v_HPanicD = 25.mm				#default panic par dia
		@v_HPanicT = 10.mm				#default panic bar bracket thickness
		@v_HPanicH = 75.mm				#default panic bar bracket height
		@v_HPanicO = 50.mm				#default panic bar bracket depth(offset) from leaf
		@v_HSegs = 12					#default secments in curved/round handel parts
		
		#Panel Defaults
		@@s_PanelStyle = "None" if not @@s_PanelStyle	##setable panel style
		@s_PanelMaterial = "Door-Glass"			#default material for panel
		@v_PanelFromTop = 100.mm			#position from top of leaf
		@v_PanelWidth = 175.mm				#width of panel
		@v_PanelInset = 10.mm				#depth and width of inset
		#bottom of visionpanel is bottom of pushplate
		@v_PortH_D = 300.mm				#Porthole dia
		@v_PortH_FromG = 1700.mm			#center height from ground
		
		
		#Swing Defaults
		@@s_SwingStyle = "Single Swing" if not @@s_SwingStyle		#setable switch for the swing arc
		@v_SwingFromG = 1.mm						#default height that swing arc is drawn
		@s_SwingGrpName = "DM Swing Arc"
		@v_SwingSegs = 10						#default segments in arc
		
		@@s_UseMaterials = "YES" if not @@s_UseMaterials			#setabe default for using materials
		
		d_dstyles = "Single|Double|Door & a Half 830mm|Door & a Half 930mm|Door & a Half 1030mm|Door & a Half 1130mm"
		d_fstyles = "Simple|Rebated|Rebated + Architrave"						#setable list for the frame style
		d_loffset = "Flush|Centred"
		d_hstyles = "None|Handle|Push Plate|Long Bar|Panic Bolt"	#setable list for the handel style
		d_panelstyles = "None|VP Centered|VP Offset|1x1 Glass|1x1 Solid|1x2 Glass|1x2 Solid|Port Hole"	#setable list for the vision panel
		d_SwingStyle = "None|Single Swing|Double Swing"
		
		d_truefalse = "YES|NO"						#yes or no for use in the dialog
	
	#-----------------------------------------------------------------------------------------------
		#Prompt for dims
		#Create arrays for prompts
		prompts = [	"Layer Root",				#1
				"Door type",					#1a
				"Opening Width",				#2
				"Opening Height",				#3
				"Frame Depth",					#4
				"Frame Width",					#5
				"Frame Profile",				#6
				"Leaf Thickness",				#7
				"Leaf Offset",					#7a
				"Panel Type",					#8
				"Kicker Outward",				#9
				"Kicker Inward",				#10
				"Nameplate Outward",			#11
				"Nameplate Inward",				#12
				"Handle Outward",				#13
				"Handle Inward",				#14
				"Swing Arc Style",				#15
				"Create/Use Default Materials "	#16
									]
		values = [	@@s_LayerRoot,			#1
				@@s_DoorType,				#1a
				@@v_FWidth,					#2
				@@v_FHeight,					#3
				@@v_FDepth,					#4
				@@v_FThick,					#5
				@@s_FStyle,					#6
				@@v_LDepth,					#7
				@@s_LOffset,					#7a
				@@s_PanelStyle,				#8
				@@s_KOutOn,					#9
				@@s_KInOn,					#10
				@@s_NOutOn,					#11
				@@s_NInOn,					#12
				@@s_HOutStyle,				#13
				@@s_HInStyle,				#14	
				@@s_SwingStyle,				#15
				@@s_UseMaterials				#16
						]
		enums = [	'',					#1
				d_dstyles,				#1a
				'',						#2
				'',						#3
				'',						#4
				'',						#5
				d_fstyles,				#6
				'',						#7
				d_loffset,				#7a	
				d_panelstyles,			#8
				d_truefalse,			#9
				d_truefalse,			#10
				d_truefalse,			#11
				d_truefalse,			#12
				d_hstyles,				#13
				d_hstyles,				#14
				d_SwingStyle,			#15
				d_truefalse				#16
						]
		#Now display the inputbOx (from sketchup.rb)
		results = inputbox(prompts, values, enums, "Door Settings")
	
		return if not results 						# This means that the user canceled the operation
	
				@@s_LayerRoot,					#1
				@@s_DoorType,					#1a
				@@v_FWidth,						#2
				@@v_FHeight,						#3
				@@v_FDepth,						#4
				@@v_FThick,						#5
				@@s_FStyle,						#6
				@@v_LDepth,						#7
				@@s_LOffset,						#7a
				@@s_PanelStyle,					#8
				@@s_KOutOn,						#9
				@@s_KInOn,						#10
				@@s_NOutOn,						#11
				@@s_NInOn,						#12
				@@s_HOutStyle,					#13
				@@s_HInStyle,					#14
				@@s_SwingStyle,					#15
				@@s_UseMaterials = results		#16
						

	end #end initialize
	
	#-----------------------------------------------------------------------------------------------
	def onMouseMove(flags, x, y, view)    
		@a_FOrigintemp.pick view, x, y					# get a position in the model 
		if( @a_FOrigintemp.valid? )
			changed = @a_FOrigintemp != @vp
			@vp.copy! @a_FOrigintemp
			pos = @vp.position;
			@a_FOrigin=pos						#save position so it can be used in  onLButtonUp()
			@a_LOrigin = pos			
			msg = @vp.tooltip 					#start building tooltip message
			if( msg.length > 0 )
				msg << " "
			end
			msg << "Door Here: "
			msg << pos.x.to_s
			msg << ", "
			msg << pos.y.to_s
			msg << ", "
			msg << pos.z.to_s
			view.tooltip = msg					# show message as tooltip
			# check if display needs update
			if( changed and (@vp.display? or @displayed) )         
				view.invalidate					#make the view update  
			end
		end #end if	
	end #end onMouseMove
	
	#-----------------------------------------------------------------------------------------------
	def onLButtonUp(flags, x, y, view)					#called when left mouse button released
		dm_create_door
	end #end onLButtonUp
	
	#-----------------------------------------------------------------------------------------------
	def dm_create_door
			
		#get the current layer
		model = Sketchup.active_model
		layers = model.layers
		userlayer = model.active_layer
		model.commit_operation
	
	
		#set the rebate to zero or the leaf will be to big for the frame
		if(@@s_FStyle == "Simple") then  @@v_FRebate=0 end
		
		#create the origin array for the leaf, Sketchup's base units are inches so
		#mm units have to be converted to inches to be added to the frame origins (thanks RickW)
		case @@s_LOffset
		when "Centred"
			@a_LOrigin = [
					((@a_FOrigin.x+@@v_FThick.to_inch-@@v_FRebate.to_inch).to_mm).mm, 
					((@a_FOrigin.y+(@@v_FDepth.to_inch/2)-(@@v_LDepth.to_inch/2)).to_mm).mm,
					@a_FOrigin.z
					]
		when "Flush"
			@a_LOrigin = [
					((@a_FOrigin.x+@@v_FThick.to_inch-@@v_FRebate.to_inch).to_mm).mm, 
					((@a_FOrigin.y+@@v_FDepth.to_inch-@@v_LDepth.to_inch).to_mm).mm,
					@a_FOrigin.z
					]
		end #end case
		
		
		
		#set the size of the leaf
		case @@s_DoorType
		when "Single"
			@v_LWidth = ((@@v_FWidth-@@v_FThick-@@v_FThick+@@v_FRebate+@@v_FRebate).to_mm).mm
		when "Double"
			@v_LWidth = (((@@v_FWidth-@@v_FThick-@@v_FThick+@@v_FRebate+@@v_FRebate)/2).to_mm).mm
		when "Door & a Half 830mm"
			#Ok this is how I'm working it:
			#Say its a 1400mm opening, 50mm frame thickness and a 830mm main leaf
			#I set the leaf width to (1400-50-50)-830 = 470mm and make the leafset
			#Later in the script:
				#Mirror it without a copy
				#Then make a leafset of 830mm and do not mirror it
			@v_LWidth = (((@@v_FWidth-@@v_FThick-@@v_FThick+@@v_FRebate+@@v_FRebate)-@@s_DamdH_W1).to_mm).mm
		when "Door & a Half 930mm"
			@v_LWidth = (((@@v_FWidth-@@v_FThick-@@v_FThick+@@v_FRebate+@@v_FRebate)-@@s_DamdH_W2).to_mm).mm
		when "Door & a Half 1030mm"
			@v_LWidth = (((@@v_FWidth-@@v_FThick-@@v_FThick+@@v_FRebate+@@v_FRebate)-@@s_DamdH_W3).to_mm).mm
		when "Door & a Half 1130mm"
			@v_LWidth = (((@@v_FWidth-@@v_FThick-@@v_FThick+@@v_FRebate+@@v_FRebate)-@@s_DamdH_W4).to_mm).mm
		end #end case
		@v_LHeight = ((@@v_FHeight-@@v_FThick+@@v_FRebate).to_mm).mm

		#Go create the materials of the user wants them
		if(@@s_UseMaterials == "YES") then dm_add_paint end

		#Start the entire creation process
		@model = Sketchup.active_model
		
			#Create and set the master layer
			@s_LayerDoorSet2 = @@s_LayerRoot+@s_LayerDoorSet	#set destination layer
			layers = @model.layers
			layers.add(@s_LayerDoorSet2)
			activelayer = @model.active_layer=layers[@s_LayerDoorSet2]
			layer=@model.active_layer
			
			#create the master group for the door set
			@doorset_entities = @model.active_entities
			@doorset_group = @doorset_entities.add_group
			@doorset_entities = @doorset_group.entities
			@doorset_group.name = @s_DSGrpName
		
			#Create frame on the master group
			dm_create_frame
				case @@s_DoorType
				when "Single", "Double"
					#create a group for all the leafs and swing arc
					@leafset_entities = @model.active_entities
					@leafset_group = @doorset_entities.add_group
					@leafset_entities = @leafset_group.entities
					@leafset_group.name = @s_LGrpName+"s"
					
					#Create leaf 00 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf00	#set destination layer
					@v_Langle = 0					#Set opening angle
					dm_create_leaf					#call routine
		
					#Create leaf 30 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf30
					@v_Langle = @v_Rad30
					dm_create_leaf
		
					#Create leaf 60 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf60
					@v_Langle = @v_Rad60
					dm_create_leaf
		
					#Create leaf 90 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf90
					@v_Langle = @v_Rad90
					dm_create_leaf
		
					#Create swing Arc
					if(@@s_SwingStyle != "None")
						@s_LayerSwing2 = @@s_LayerRoot+@s_LayerSwing	#set destination layer
						dm_create_swing					#call routine
					end
					
					#now we have our leafset group we need to make a copy and mirror(scale -1) it if its a double door
					if(@@s_DoorType == "Double")
						#Set the master layer
						@s_LayerDoorSet2 = @@s_LayerRoot+@s_LayerDoorSet	#set destination layer
						layers = @model.layers
						activelayer = @model.active_layer=layers[@s_LayerDoorSet2]
						layer=@model.active_layer
				
						@leafset2_group=@leafset_group.copy
						@leafset2_group.name = @s_LGrpName+"s"
						mpoint = Geom::Point3d.new @a_FOrigin.x+(@@v_FWidth/2), @a_FOrigin.y, @a_FOrigin.z				
						tr=Geom::Transformation.scaling mpoint,-1,1,1
						@leafset2_group.transform!(tr)
					end #end if
				
				when "Door & a Half 830mm", "Door & a Half 930mm", "Door & a Half 1030mm", "Door & a Half 1130mm"
				#first we have to make the smaller leafset (the size has already been set above)
					
					#create a group for all the leafs and swing arc
					@leafset_entities = @model.active_entities
					@leafset_group = @doorset_entities.add_group
					@leafset_entities = @leafset_group.entities
					@leafset_group.name = @s_LGrpName+"s"
					
					#Create leaf 00 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf00	#set destination layer
					@v_Langle = 0					#Set opening angle
					dm_create_leaf					#call routine
		
					#Create leaf 30 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf30
					@v_Langle = @v_Rad30
					dm_create_leaf
		
					#Create leaf 60 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf60
					@v_Langle = @v_Rad60
					dm_create_leaf
		
					#Create leaf 90 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf90
					@v_Langle = @v_Rad90
					dm_create_leaf
		
					#Create swing Arc
					if(@@s_SwingStyle != "None")
						@s_LayerSwing2 = @@s_LayerRoot+@s_LayerSwing	#set destination layer
						dm_create_swing					#call routine
					end

					#Set the master layer
					@s_LayerDoorSet2 = @@s_LayerRoot+@s_LayerDoorSet	#set destination layer
					layers = @model.layers
					activelayer = @model.active_layer=layers[@s_LayerDoorSet2]
					layer=@model.active_layer

					#mirror (scale -1) the group about the midpoint of the frame
					@leafset_group.name = @s_LGrpName+"s"
					mpoint = Geom::Point3d.new @a_FOrigin.x+(@@v_FWidth/2), @a_FOrigin.y, @a_FOrigin.z				
					tr=Geom::Transformation.scaling mpoint,-1,1,1
					@leafset_group.transform!(tr)
					
					
					#now we can make the larger leafset
					#create a group for all the leafs and swing arc
					@leafset_entities = @model.active_entities
					@leafset_group = @doorset_entities.add_group
					@leafset_entities = @leafset_group.entities
					@leafset_group.name = @s_LGrpName+"s"
					
					#set the width of the master leaf
					case @@s_DoorType
					when "Door & a Half 830mm"
						@v_LWidth = @@s_DamdH_W1
					when "Door & a Half 930mm"
						@v_LWidth = @@s_DamdH_W2
					when "Door & a Half 1030mm"
						@v_LWidth = @@s_DamdH_W3
					when "Door & a Half 1130mm"
						@v_LWidth = @@s_DamdH_W4
					end #end case
					
					
					#Create leaf 00 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf00	#set destination layer
					@v_Langle = 0					#Set opening angle
					dm_create_leaf					#call routine
		
					#Create leaf 30 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf30
					@v_Langle = @v_Rad30
					dm_create_leaf
		
					#Create leaf 60 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf60
					@v_Langle = @v_Rad60
					dm_create_leaf
		
					#Create leaf 90 degress open
					@s_LayerLeaf = @@s_LayerRoot+@s_LayerLeaf90
					@v_Langle = @v_Rad90
					dm_create_leaf
		
					#Create swing Arc
					if(@@s_SwingStyle != "None")
						@s_LayerSwing2 = @@s_LayerRoot+@s_LayerSwing	#set destination layer
						dm_create_swing					#call routine
					end
				
				end #end case
		
		#End the entire creation process
		@model.commit_operation
		
		#reset the layer back to the users layer
		layers = model.layers
		activelayer = model.active_layer= userlayer
		layer=model.active_layer
		model.commit_operation
		
	end	# end of dm_create_door
#-----------------------------------------------------------------------------------------------



	#-----------------------------------------------------------------------------------------------
	#Routines
	#-----------------------------------------------------------------------------------------------
	def dm_create_frame

		@model.start_operation("Create Door Frame")

		#Create and set layer
		layers = @model.layers
		layers.add(@s_LayerDoorSet2)
		activelayer = @model.active_layer=layers[@s_LayerDoorSet2]
		layer=@model.active_layer

		@frame_entities = @model.active_entities
	
		@frame_group = @doorset_entities.add_group
		@frame_entities = @frame_group.entities
		@frame_group.name = @s_FGrpName
		
		#create the path for the frame profile to follow
		pts = Array.new(4) { @a_FOrigin.clone }
		pts[1].z += @@v_FHeight				#x		y		+FHeight
		pts[2].x += @@v_FWidth				#+FWidth	y		z
		pts[2].z += @@v_FHeight				#x		y		+FHeight
		pts[3].x += @@v_FWidth				#+FWidth	y		z
		path = @frame_entities.add_edges(pts)
		
		#create the profile
		case @@s_FStyle
		when "Simple"
			pts = Array.new(4) { @a_FOrigin.clone }
			pts[1].y += @@v_FDepth			#x		+FDepth		z
			pts[2].x += @@v_FThick			#+FThick	y		z
			pts[2].y += @@v_FDepth			#x		+FDepth		z
			pts[3].x += @@v_FThick			#+FThick	y		z
			
		when "Rebated"	
			pts = Array.new(6) { @a_FOrigin.clone }
			pts[1].y += @@v_FDepth			#x		+FDepth		z
			pts[2].y += @@v_FDepth			#x		+FDepth		z
			pts[2].x += @@v_FThick-@@v_FRebate	#+FThick+FReb	y		z
			pts[3].x += @@v_FThick-@@v_FRebate	#+FThick+FReb	y		z
			pts[3].y = @a_LOrigin.y			#x		=LOrigin	z
			pts[4].x += @@v_FThick			#+FThick	y		z
			pts[4].y = @a_LOrigin.y			#x		=LOrigin	z
			pts[5].x += @@v_FThick			#+FThick	y		z
		when "Rebated + Architrave"
			pts = Array.new(14) { @a_FOrigin.clone }
			#pts[1].x
			pts[1].y += @@v_FDepth
			pts[2].x += -(@@v_FThick/2)
			pts[2].y += @@v_FDepth
			pts[3].x += -(@@v_FThick/2)
			pts[3].y += @@v_FDepth+@@v_FRebate
			pts[4].x += (@@v_FThick/2)
			pts[4].y += @@v_FDepth+@@v_FRebate
			pts[5].x += (@@v_FThick/2)
			pts[5].y += @@v_FDepth
			pts[6].x += @@v_FThick-@@v_FRebate
			pts[6].y += @@v_FDepth
			pts[7].x += @@v_FThick-@@v_FRebate
			pts[7].y = @a_LOrigin.y	
			pts[8].x += @@v_FThick
			pts[8].y = @a_LOrigin.y	
			pts[9].x += @@v_FThick
			#pts[9].y
			pts[10].x += (@@v_FThick/2)
			#pts[10].y
			pts[11].x += (@@v_FThick/2)
			pts[11].y += -@@v_FRebate
			pts[12].x += -(@@v_FThick/2)
			pts[12].y += -@@v_FRebate
			pts[13].x += -(@@v_FThick/2)
			#pts[13].y

		
		end #end case
		#create face from points
		profile = @frame_entities.add_face(pts)
		#Paint
		if(@@s_UseMaterials == "YES") then profile.material = @s_FMaterial end
		#pull the profile around the path to make the frame
		profile.followme(path)
			    
	end	# end of dm_create_frame
	
	#-----------------------------------------------------------------------------------------------
	def dm_create_leaf
	
		@model.start_operation("Create Door Leaf")
	
		#Create and set layer
		layers = @model.layers
		layers.add(@s_LayerLeaf)
	  	activelayer = @model.active_layer=layers[@s_LayerLeaf]
		layer=@model.active_layer
	
		
		@leaf_entities = @model.active_entities
		
		@leaf_group = @leafset_entities.add_group
	        @leaf_entities = @leaf_group.entities
	        @leaf_group.name = @s_LGrpName
	        
		#Create The Leaf
		pts = Array.new(4) { @a_LOrigin.clone }
		pts[1].z += @v_LHeight				#x		y		+LHeight
		pts[2].x += @v_LWidth				#+LWidth	y		z
		pts[2].z += @v_LHeight				#x		y		+LHeight
		pts[3].x += @v_LWidth				#+LWidth	y		z
		#create face from points
		base = @leaf_entities.add_face(pts)
		#Paint
		if(@@s_UseMaterials == "YES") then base.material = @s_LMaterial end
		# Now we can do the pushpull
		base.pushpull(@@v_LDepth)
		
		#Create the vision panel
		#only create the panels if the leaf thickness is more than twice the inset depth or the leaf width is wider than the panel + 2x the from edge value
		if (@@v_LDepth>(@v_PanelInset+@v_PanelInset)) and (@v_LWidth>(@v_PanelWidth+@v_PanelWidth))

			case @@s_PanelStyle
			when "None"
			
			when "VP Centered"
				#Rebated inset on front of leaf
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += (@v_LWidth/2)-(@v_PanelWidth/2)
				pts[0].z += @v_HFromG-(@v_HPplateH/2)
				pts[1].x += (@v_LWidth/2)-(@v_PanelWidth/2)
				pts[1].z += @v_LHeight-@v_PanelFromTop
				pts[2].x += (@v_LWidth/2)+(@v_PanelWidth/2)
				pts[2].z += @v_LHeight-@v_PanelFromTop
				pts[3].x += (@v_LWidth/2)+(@v_PanelWidth/2)
				pts[3].z += @v_HFromG-(@v_HPplateH/2)
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-@v_PanelInset)
				
				#Rebated inset on rear of leaf				
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += (@v_LWidth/2)-(@v_PanelWidth/2)
				pts[0].y += @@v_LDepth
				pts[0].z += @v_HFromG-(@v_HPplateH/2)
				pts[1].x += (@v_LWidth/2)-(@v_PanelWidth/2)
				pts[1].y += @@v_LDepth
				pts[1].z += @v_LHeight-@v_PanelFromTop
				pts[2].x += (@v_LWidth/2)+(@v_PanelWidth/2)
				pts[2].y += @@v_LDepth
				pts[2].z += @v_LHeight-@v_PanelFromTop
				pts[3].x += (@v_LWidth/2)+(@v_PanelWidth/2)
				pts[3].y += @@v_LDepth
				pts[3].z += @v_HFromG-(@v_HPplateH/2)
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-@v_PanelInset)
				
				#hole through leaf
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += (@v_LWidth/2)-(@v_PanelWidth/2)+@v_PanelInset
				pts[0].y += @v_PanelInset
				pts[0].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				pts[1].x += (@v_LWidth/2)-(@v_PanelWidth/2)+@v_PanelInset
				pts[1].y += @v_PanelInset
				pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[2].x += (@v_LWidth/2)+(@v_PanelWidth/2)-@v_PanelInset
				pts[2].y += @v_PanelInset
				pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[3].x += (@v_LWidth/2)+(@v_PanelWidth/2)-@v_PanelInset
				pts[3].y += @v_PanelInset
				pts[3].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-(@@v_LDepth-@v_PanelInset-@v_PanelInset))
			
				#glass
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += (@v_LWidth/2)-(@v_PanelWidth/2)+@v_PanelInset
				pts[0].y += (@@v_LDepth/2)
				pts[0].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				pts[1].x += (@v_LWidth/2)-(@v_PanelWidth/2)+@v_PanelInset
				pts[1].y += (@@v_LDepth/2)
				pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[2].x += (@v_LWidth/2)+(@v_PanelWidth/2)-@v_PanelInset
				pts[2].y += (@@v_LDepth/2)
				pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[3].x += (@v_LWidth/2)+(@v_PanelWidth/2)-@v_PanelInset
				pts[3].y += (@@v_LDepth/2)
				pts[3].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				#create face from points
				base = @leaf_entities.add_face(pts)
				#Paint
				if(@@s_UseMaterials == "YES") then base.material = @s_PanelMaterial end
				
				
			when "VP Offset"
				#Rebated inset on front of leaf
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth
				pts[0].z += @v_HFromG-(@v_HPplateH/2)
				pts[1].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth
				pts[1].z += @v_LHeight-@v_PanelFromTop
				pts[2].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)
				pts[2].z += @v_LHeight-@v_PanelFromTop
				pts[3].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)
				pts[3].z += @v_HFromG-(@v_HPplateH/2)
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-@v_PanelInset)
				
				#Rebated inset on rear of leaf				
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth
				pts[0].y += @@v_LDepth
				pts[0].z += @v_HFromG-(@v_HPplateH/2)
				pts[1].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth
				pts[1].y += @@v_LDepth
				pts[1].z += @v_LHeight-@v_PanelFromTop
				pts[2].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)
				pts[2].y += @@v_LDepth
				pts[2].z += @v_LHeight-@v_PanelFromTop
				pts[3].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)
				pts[3].y += @@v_LDepth
				pts[3].z += @v_HFromG-(@v_HPplateH/2)
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-@v_PanelInset)
				
				#hole through leaf
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth+@v_PanelInset
				pts[0].y += @v_PanelInset
				pts[0].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				pts[1].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth+@v_PanelInset
				pts[1].y += @v_PanelInset
				pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[2].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelInset
				pts[2].y += @v_PanelInset
				pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[3].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelInset
				pts[3].y += @v_PanelInset
				pts[3].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-(@@v_LDepth-@v_PanelInset-@v_PanelInset))
			
				#glass
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth+@v_PanelInset
				pts[0].y += (@@v_LDepth/2)
				pts[0].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				pts[1].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelWidth+@v_PanelInset
				pts[1].y += (@@v_LDepth/2)
				pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[2].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelInset
				pts[2].y += (@@v_LDepth/2)
				pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
				pts[3].x += @v_LWidth-@v_HFromEdge-@v_HPplateW-(@v_HFromEdge/2)-@v_PanelInset
				pts[3].y += (@@v_LDepth/2)
				pts[3].z += @v_HFromG-(@v_HPplateH/2)+@v_PanelInset
				#create face from points
				base = @leaf_entities.add_face(pts)
				#Paint
				if(@@s_UseMaterials == "YES") then base.material = @s_PanelMaterial end
				
			#if the user wants a 1x1 panel either solid or glass then...
			when "1x1 Glass", "1x1 Solid"
				#Rebated inset on front of leaf
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += @v_HFromEdge+@v_HFromEdge
				pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
				pts[1].x += @v_HFromEdge+@v_HFromEdge
				pts[1].z += @v_LHeight-@v_PanelFromTop
				pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
				pts[2].z += @v_LHeight-@v_PanelFromTop
				pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
				pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-@v_PanelInset)
				
				#Rebated inset on rear of leaf
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += @v_HFromEdge+@v_HFromEdge
				pts[0].y += @@v_LDepth
				pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
				pts[1].x += @v_HFromEdge+@v_HFromEdge
				pts[1].y += @@v_LDepth
				pts[1].z += @v_LHeight-@v_PanelFromTop
				pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
				pts[2].y += @@v_LDepth
				pts[2].z += @v_LHeight-@v_PanelFromTop
				pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
				pts[3].y += @@v_LDepth
				pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
				#create face from points
				base = @leaf_entities.add_face(pts)
				# Now we can do the pushpull
				base.pushpull(-@v_PanelInset)
				
				#if the 1x1 panel is glass then go ahead and punch the hole and fill it with glass
				if(@@s_PanelStyle == "1x1 Glass")
					#hole through leaf
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
					pts[0].y += @v_PanelInset
					pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
					pts[1].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
					pts[1].y += @v_PanelInset
					pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
					pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
					pts[2].y += @v_PanelInset
					pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
					pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
					pts[3].y += @v_PanelInset
					pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
					#create face from points
					base = @leaf_entities.add_face(pts)
					# Now we can do the pushpull
					base.pushpull(-(@@v_LDepth-@v_PanelInset-@v_PanelInset))
					
					#glass
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
					pts[0].y += (@@v_LDepth/2)
					pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
					pts[1].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
					pts[1].y += (@@v_LDepth/2)
					pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
					pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
					pts[2].y += (@@v_LDepth/2)
					pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
					pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
					pts[3].y += (@@v_LDepth/2)
					pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
					#create face from points
					base = @leaf_entities.add_face(pts)
					#Paint
					if(@@s_UseMaterials == "YES") then base.material = @s_PanelMaterial end
				end #end if
			
			#if the user wants a 1x2 panel either solid or glass then...
			when "1x2 Glass", "1x2 Solid"
				#only create the 2 panels if the leaf height is (a bit) taller than the handle height
				if ((@v_HFromG+@v_HFromEdge+@v_HFromEdge) < @v_LHeight)
				#Lower Panel
					#Rebated inset on front of leaf
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_HFromEdge+@v_HFromEdge
					pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
					pts[1].x += @v_HFromEdge+@v_HFromEdge
					pts[1].z += @v_HFromG-@v_HFromEdge
					pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[2].z += @v_HFromG-@v_HFromEdge
					pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
					#create face from points
					base = @leaf_entities.add_face(pts)
					# Now we can do the pushpull
					base.pushpull(-@v_PanelInset)
					
					#Rebated inset on rear of leaf
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_HFromEdge+@v_HFromEdge
					pts[0].y += @@v_LDepth
					pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
					pts[1].x += @v_HFromEdge+@v_HFromEdge
					pts[1].y += @@v_LDepth
					pts[1].z += @v_HFromG-@v_HFromEdge
					pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[2].y += @@v_LDepth
					pts[2].z += @v_HFromG-@v_HFromEdge
					pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[3].y += @@v_LDepth
					pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge
					#create face from points
					base = @leaf_entities.add_face(pts)
					# Now we can do the pushpull
					base.pushpull(-@v_PanelInset)
					
					#if the 1x2 panel is glass then go ahead and punch the hole and fill it with glass
					if(@@s_PanelStyle == "1x2 Glass")
						#hole through leaf
						pts = Array.new(4) { @a_LOrigin.clone }
						pts[0].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[0].y += @v_PanelInset
						pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
						pts[1].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[1].y += @v_PanelInset
						pts[1].z += @v_HFromG-@v_HFromEdge-@v_PanelInset
						pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[2].y += @v_PanelInset
						pts[2].z += @v_HFromG-@v_HFromEdge-@v_PanelInset
						pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[3].y += @v_PanelInset
						pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
						#create face from points
						base = @leaf_entities.add_face(pts)
						# Now we can do the pushpull
						base.pushpull(-(@@v_LDepth-@v_PanelInset-@v_PanelInset))
						
						#glass
						pts = Array.new(4) { @a_LOrigin.clone }
						pts[0].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[0].y += (@@v_LDepth/2)
						pts[0].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
						pts[1].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[1].y += (@@v_LDepth/2)
						pts[1].z += @v_HFromG-@v_HFromEdge-@v_PanelInset
						pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[2].y += (@@v_LDepth/2)
						pts[2].z += @v_HFromG-@v_HFromEdge-@v_PanelInset
						pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[3].y += (@@v_LDepth/2)
						pts[3].z += @v_KFromEdge+@v_KHeight+@v_KFromEdge+@v_PanelInset
						#create face from points
						base = @leaf_entities.add_face(pts)
						#Paint
						if(@@s_UseMaterials == "YES") then base.material = @s_PanelMaterial end
					end #end if

				#Upper Panel
					#Rebated inset on front of leaf
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_HFromEdge+@v_HFromEdge
					pts[0].z += @v_HFromG+@v_HFromEdge
					pts[1].x += @v_HFromEdge+@v_HFromEdge
					pts[1].z += @v_LHeight-@v_PanelFromTop
					pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[2].z += @v_LHeight-@v_PanelFromTop
					pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[3].z += @v_HFromG+@v_HFromEdge
					#create face from points
					base = @leaf_entities.add_face(pts)
					# Now we can do the pushpull
					base.pushpull(-@v_PanelInset)
					
					#Rebated inset on rear of leaf
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_HFromEdge+@v_HFromEdge
					pts[0].y += @@v_LDepth
					pts[0].z += @v_HFromG+@v_HFromEdge
					pts[1].x += @v_HFromEdge+@v_HFromEdge
					pts[1].y += @@v_LDepth
					pts[1].z += @v_LHeight-@v_PanelFromTop
					pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[2].y += @@v_LDepth
					pts[2].z += @v_LHeight-@v_PanelFromTop
					pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge
					pts[3].y += @@v_LDepth
					pts[3].z += @v_HFromG+@v_HFromEdge
					#create face from points
					base = @leaf_entities.add_face(pts)
					# Now we can do the pushpull
					base.pushpull(-@v_PanelInset)
					
					#if the 1x2 panel is glass then go ahead and punch the hole and fill it with glass
					if(@@s_PanelStyle == "1x2 Glass")
						#hole through leaf
						pts = Array.new(4) { @a_LOrigin.clone }
						pts[0].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[0].y += @v_PanelInset
						pts[0].z += @v_HFromG+@v_HFromEdge+@v_PanelInset
						pts[1].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[1].y += @v_PanelInset
						pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
						pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[2].y += @v_PanelInset
						pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
						pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[3].y += @v_PanelInset
						pts[3].z += @v_HFromG+@v_HFromEdge+@v_PanelInset
						#create face from points
						base = @leaf_entities.add_face(pts)
						# Now we can do the pushpull
						base.pushpull(-(@@v_LDepth-@v_PanelInset-@v_PanelInset))
						
						#glass
						pts = Array.new(4) { @a_LOrigin.clone }
						pts[0].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[0].y += (@@v_LDepth/2)
						pts[0].z += @v_HFromG+@v_HFromEdge+@v_PanelInset
						pts[1].x += @v_HFromEdge+@v_HFromEdge+@v_PanelInset
						pts[1].y += (@@v_LDepth/2)
						pts[1].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
						pts[2].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[2].y += (@@v_LDepth/2)
						pts[2].z += @v_LHeight-@v_PanelFromTop-@v_PanelInset
						pts[3].x += @v_LWidth-+@v_HFromEdge-@v_HPplateW-@v_HFromEdge-@v_PanelInset
						pts[3].y += (@@v_LDepth/2)
						pts[3].z += @v_HFromG+@v_HFromEdge+@v_PanelInset
						#create face from points
						base = @leaf_entities.add_face(pts)
						#Paint
						if(@@s_UseMaterials == "YES") then base.material = @s_PanelMaterial end
					end #end if
				end #end if
			when "Port Hole"
				#only create the port hole id the leaf is wide enough
				if (@v_LWidth > (@v_PortH_D+(@v_PanelInset*4)+(@v_HFromEdge*4)))
					#create the frame on the 1st side of the leaf
					if @v_PortH_FromG+(@v_PortH_D/2)+(@v_PanelInset*2)+(@v_HFromEdge*2) < @v_LHeight
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y, @a_LOrigin.z+@v_PortH_FromG]
					else
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y, @a_LOrigin.z+@v_LHeight-(@v_HFromEdge*2)-(@v_PanelInset*2)-(@v_PortH_D/2)]
					end #end if
					circ = @leaf_entities.add_circle(pt1, Y_AXIS, (@v_PortH_D/2)+(@v_PanelInset*2), @v_HSegs*2)
					if(@@s_UseMaterials == "YES") then @leaf_entities[-1].material = @s_HMaterial end
					@leaf_entities[-1].pushpull(@v_PanelInset)
	
					#create the frame on the 2nd side of the leaf
					if @v_PortH_FromG+(@v_PortH_D/2)+(@v_PanelInset*2)+(@v_HFromEdge*2) < @v_LHeight
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y+@@v_LDepth, @a_LOrigin.z+@v_PortH_FromG]
					else
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y+@@v_LDepth, @a_LOrigin.z+@v_LHeight-(@v_HFromEdge*2)-(@v_PanelInset*2)-(@v_PortH_D/2)]
					end #end if
					circ = @leaf_entities.add_circle(pt1, Y_AXIS, (@v_PortH_D/2)+(@v_PanelInset*2), @v_HSegs*2)
					if(@@s_UseMaterials == "YES") then @leaf_entities[-1].material = @s_HMaterial end
					@leaf_entities[-1].pushpull(@v_PanelInset)
	
					#create the hole through the leaf
					if @v_PortH_FromG+(@v_PortH_D/2)+(@v_PanelInset*2)+(@v_HFromEdge*2) < @v_LHeight
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y-@v_PanelInset, @a_LOrigin.z+@v_PortH_FromG]
					else
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y-@v_PanelInset, @a_LOrigin.z+@v_LHeight-(@v_HFromEdge*2)-(@v_PanelInset*2)-(@v_PortH_D/2)]
					end #end if
					circ = @leaf_entities.add_circle(pt1, Y_AXIS, (@v_PortH_D/2), @v_HSegs*2)
					if(@@s_UseMaterials == "YES") then @leaf_entities[-1].material = @s_HMaterial end
					@leaf_entities[-1].pushpull(-(@v_PanelInset+@@v_LDepth+@v_PanelInset))
	
					#create the glass to fill the hole
					if @v_PortH_FromG+(@v_PortH_D/2)+(@v_PanelInset*2)+(@v_HFromEdge*2) < @v_LHeight
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z+@v_PortH_FromG]
					else
						pt1 = [@a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z+@v_LHeight-(@v_HFromEdge*2)-(@v_PanelInset*2)-(@v_PortH_D/2)]
					end #end if
					circ = @leaf_entities.add_circle(pt1, Y_AXIS, (@v_PortH_D/2), @v_HSegs*2)
					base = @leaf_entities.add_face(circ)
					if(@@s_UseMaterials == "YES") then base.material = @s_PanelMaterial end
				end #end if
			end #end case
		end #end if
		
		#Create the Kickers
		#if either the Outside or Inside kicker is set to on - make the outside kicker
		if(@@s_KOutOn == "YES") or (@@s_KInOn =="YES")
		
			@kicker_entities = @model.active_entities
			@kicker_group = @leaf_entities.add_group
			@kicker_entities = @kicker_group.entities
			@kicker_group.name = "Kicker Out"
		
			#create the points
			pts = Array.new(4) { @a_LOrigin.clone }
			pts[0].x += @v_KFromEdge+@@v_FRebate
			pts[0].z += @v_KFromEdge
			pts[1].x += @v_KFromEdge+@@v_FRebate
			pts[1].z += @v_KFromEdge+@v_KHeight
			pts[2].x += @v_LWidth-@v_KFromEdge-@@v_FRebate
			pts[2].z += @v_KFromEdge+@v_KHeight
			pts[3].x += @v_LWidth-@v_KFromEdge-@@v_FRebate 
			pts[3].z += @v_KFromEdge
			#create face from points
			base = @kicker_entities.add_face(pts)
			base=base.reverse!
			#Paint
			if(@@s_UseMaterials == "YES") then base.material = @s_KMaterial end
			# Now we can do the pushpull
			base.pushpull(@v_KThick)

			#If it turns out that only the inside kicker is on then mirror the outside kicker to the inside
			if(@@s_KOutOn == "NO") and (@@s_KInOn =="YES")
				@kicker_group.name = "Kicker In"
				mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
				tr=Geom::Transformation.scaling mpoint,1,-1,1
				@kicker_group.transform!(tr)
			end #end if
			#If it turns out that both are on then copy the kicker group and then mirror the copy to the inside
			if(@@s_KOutOn == "YES") and (@@s_KInOn =="YES")
				@kicker2_group=@kicker_group.copy
				@kicker2_group.name = "Kicker In"
				mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
				tr=Geom::Transformation.scaling mpoint,1,-1,1
				@kicker2_group.transform!(tr)
			end #end if
		end #end if
							
		#Create the Name Plate
		#only create the nameplate if the panelstyle is none
		if(@@s_PanelStyle == "None")
			if(@@s_NOutOn == "YES") or (@@s_NInOn == "YES")
			
				@nplate_entities = @model.active_entities
				@nplate_group = @leaf_entities.add_group
				@nplate_entities = @nplate_group.entities
				@nplate_group.name = "NamePlate Out"
			
				pts = Array.new(4) { @a_LOrigin.clone }
				pts[0].x += (@v_LWidth/2)-(@v_NWidth/2)
				#pts[0].y += @@v_LDepth
				pts[0].z += @v_LHeight-@v_NFromTop-@v_NHeight
				pts[1].x += (@v_LWidth/2)-(@v_NWidth/2)
				#pts[1].y += @@v_LDepth
				pts[1].z += @v_LHeight-@v_NFromTop
				pts[2].x += (@v_LWidth/2)+(@v_NWidth/2)
				#pts[2].y += @@v_LDepth
				pts[2].z += @v_LHeight-@v_NFromTop
				pts[3].x += (@v_LWidth/2)+(@v_NWidth/2)
				#pts[3].y += @@v_LDepth
				pts[3].z += @v_LHeight-@v_NFromTop-@v_NHeight
				#create face from points
				base = @nplate_entities.add_face(pts)
				base=base.reverse!
				#Paint
				if(@@s_UseMaterials == "YES") then base.material = @s_NMaterial end
				# Now we can do the pushpull
				base.pushpull(@v_NThick)
	
				if(@@s_NOutOn == "NO") and (@@s_NInOn =="YES")
					@nplate_group.name = "NamePlate In"
					mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
					tr=Geom::Transformation.scaling mpoint,1,-1,1
					@nplate_group.transform!(tr)
				end #end if
				
				if(@@s_NOutOn == "YES") and (@@s_NInOn =="YES")
					@nplate2_group=@nplate_group.copy
					@nplate2_group.name = "NamePlate In"
					mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
					tr=Geom::Transformation.scaling mpoint,1,-1,1
					@nplate2_group.transform!(tr)
				end #end if
			end #end if
		end #end if
	
		
		#Create the Handle(s)
		#only create the handles if the leaf height is (a bit) taller than the handle height or the leaf is wide enough
		if ((@v_HFromG+@v_HFromEdge+@v_HFromEdge) < @v_LHeight) and (@v_LWidth>(@v_PanelWidth*2.3))

			#handle
			if(@@s_HOutStyle == "Handle") or (@@s_HInStyle == "Handle")
				@handle_entities = @model.active_entities
				@handle_group = @leaf_entities.add_group
				@handle_entities = @handle_group.entities
				@handle_group.name = "Handle Out"
			
				#handle plate
					pt1 = [@a_LOrigin.x+@v_LWidth-@v_HFromEdge, @a_LOrigin.y-@v_HShaft1L, @a_LOrigin.z+@v_HFromG]
					vec = Geom::Vector3d.new(0,1,0)
					#create face
					circ = @handle_entities.add_circle(pt1, vec, (@v_HShaft1D/2), @v_HSegs)
					base = @handle_entities.add_face(circ)
					if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
					base.pushpull(@v_HShaft1L)
					
				#handle shaft
					pt1 = [@a_LOrigin.x+@v_LWidth-@v_HFromEdge, @a_LOrigin.y-@v_HShaft1L-@v_HShaft2L, @a_LOrigin.z+@v_HFromG]
					vec = Geom::Vector3d.new(0,1,0)
					#create face
					circ = @handle_entities.add_circle(pt1, vec, (@v_HShaft2D/2), @v_HSegs)
					base = @handle_entities.add_face(circ)
					if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
					base.pushpull(@v_HShaft2L)
		
				#handle....... handle!
					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_LWidth-@v_HFromEdge+(@v_HHeight/2)-@v_HLength
					pts[0].y += -@v_HShaft1L-@v_HShaft2L-@v_HThick
					pts[0].z += @v_HFromG-(@v_HHeight/2)
					pts[1].x += @v_LWidth-@v_HFromEdge+(@v_HHeight/2)-@v_HLength
					pts[1].y += -@v_HShaft1L-@v_HShaft2L-@v_HThick
					pts[1].z += @v_HFromG+(@v_HHeight/2)
					pts[2].x += @v_LWidth-@v_HFromEdge+(@v_HHeight/2)
					pts[2].y += -@v_HShaft1L-@v_HShaft2L-@v_HThick
					pts[2].z += @v_HFromG+(@v_HHeight/2)
					pts[3].x += @v_LWidth-@v_HFromEdge+(@v_HHeight/2)
					pts[3].y += -@v_HShaft1L-@v_HShaft2L-@v_HThick
					pts[3].z += @v_HFromG-(@v_HHeight/2)
					#create face from points
					base = @handle_entities.add_face(pts)
					#Paint
					if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
					# Now we can do the pushpull
					base.pushpull(@v_HThick)
					
					if(@@s_HOutStyle != "Handle") and (@@s_HInStyle =="Handle")
						@handle_group.name = "Handle In"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,1,-1,1
						@handle_group.transform!(tr)
					end #end if
					
					if(@@s_HOutStyle == "Handle") and (@@s_HInStyle =="Handle")
						@handle2_group=@handle_group.copy
						@handle2_group.name = "Handle In"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,1,-1,1
						@handle2_group.transform!(tr)
					end #end if
			end #end if
			
			#push plates
			if(@@s_HOutStyle == "Push Plate") or (@@s_HInStyle == "Push Plate")
				@handle_entities = @model.active_entities
				@handle_group = @leaf_entities.add_group
				@handle_entities = @handle_group.entities
				@handle_group.name = "Handle Out"

					pts = Array.new(4) { @a_LOrigin.clone }
					pts[0].x += @v_LWidth-@v_HFromEdge-@v_HPplateW
					#pts[0].y += @@v_LDepth
					pts[0].z += @v_HFromG-(@v_HPplateH/2)
					pts[1].x += @v_LWidth-@v_HFromEdge-@v_HPplateW
					#pts[1].y += @@v_LDepth
					pts[1].z += @v_HFromG+(@v_HPplateH/2)
					pts[2].x += @v_LWidth-@v_HFromEdge
					#pts[2].y += @@v_LDepth
					pts[2].z += @v_HFromG+(@v_HPplateH/2)
					pts[3].x += @v_LWidth-@v_HFromEdge
					#pts[3].y += @@v_LDepth
					pts[3].z += @v_HFromG-(@v_HPplateH/2)
					#create face from points
					base = @handle_entities.add_face(pts)
					base=base.reverse!
					#Paint
					if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
					# Now we can do the pushpull
					base.pushpull(@v_HPplateT)

					if(@@s_HOutStyle != "Push Plate") and (@@s_HInStyle =="Push Plate")
						@handle_group.name = "Handle In"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,1,-1,1
						@handle_group.transform!(tr)
					end #end if
					
					if(@@s_HOutStyle == "Push Plate") and (@@s_HInStyle =="Push Plate")
						@handle2_group=@handle_group.copy
						@handle2_group.name = "Handle In"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,1,-1,1
						@handle2_group.transform!(tr)
					end #end if
			end
			
			#Long Bar
			if(@@s_HOutStyle == "Long Bar") or (@@s_HInStyle == "Long Bar")
				@handle_entities = @model.active_entities
				@handle_group = @leaf_entities.add_group
				@handle_entities = @handle_group.entities
				@handle_group.name = "Handle Out"

					@bracket_entities = @model.active_entities
					@bracket_group = @handle_entities.add_group
					@bracket_entities = @bracket_group.entities
					@bracket_group.name = "Handle Bracket"

					#handle plate
						pt1 = [@a_LOrigin.x+@v_LWidth-@v_HFromEdge, @a_LOrigin.y-@v_HShaft1L, @a_LOrigin.z+@v_HFromG+(@v_HLBarL/3)]
						vec = Geom::Vector3d.new(0,1,0)
						#create face
						circ = @bracket_entities.add_circle(pt1, vec, (@v_HShaft1D/2), @v_HSegs)
						base = @bracket_entities.add_face(circ)
						if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
						base.pushpull(@v_HShaft1L)
						
					#handle shaft
						pt1 = [@a_LOrigin.x+@v_LWidth-@v_HFromEdge, @a_LOrigin.y-@v_HShaft1L-@v_HShaft2L, @a_LOrigin.z+@v_HFromG+(@v_HLBarL/3)]
						vec = Geom::Vector3d.new(0,1,0)
						#create face
						circ = @bracket_entities.add_circle(pt1, vec, (@v_HShaft2D/2), @v_HSegs)
						base = @bracket_entities.add_face(circ)
						if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
						base.pushpull(@v_HShaft2L)
					
					#2nd barcket mirrored vertically about HFromG
						@bracket2_group=@bracket_group.copy
						@bracket2_group.name = "Handle Bracket"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y, @a_LOrigin.z+@v_HFromG				
						tr=Geom::Transformation.scaling mpoint,1,1,-1
						@bracket2_group.transform!(tr)
					
				#long bar
					pt1 = [@a_LOrigin.x+@v_LWidth-@v_HFromEdge, @a_LOrigin.y-@v_HShaft1L-@v_HShaft2L, @a_LOrigin.z+@v_HFromG-(@v_HLBarL/2)]
					vec = Geom::Vector3d.new(0,0,1)
					#create face
					circ = @handle_entities.add_circle(pt1, vec, (@v_HLBarD/2), @v_HSegs)
					base = @handle_entities.add_face(circ)
					if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
					base.pushpull(@v_HLBarL)

				if(@@s_HOutStyle != "Long Bar") and (@@s_HInStyle =="Long Bar")
					@handle_group.name = "Handle In"
					mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
					tr=Geom::Transformation.scaling mpoint,1,-1,1
					@handle_group.transform!(tr)
				end #end if

				if(@@s_HOutStyle == "Long Bar") and (@@s_HInStyle =="Long Bar")
					@handle2_group=@handle_group.copy
					@handle2_group.name = "Handle In"
					mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
					tr=Geom::Transformation.scaling mpoint,1,-1,1
					@handle2_group.transform!(tr)
				end #end if
			end #end if
			
			
			
			if(@@s_HOutStyle == "Panic Bolt") or (@@s_HInStyle == "Panic Bolt")
				@handle_entities = @model.active_entities
				@handle_group = @leaf_entities.add_group
				@handle_entities = @handle_group.entities
				@handle_group.name = "Handle Out"

					@bracket_entities = @model.active_entities
					@bracket_group = @handle_entities.add_group
					@bracket_entities = @bracket_group.entities
					@bracket_group.name = "Handle Bracket"

					#panic bolt bracket1
						pts = Array.new(6) { @a_LOrigin.clone }
						pts[0].x += @v_HFromEdge+@v_HFromEdge
						pts[0].y += -@v_HPanicO
						pts[0].z += @v_HFromG
						pts[1].x += @v_HFromEdge+@v_HFromEdge
						pts[1].y += -@v_HPanicO
						pts[1].z += @v_HFromG+@v_HPanicH-@v_HPanicD
						pts[2].x += @v_HFromEdge+@v_HFromEdge
						#pts[2].y += 
						pts[2].z += @v_HFromG+@v_HPanicH
						pts[3].x += @v_HFromEdge+@v_HFromEdge
						#pts[3].y += 
						pts[3].z += @v_HFromG+@v_HPanicH-@v_HPanicT
						pts[4].x += @v_HFromEdge+@v_HFromEdge
						pts[4].y += -@v_HPanicO+@v_HPanicT
						pts[4].z += @v_HFromG+@v_HPanicD
						pts[5].x += @v_HFromEdge+@v_HFromEdge
						pts[5].y += -@v_HPanicO+@v_HPanicT
						pts[5].z += @v_HFromG
						#create face from points
						base = @bracket_entities.add_face(pts)
						#Paint
						if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
						base.pushpull(@v_HPanicT)
					
					#2nd barcket mirrored horizontally about mid leaf
						@bracket2_group=@bracket_group.copy
						@bracket2_group.name = "Handle Bracket"
						mpoint = Geom::Point3d.new @a_LOrigin.x+(@v_LWidth/2), @a_LOrigin.y, @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,-1,1,1
						@bracket2_group.transform!(tr)
										
				#panic bolt bar
					pt1 = [@a_LOrigin.x+@v_HFromEdge, @a_LOrigin.y-@v_HPanicO+(@v_HPanicT/2), @a_LOrigin.z+@v_HFromG]
					vec = Geom::Vector3d.new(1,0,0)
					#create face
					circ = @handle_entities.add_circle(pt1, vec, (@v_HPanicD/2), @v_HSegs)
					base = @handle_entities.add_face(circ)
					if(@@s_UseMaterials == "YES") then base.material = @s_HMaterial end
					base.pushpull(@v_LWidth-@v_HFromEdge-@v_HFromEdge)
					
					
					
					if(@@s_HOutStyle != "Panic Bolt") and (@@s_HInStyle =="Panic Bolt")
						@handle_group.name = "Handle In"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,1,-1,1
						@handle_group.transform!(tr)
					end #end if
					
					if(@@s_HOutStyle == "Panic Bolt") and (@@s_HInStyle =="Panic Bolt")
						@handle2_group=@handle_group.copy
						@handle2_group.name = "Handle In"
						mpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+(@@v_LDepth/2), @a_LOrigin.z				
						tr=Geom::Transformation.scaling mpoint,1,-1,1
						@handle2_group.transform!(tr)
					end #end if
			end #end if
		end #end if



		#Rotate the leaf around its hinge point by the required angle
		point = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+@@v_LDepth, @a_LOrigin.z
		vector = Geom::Vector3d.new 0,0,1
		tr=Geom::Transformation.rotation point, vector, @v_Langle
		@leaf_group.transform!(tr)

	
	end #end of dm_create_leaf

	#-----------------------------------------------------------------------------------------------
	def dm_create_swing
		@model.start_operation("Create Door Swing arc")
	
		#Create and set layer
		layers = @model.layers
		layers.add(@s_LayerSwing2)
		activelayer = @model.active_layer=layers[@s_LayerSwing2]
		layer=@model.active_layer
		
		@arc_entities = @model.active_entities
		@arc_group = @leafset_entities.add_group
		@arc_entities = @arc_group.entities
		@arc_group.name = @s_SwingGrpName
		
		#do it
		cpoint = Geom::Point3d.new @a_LOrigin.x, @a_LOrigin.y+@@v_LDepth, @a_LOrigin.z+@v_SwingFromG
		vector = Geom::Vector3d.new 0,0,@v_SwingFromG+1
		radius=(@v_LWidth)
		norm=1,0,0
		
		case @@s_SwingStyle
		when "Single Swing"
			arccurve = @arc_entities.add_arc(cpoint, norm, vector, radius, 0, @v_Rad90, @v_SwingSegs) 
		when "Double Swing"
			arccurve = @arc_entities.add_arc(cpoint, norm, vector, radius, -@v_Rad90, @v_Rad90, @v_SwingSegs) 
		end #end case
		
	end	#end of dm_create_swing

	#-----------------------------------------------------------------------------------------------
	def dm_add_paint
		@model = Sketchup.active_model
		materials = @model.materials
		
	#	ADD THE DOOR FRAME MATERIAL
		if ((materials["Door-Frame"] == nil) && (materials["<Door-Frame>"] == nil))
			dframe = materials.add("Door-Frame")
			dframe.color = [255,255,200]
		end
	
	#	ADD THE DOOR LEAF MATERIAL
		if ((materials["Door-Leaf"] == nil) && (materials["<Door-Leaf>"] == nil))
			dleaf=materials.add("Door-Leaf")
			dleaf.color=[255,230,200]
		end
	
	#	ADD THE DOOR FITTINGS MATERIAL
		if (@@s_KInOn == "YES") or (@@s_KOutOn == "YES") or (@@s_HInStyle != "None") or (@@s_HOutStyle != "None")	#dont create of not needed
			if ((materials["Door-Fittings"] == nil) && (materials["<Door-Fittings>"] == nil))
				dfittings=materials.add("Door-Fittings")
				dfittings.color=[220,220,220]
			end
		end
		
	#	ADD THE DOOR NAMEPLATE MATERIAL
		if (@@s_NInOn == "YES") or (@@s_NOutOn == "YES")	#dont create of not needed
			if ((materials["Door-Nameplate"] == nil) && (materials["<Door-Nameplate>"] == nil))
				dnameplate=materials.add("Door-Nameplate")
				dnameplate.color=[190,190,190]
			end
		end
	
	#	ADD THE DOOR GLASS MATERIAL
		if (@@s_PanelStyle != "None")	#dont create of not needed
			if ((materials["Door-Glass"] == nil) && (materials["<Door-Glass>"] == nil))
				dglass=materials.add("Door-Glass")
				dglass.color=[97,106,142]
				dglass.alpha=0.6
			end
		end

	end # end of dm_add_paint
	
#-----------------------------------------------------------------------------------------------
end		#### END OF CLASS ####
#-----------------------------------------------------------------------------------------------


#-----------------------------------------------------------------------------------------------
# This shows how you can add new items to the main menu from a Ruby script.
unless file_loaded?(__FILE__)		# Add to the plugins menu once only
    plugin_menu = UI.menu("Plugins").add_item("Door Maker") { 
	    Sketchup.active_model.select_tool( DM_Create.new() ) }
    @DM_Create_Loaded = true
end
#-----------------------------------------------------------------------------
file_loaded(__FILE__)

end#module
