// pullies 11 variable macro Lathe
// Vers: 3.6
// Auth: Leroy Whetstone Email lrwii@joplin.com
// Display: Width=640; Height=480;
// uses Get to hide display

// you can use this two ways include it once 
// and you have the marco you can call
// include it more than once and with the variables 
// changed and get a pully

//#macro Pully(D1,D2,D3,D4,D5,D6,T1,T2,T3,T4,T5)

//  T1,T2          
//  ||T5||    D5
//   \\_//    D4
//   |T3| ___ D3
//  |___ |    D2
// T4         D1


//====================================================================

#ifndef(Get)
 #include "colors.inc"
 #include "finish.inc"
 global_settings {assumed_gamma 1 max_trace_level 20}

 camera{ location <0,0,-8>
        look_at <0,0,0>
        right x*image_width/image_height
        }
 light_source{<0,20,0> color White}
// light_source{<0,-20,0> color White}
// light_source{<0,0,20> color White}
 light_source{<0,0,-20> color White*.5}
 light_source{<20,0,-20> color White*.5}
 light_source{<-20,0,-20> color White*.5}
 #declare Pully_Debug=0;
#end

 #ifndef(Pully_Uses_Variables) 
  #declare Pully_Debug=0;
  #debug"\n\nPully system Active !!!\n\n"
  #declare Pully_Uses_Variables=1; 
 #else #declare Pully_Uses_Variables=2; 
 #end


#if(Pully_Uses_Variables>1)
 #ifndef(PullyName) #declare PullyName="Untitled";  #end
 #ifndef(PullyDiameter) #declare PullyDiameter= 3;  #end
 #ifndef(ShaftDiameter) #declare ShaftDiameter=.5;  #end
 #ifndef(ShaftSuportDiameter) #declare ShaftSuportDiameter=.75; #end
 #ifndef(ShaftSuportThickness) #declare ShaftSuportThickness=1;#end
 #ifndef(WallThickness) #declare WallThickness=.25; #end
 #ifndef(BeltSuportDiameter) #declare BeltSuportDiameter=2; #end
 #ifndef(BeltTopWidth) #declare BeltTopWidth=.5;  #end
 #ifndef(BeltLowerWidth) #declare BeltLowerWidth=.25; #end
 #ifndef(BeltTopHeigth) #declare BeltTopHeigth=.25;  #end
 #ifndef(BeltLowerHeigth) #declare BeltLowerHeigth=.25; #end
 #ifndef(BeltSuportThickness) #declare BeltSuportThickness=.1;#end
 #ifndef(Pully_Debug)  #declare Pully_Debug=0;  #end

 #declare PD1=ShaftDiameter;
 #declare PD2=ShaftSuportDiameter;
 #declare PD3=BeltSuportDiameter;
 #declare PD4=PullyDiameter;
 #declare PL1=BeltTopHeigth;
 #declare PL2=BeltLowerHeigth;
 #declare PL3=BeltSuportThickness;
 #declare PT1=BeltTopWidth+2*BeltSuportThickness;
 #declare PT2=WallThickness;
 #declare PT3=ShaftSuportThickness;
 #declare PT4=BeltLowerWidth;
 #if(PD3>PD4-(PL1+PL2+PL3)*2) 
   #declare PD3=PD4-(PL1+PL2+PL3)*2;
    #debug concat("\n Warning! ",PullyName," Pully: BeltStartDiameter changed to ",str(PD3,3,3)," !!! \n")
 #end
 #if(PD3<PD2) 
   #declare PD3=PD2;
    #debug concat("\n Warning! ",PullyName," Pully: BeltStartDiameter changed to ShaftSuportDiameter : ",str(PD3,3,3)," !!! \n")
 #end
 Pully(PD1,PD2,PD3,PD4,PL1,PL2,PL3,PT1,PT2,PT3,PT4)
#else
 #macro Pully(D1,D2,D3,D4,L1,L2,L3,T1,T2,T3,T4)
  #local R6=D4/2;
  #local R1=D1/2;
  #local R2=D2/2;
  #local R3=D3/2;
  #local R4=R6-L1-L2; //#debug concat("R4 =",str(R4,3,3),"\n")
  #local R5=R6-L1;  //#debug concat("L2 =",str(L2,3,3),"\n")
  #local S1=T1/2+L3; 
  #local S2=T1/2;
  #local S3=T2/2;
  #local S4=T3/2;
  #local S5=T4/2;   //#debug concat("S5 =",str(S5,3,3),"\n")
  lathe{linear_spline
   20
   <R1,0>,<R1,S4>,<R2,S4>,<R2,S3>,<R3,S3,>,  
   <R5,S1>,<R6,S1>,<R6,S2>,<R5,S2>,<R4,S5>,<R4,-S5>,<R5,-S2> 
   <R6,-S2>,<R6,-S1>,<R5,-S1>,
   <R3,-S3,>,<R2,-S3>,<R2,-S4>,<R1,-S4>,<R1,0>
  rotate x*90
  }
  #if(Pully_Debug) 
   #debug"\n\n"
   #debug"#declare Pully=lathe{ linear_spline\n"
   #debug" 20\n"
   #debug concat(" <",vstr(2,<R1,0>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R1,S4>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R2,S4>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R2,S3>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R3,S3>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R5,S1>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R6,S1>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R6,S2>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R5,S2>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R4,S5>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R4,-S5>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R5,-S2>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R6,-S2>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R6,-S1>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R5,-S1>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R3,-S3>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R2,-S3>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R2,-S4>,",",4,4),">,\n")
   #debug concat(" <",vstr(2,<R1,-S4>,",",4,4),">,")
   #debug concat(" <",vstr(2,<R1,0>,",",4,4),">\n")
   #debug concat(" rotate x*90}\n\n\n")
  #end
 #end
#end

#ifndef(Get)
//test objects
//Top row
 #declare Pully_Debug=1;
 object{Pully(.5,1,4,5,.2,.3,.2,.75,.5,.6,0) 
  pigment{Gold*.5}
  finish{Glossy}
 rotate <0,45,0>
 translate <5,4,10>}
 //#debug "Hit "

  #declare Pully_Debug=0;
 object{Pully(.5,1,3,6,.35,.35,.1,1,.5,.2,.2) 
  pigment{rgb .6}//gradient y scale .1}
  finish{Glossy}
  rotate <-10,0,0>
 translate <0,4,10>}
 //#debug "Hit "

  #declare Pully_Debug=0;
 object{Pully(.5,1,2,4,.1,.3,.05,1,.5,.6,.01) 
  pigment{rgb .3}
  rotate <0,-45,0>
  finish{Glossy}
 translate <-5,4,8>}
  // #debug "Hit "
//second row
  #declare Pully_Debug=0;
 object{Pully(.75,1,3,4,.25,.25,.1,1,.25,.5,.02) 
  pigment{Gold*.5}
  finish{Glossy}
 rotate <0,-60,0>
 translate <-8,2,10>}
  //#debug "Hit "

  #declare Pully_Debug=0;
 object{Pully(.75,1,3,4,.2,.2,.01,.5,.25,.5,.02) 
  pigment{rgb .3}
  finish{Glossy}
 rotate <0,60,0>
 translate <8,2,10>}
  //#debug "Hit "

//third row
  #declare Pully_Debug=0;
 object{Pully(.5,.75,1,3,.25,.25,.1,.5,.2,.3,0) 
  pigment{rgb .3}
  finish{Glossy}
 rotate <0,-80,0>
 translate <-9,-2,10>}
  //  #debug "Hit "

  #declare Pully_Debug=0;
 object{Pully(.5,.75,2,3,.2,.25,.05,.5,.2,.3,.1) 
  pigment{Gold*.5}
  finish{Glossy}
 rotate <0,80,0>
 translate <9,-2,10>}
  //  #debug "Hit "

// display 

text {
  ttf             
  "crystal.ttf",  
  "Pully(  ,  ,  ,  ,  ,  ,  ,  ,  ,  ,  )",      
  .1,       
  0         
 pigment{Gold}
 translate <-10,6,7.5>
 no_shadow
}
text {
  ttf             
  "crystal.ttf",  
  "      D1 D2 D3 D4                     ",      
  .1,       
  0         
 pigment{rgb<.7,.5,.6>}
 translate <-10,6,7.5>
 no_shadow
}
text {
  ttf             
  "crystal.ttf",  
  "                  L1 L2 L3            ",      
  .1,       
  0         
 pigment{rgb<.5,.7,.5>}
 translate <-10,6,7.5>
 no_shadow
}
text {
  ttf             
  "crystal.ttf",  
  "                         T1 T2 T3 T4 ",      
  .1,       
  0         
 pigment{rgb<.4,.4,.7>}
 translate <-9,6,7.5>
 no_shadow
}

 // don't change display pully
 intersection{
  box{<7,7,2><0,-6,-2> pigment{Red}}
  object{Pully(.5,1,3,6,.35,.35,.1,1,.5,.2,.1)} 
 pigment{rgb .6}//gradient y scale .1}
 finish{Glossy}
 rotate <90,-90,0>
 translate -y*3}

 //cylinder{-y*7,-y*2,.02 pigment{rgb<.7,.5,.6>}}
 #declare Point=
  union {cone{0,0,y*.5,.1}
        cylinder{y*.4,y*2,.04}
        pigment{rgb<.7,.5,.6>}no_shadow}
 #declare Pointa=
  union {cone{0,0,y*.5,.1}
        cylinder{y*.4,y*2,.04}
        pigment{rgb<.5,.7,.5>}no_shadow}

//D1
 object{Point scale <1,.5,1> translate <.25,-2.9,0>} 
 object{Point scale <1,.5,1> translate <-.25,-2.9,0>}
   union{
    cylinder{<.25,0,0><-.25,0,0>,.02 }
    cone{<.25,0,0>,.02,<.15,0,0>,.07}
    cone{<-.25,0,0>,.02,<-.15,0,0>,.07}
   pigment{rgb<.7,.5,.6>} translate -y*2.25 no_shadow}
//D2    
 object{Point scale <1,.75,1> translate <.5,-2.75,0>}
 object{Point scale <1,.75,1> translate <-.5,-2.75,0>}
   union{
    cylinder{<.5,0,0><-.5,0,0>,.02 }
    cone{<.5,0,0>,.02,<.3,0,0>,.05 }
    cone{<-.5,0,0>,.02,<-.3,0,0>,.05}
   pigment{rgb<.7,.5,.6>}translate -y*1.5 no_shadow}
//D3 
 object{Point   translate <1.5,-2.75,0>}
 object{Point   translate <-1.5,-2.75,0>}
   union{
   cylinder{<1.5,0,0><-1.5,0,0>,.02 }
   cone{<1.5,0,0>,.02,<1.3,0,0>,.05 }
   cone{<-1.5,0,0>,.02,<-1.3,0,0>,.05}
   pigment{rgb<.7,.5,.6>}translate -y*.9 no_shadow}

//D4
 object{Point scale <1,.95,1> translate <3,-2.4,0>}
 object{Point scale <1,.95,1> translate <-3,-2.4,0>}
  union{
   cylinder{<-2.75,-.25,0><2.75,-.25,0>,.02}
   cone{<2.75,-.25,0>,.06,<3,-.5,0>,0}
   cone{<-2.75,-.25,0>,.06,<-3,-.5,0>,0}
   pigment{rgb<.7,.5,.6>}no_shadow}

//L1
  object{Pointa  scale<1,.9,1> translate <2.65,-2.4,0>}
   union{
    cylinder{<2.65,0,0><4,0,0>,.02 }
    cone{<2.65,0,0>,.02,<2.8,0,0>,.06}
    cone{<3,0,0>,.02,<2.85,0,0>,.06}
    pigment{rgb<.5,.7,.5>}translate -y*1.1 no_shadow}

//L2
 object{Pointa  scale<1,.9,1> translate <2.3,-2.9,-.01>}
   union{
    cylinder{<2.3,0,0><4,0,0>,.02 }
    cone{<2.3,0,0>,.02,<2.45,0,0>,.06}
    cone{<2.65,0,0>,.02,<2.5,0,0>,.06}
   pigment{rgb<.5,.7,.5>} translate -y*1.66 no_shadow}

//L3
 object{Pointa scale <.5,.4,.5> rotate -z*40 translate <3,-2.4,0>}
 object{Pointa scale <.5,.4,.5> rotate -z*90 translate <3,-2.5,0>}  

 text {ttf "crystal.ttf","D1",.1,0  pigment{rgb<.7,.5,.6>}
       scale<.3,.5,.3>  translate <-.1,-2.1,0> no_shadow}
 text {ttf "crystal.ttf","D2",.1,0  pigment{rgb<.7,.5,.6>}
       scale<.3,.5,.3> translate <-.1,-1.4,0> no_shadow}
 text {ttf "crystal.ttf","D3",.1,0  pigment{rgb<.7,.5,.6>}
       scale<.3,.5,.3> translate <-.1,-.8,0> no_shadow}
 text {ttf "crystal.ttf","D4",.1,0  pigment{rgb<.7,.5,.6>}
       scale<.3,.5,.3> translate <-.1,-.22,0> no_shadow}
 text {ttf "crystal.ttf","L2",.1,0  pigment{rgb<.5,.7,.5>}
       scale<.3,.5,.3> translate <3.4,-1.65,0> no_shadow}
 text {ttf "crystal.ttf","L1",.1,0  pigment{rgb<.5,.7,.5>}
       scale<.3,.5,.3> translate <3.4,-1.05,0> no_shadow}
 text {ttf "crystal.ttf","L3",.1,0  pigment{rgb<.5,.7,.5>}
       scale<.3,.5,.3>  translate <3.4,-2.4,0> no_shadow}

 #declare Point2=
  union {cone{0,0,-x*.5,.06}
        cylinder{-x*.5,-x*1,.04}
        pigment{rgb<.4,.4,.7>}}

//T1
 object{Point2 scale <.8,1,1> translate <-3,-2.5,0>}
 object{Point2 scale <.75,1,1> translate <-3,-3.5,0>}
  cone{<-3.7,-2.5,0>,.02,<-3.7,-2.75,0>,.05 pigment{rgb<.4,.4,.7>}}
  cone{<-3.7,-3.5,0>,.02,<-3.7,-3.25,0>,.05 pigment{rgb<.4,.4,.7>}}
//T3
 union{
  cylinder{<0,-2.95,0>,<0,-3.05,0>,.04 }
  cylinder{<0,-3,0>,<-.2,-3,0>,.03 }
  cone{<0,-2.75,0>,.0,<0,-2.95,0>,.06 }
  cone{<0,-3.25,0>,.0,<0,-3.05,0>,.06}
  pigment{rgb<.4,.4,.7>} translate <-1,0,-.01>}    

//T4
 object{Point2 scale .5  rotate z*185 translate <.45,-2.9,-.1>}
 object{Point2 scale .5  rotate z*170 translate <.45,-3.1,-.1>}
//T5
 object{Point2 scale .5  rotate -z*10 translate <2.3,-2.95,-.1>}
 object{Point2 scale .5  rotate z*10 translate <2.3,-3.05,-.1>}

 text {ttf "crystal.ttf","T1",.1,0  pigment{rgb<.4,.4,.7>}
       scale<.3,.5,.3>  translate <-3.7,-3.2,0> no_shadow}
 text {ttf "crystal.ttf","T2",.1,0  pigment{rgb<.4,.4,.7>}
       scale<.3,.5,.3>  translate <-1.5,-3.2,-.1> no_shadow}
 text {ttf "crystal.ttf","T3",.1,0  pigment{rgb<.4,.4,.7>}
       scale<.3,.5,.3>  translate <.99,-3.2,-.1> no_shadow}
 text {ttf "crystal.ttf","T4",.1,0  pigment{rgb<.4,.4,.7>}
       scale<.3,.5,.3>  translate <1.5,-3.2,-.1> no_shadow}


#end