#!/usr/local/bin/perl # perl script for Circular Tower Rafter Calculations use CGI::Carp qw(fatalsToBrowser); #comment out this line when your done debugging this script $|++; #dont't buffer output #use strict; # file name:: circular-tower.cgi # Created on Sunday, July 15, 2007 by Sim Ayers of SBE Builders # get latest version of this script at http://www.sbebuilders.com ################################################### # You are free to customize this script as you wish. # DISCLAIMER # The information and code provided is provided 'as is' without # warranty of any kind, either express or implied. In no event # shall the Company SBE Builders be liable for any damages whatsoever # including direct, indirect, incidental, consequential, loss of # business profits or special damages, even if the author has been # advised of the possibility of such damages. # DO NOT USE THIS SCRIPT UNLESS YOU CAN FULLY AGREE WITH THIS # DISCLAIMER. # copyright(C) 2007 ################################################### =head In a circle of radius r, the length of the chord that subtends a central angle x is r * sin( x/2). This script uses the theory of an polygon inscribed inside of a circle. So the radius is the run of the polygon hips and the chord is the length of the sides of the polygon. =cut my ($content_type_printed,%data,$back_wall,$projection,$face_wall,$pitch,$show_feet_inches); # print out perl headers to browser print "Content-type: text/plain\n\n" unless $content_type_printed++; &read_input(); #read user input variables $plate_width = 5.5; $fascia_width = 1.5; $ply_cut_off = 16; if ($data{'radius'} eq ""){ # hand edit variables below for default values $radius = 96; $pitch = 8; $show_feet_inches = 'no'; $spacing = 8; $overhang = 24; } else{ $radius = $data{'radius'}; $pitch = $data{'pitch'}; $show_feet_inches = $data{'show_feet_inches'}; $spacing = $data{'spacing'}; $overhang = $data{'overhang'}; } #Global symbols # http://perldoc.perl.org/Math/Complex.html use Math::Complex qw(:trig); use POSIX qw(ceil floor); $PIE = 3.14159265358979323846; $RAD_TO_DEGREE = (180/pi); $DEGREE_TO_RAD = ($PIE/180); $RAD_TO_DEGREE45 = (0.785398163397448309616); $dxf_line_count = 100; $dxf_circle_count = 200; $arc_count = 300; $DXF_STYLE=0; #dxf Line style $DXF_LAYER=0; #dxf file LAYER number $DXF_COLOR=0; # dxf line color $DXF_TSIZE=4.0; #dxf default dxf text size $half_the_thickness_of_the_hips = 0.75; # get geometry angles for circle $run = $radius; $circumference = 2 * $PIE * $radius; $circumference_fascia_line = 2 * $PIE * ($radius + $overhang); $nsides = ceil($circumference / $spacing); $angle_deg = 360 / ($nsides *2); $common_count = $nsides; $cord = 2 * $radius * sin(($angle_deg) * $DEGREE_TO_RAD); $cord_inside = 2 * ($radius - $plate_width) * sin(($angle_deg) * $DEGREE_TO_RAD); $cord_at_overhang = 2 * ($radius + $overhang - $fascia_width) * sin(($angle_deg) * $DEGREE_TO_RAD); $area = ($circumference * $radius) / 2; $arc_segment = $radius * ($angle_deg * $DEGREE_TO_RAD); $pitch_angle = $RAD_TO_DEGREE * atan($pitch / 12); $Major_Pitch = atan($pitch / 12); $common_rafter_rise = $run * ($pitch / 12); $common_rafter_length = $run / cos(atan($pitch / 12)); $overhang_length = $overhang / cos(atan($pitch / 12)); $plywood_count = 0; $num_rows = ceil((($radius + $overhang) / cos(atan($pitch / 12)))/48); for($i=0; $i<$num_rows; $i++){ $circumference_row = 2 * $PIE * (($radius + $overhang - ((48 * $i) * cos(atan($pitch / 12))))); $count = ceil($circumference_row / (96 - $ply_cut_off)); $plywood_count += $count; #print "\n\n ply $i $count = " .convertTOfeet($circumference_row); #print "\n\n"; } print "Circular Tower Rafter Calculations"; print "\nCalculations are rounded to the nearest 1/16 inch"; print "\n\n Radius of Tower = " .convertTOfeet($radius); print "\n\n Circumference of Tower Plate Line= " .convertTOfeet($circumference); print "\n\n Circumference At Fascia Line = " .convertTOfeet($circumference_fascia_line); print "\n\n\n Common Rafter Run = " .convertTOfeet($run); print "\n\n Common Rafter Pitch = " .roundAngle($pitch); print "\n\n Common Rafter Pitch Angle = " .roundAngle($pitch_angle); print "\n\n Common Rafter Rise = " .convertTOfeet($common_rafter_rise); print "\n\n Common Rafter Length = " .convertTOfeet($common_rafter_length); print "\n\n Common Rafter Overhang Run = " .convertTOfeet($overhang); print "\n\n Common Overhang Rafter Length = " .convertTOfeet($overhang_length); print "\n\n Common Rafter Length To Fascia Line= " .convertTOfeet($common_rafter_length + $overhang_length); print "\n\n Common Rafter Spacing = " .convertTOfeet($spacing); print "\n\n Number of Common Rafters at $spacing'' O.C = " .$common_count; print "\n\n Common Rafter spacing at plate = " .convertTOfeet($cord); print "\n\n Common Rafter spacing at inside of plate = " .convertTOfeet($cord_inside); print "\n\n Common Rafter spacing at inside of fascia = " .convertTOfeet($cord_at_overhang); print "\n\n Plywood Roof Sheathing Count = " .$plywood_count; # text file with dxf header information $DXF_HEADER_FILE = "dxf_header.txt"; # text file to save dxf drawing to $DXF_File="circular-tower.dxf"; #load dxf header file $dxf_header = get_dxf_header_file($DXF_HEADER_FILE); open(FILE, ">$DXF_File") || die("$DXF_File: Can't open because ($!)."); # add dxf header information to dxf output file print FILE "$dxf_header\n0\n"; # origin center in dxf document $originX = 100; $originY = 100; $theta = (2 * $PIE) / $nsides; $start_angle = $theta / 2; $x1 = $originX; $y1 = $originY; $radius2 = $overhang + $radius; $radius3 = $overhang + $radius - $fascia_width; # draw outside circumference circular plate dxf_circle($originX,$originY,$radius); # draw insidee circumference circular plate dxf_circle($originX,$originY,$radius - $plate_width); # draw outside circumference circular fascia line dxf_circle($originX,$originY,$radius2); # draw inside circumference circular fascia line dxf_circle($originX,$originY,$radius3); # draw common rafters for($i=1; $i<=$nsides; $i++){ $x2 = $radius3 * cos(($theta * $i) + $start_angle); $y2 = $radius3 * sin(($theta * $i) + $start_angle); $x2 += $originX; $y2 += $originY; dxf_line($x1,$y1,$x2,$y2); } $str= "ENDSEC\n0\nEOF"; print FILE $str; close FILE; exit; ################################### # library sub routines follow ################################### # [Ctrl]+E for zoom extents sub dxf_line { my ($x1,$y1,$x2,$y2) = @_; $color=0; $dxf_line_count++; $str = sprintf("LINE\n5\n%dD\n8\n%d\n6\nCONTINOUS\n62\n%d\n10\n%lf\n20\n%lf\n30\n0.0\n11\n%lf\n21\n%lf\n31\n0.0\n0\n", $dxf_line_count,$DXF_LAYER,$DFX_COLOR,$x1,$y1,$x2,$y2); #print $str; print FILE $str; } #=============================================================================# sub dxf_arc { my ($x1,$y1,$radius) = @_; $start=0.0000; $end=180.0000; $arc_count++; $str = sprintf("0\nARC\n5\n26\n8\n0\n62\n%d\n10\n%lf\n20\n%lf\n30\n0\n40\n%lf\n50\n%lf\n51\n%lf\n", $DFX_COLOR,$x1,$y1,$radius,$start,$end); $str = sprintf("ARC\n5\n8B\n8\n0\n62\n%d\n10\n%lf\n20\n%lf\n30\n0.0\n40\n%lf\n50\n%lf\n51\n%lf\n0\n", $DFX_COLOR,$x1,$y1,$radius,$start,$end); $str = sprintf("ARC\n8\n0\n62\n%d\n10\n%lf\n20\n%lf\n30\n0.0\n40\n%lf\n50\n%lf\n51\n%lf\n0\n", $DFX_COLOR,$x1,$y1,$radius,$start,$end); $str = sprintf("ARC\n5\n%dA\n8\n0\n62\n%d\n10\n%lf\n20\n%lf\n30\n0.0\n40\n%lf\n50\n%lf\n51\n%lf\n0\n", $arc_count,$DFX_COLOR,$x1,$y1,$radius,$start,$end); print FILE $str; } #=============================================================================# sub dxf_circle{ my ($x1,$y1,$radius) = @_; $dxf_circle_count++; $str = sprintf("CIRCLE\n5\n10B%d\n8\n0\n10\n%lf\n20\n%lf\n30\n0\n40\n%lf\n0\n", $dxf_circle_count,$x1,$y1,$radius); print FILE $str; } #=============================================================================# sub escape_string { my($esc) = @_; $esc =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg; $esc =~ s/ /+/g; return $esc; } #=============================================================================# sub get_dxf_header_file{ my($file)=shift; my $header; open(FH, "<$file") || die("$file: Can't open because ($!)."); while(){ $header .= $_; } close(FH); return $header; } #=============================================================================# sub hypot{ my ($x,$y) = @_; my $s = sqrt( $x* $x + $y * $y); return $s; } #=============================================================================# sub convertTOfeet{ my ($decimal) = @_; return $decimal unless $show_feet_inches eq 'yes'; #print "\n inches decimal = " .$decimal; my $wholefeet = floor($decimal/12); my $wholeinch = floor($decimal - ($wholefeet*12)); my $decimalfrac = ($decimal*1) - floor($decimal); my ($frac1,$frac2,$frac_str); $frac2 = 16; if ($decimalfrac >= 0.9774) # 15/16 = 0.9375 { $wholeinch++; $decimalfrac = 0.00; if($wholeinch == 12) { $wholefeet++; $wholeinch = 0; } } if ($decimalfrac > 0.0001) { $frac1 = $decimalfrac * $frac2; my $wholefrac = floor($decimalfrac * $frac2); $frac1 = ceil($decimalfrac * $frac2); $frac1 = 15 if($frac1 == 16); ($frac1,$frac2) = check_frac($frac1,$frac2); $frac_str = sprintf(" %d/%d",$frac1,$frac2); } my $str = sprintf("%d'-%d%s''", $wholefeet,$wholeinch,$frac_str); return $str; } #=============================================================================# sub check_frac{ my ($numerator ,$denominator) = @_; return (7,8) if $numerator eq 14; return (3,4) if $numerator eq 12; return (5,8) if $numerator eq 10; return (1,2) if $numerator eq 8; return (3,8) if $numerator eq 6; return (1,4) if $numerator eq 4; return (1,8) if $numerator eq 2; return ("","") if $numerator eq 0; return ("","") if $denominator eq 0; return ($numerator ,$denominator); } #=============================================================================# sub roundAngle{ my ($angle) = @_; my $str = sprintf("%.2f",$angle); return $str; } #===================================================================# sub read_input { my($buffer) = undef; my ($item); if ($ENV{'REQUEST_METHOD'} eq 'POST') { read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); } else { $buffer=$ENV{'QUERY_STRING'}; } $buffer = $ARGV[0] if (not $buffer); my @pairs=split(/&/,$buffer); foreach $item(@pairs) { my ($key,$content)=split (/=/,$item,2); # Split into key and value. $content =~ tr/+/ /; # Convert plus's to spaces $content =~ s/%(..)/pack("c",hex($1))/ge; # Convert %XX from hex numbers to alphanumeric $content =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # prevent hackers from exploiting the input name $key =~ tr/+/ /; # Convert plus's to spaces $key =~ s/%(..)/pack("c",hex($1))/ge; # Convert %XX from hex numbers to alphanumeric $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $key =~ s/ /_/g; # get rid of attempts to insert HTML tags $content =~ s///g; # server-side-includes $content =~ s/<([^>]|\n)*>//gs; $content =~ s//>/g; # get rid of attempts to insert illegal characters $content =~ s/\\//g; # remove black slashes $content =~ s/\0//g; # remove nulls $content =~ s/[\\\&\;\`\'\"\|\*\?\~\^\[\]\{\}\$]//gs; $content =~ s/\cM/\n/g; #convert CR to LF $content =~ s/^\s+|\s+$//gs; $content = substr($content,0,4096); $data{$key} = $content; } return 1; }