Advertisement
churchx

align and rotate part to sloped segment

May 20th, 2023
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. $fn=15;
  2. /* [circle offsets] */
  3. crcXoff=0;//[-20:1:20]
  4. crcYoff=0;//[-20:1:20]
  5. crctopXoff=0;//[-20:1:20]
  6. crctopYoff=0;//[-20:1:20]
  7. /* [segment height, bottom and top circle radiuses] */
  8. seghght=10;//[0:1:20]
  9. crcrad=10;//[0:1:20]
  10. crctoprad=15;//[5:1:30]
  11. /* [rotation angles] */
  12. linerotAngle=0;//[-180:15:180]
  13. fixRotAngle=0;//[-180:1:180]
  14. // 2 circles to simulate segment
  15. translate([0+crcXoff,crcYoff,0]) { %cylinder(h=.1,r=crcrad);
  16. color("blue") hull(){ translate([crcrad,0,.1])cube(.1,true); translate([-crcrad,0,.1])cube(.2,true); }
  17. color("blue") hull(){ translate([0,crcrad,.1])cube(.1,true); translate([0,-crcrad,.1])cube(.2,true); } }
  18. translate([0+crctopXoff,crctopYoff,seghght]) { %cylinder(h=.1,r=crctoprad);
  19. color("blue") hull(){translate([crctoprad,0,0])cube(.2,true); translate([-crctoprad,0,0])cube(.2,true);}
  20. color("blue") hull(){translate([0,crctoprad,0])cube(.2,true); translate([0,-crctoprad,0])cube(.2,true);} }
  21. // red line connecting points on circles at various rotation angles
  22. color("red")hull(){ translate([crcXoff,crcYoff]) rotate(linerotAngle) translate([crcrad,0,0]) cube(0.2,true);
  23. translate([crctopXoff,crctopYoff]) rotate(linerotAngle) translate([crctoprad,0,seghght]) cube(0.2,true); }
  24. module part() { // centered, X aligned, cyan top -X
  25. //translate([seghght/2-1.5,0,.5])color("cyan")cube([1,2,1],true); translate([-.5,0,.5])color("grey")cube([seghght-2-1,2,1],true); translate([0,0,-.5])color("yellow")cube([seghght-2,2,1],true);
  26. translate([-seghght/2+1.5,0,.5])color("cyan")cube([1,2,1],true); translate([.5,0,.5])color("grey")cube([seghght-2-1,2,1],true); translate([0,0,-.5])color("yellow")cube([seghght-2,2,1],true);
  27. }
  28.  
  29. rotMulti=cos(PI*linerotAngle/180);
  30. rotateFromTo( p1=[crcrad*cos(linerotAngle)+crcXoff*rotMulti,crcrad*sin(linerotAngle)+crcYoff*rotMulti,0],
  31. p2=[crctoprad*cos(linerotAngle)+crctopXoff*rotMulti,crctoprad*sin(linerotAngle)+crctopYoff*rotMulti,seghght] )
  32. {
  33. // prerotate part to align with segment surface
  34. // rotate([0,-90,fixRotAngle-90]) // if part's top in +X
  35. rotate([0, 90,fixRotAngle+90]) // if part's top in -X
  36. part();
  37. };
  38. module rotateFromTo(p1,p2){ // rotates part to align with 2-point vector line and moves to it's mid
  39. translate((p1+p2)/2) // move to mid
  40. rotate([-acos((p2[2]-p1[2])/norm(p1-p2)),0,-atan2(p2[0]-p1[0],p2[1]-p1[1]) ]) // rotate to slope
  41. children();
  42. }
Tags: OpenSCAD
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement