Skip to content

Instantly share code, notes, and snippets.

@arnholm
Last active August 13, 2020 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnholm/d47f9ed0b9a0852593ceb433163b1c64 to your computer and use it in GitHub Desktop.
Save arnholm/d47f9ed0b9a0852593ceb433163b1c64 to your computer and use it in GitHub Desktop.
// AngelCAD code.
// comparison with OpenSCAD example at
// http://forum.openscad.org/Slow-rendering-performance-and-high-memory-consumption-when-exporting-a-3D-STL-td29888.html
solid@ DrawBallPattern(double SphereDiameter,
double DistanceFactor,
double BottomOuterPitchDiameter,
double BottomInnerPitchDiameter,
double TopOuterPitchDiameter,
double TopInnerPitchDiameter,
double PitchHeight)
{
double BottomOuterPitchRadius = BottomOuterPitchDiameter/2 ;
double BottomInnerPitchRadius = BottomInnerPitchDiameter/2 ;
//radius of sphere (r)
double SphereRadius = SphereDiameter/2 ;
//Distance between the outsides of two spheres
double OutsideSeparationDistance = DistanceFactor * SphereRadius;
//Heart to heart distance between two spheres
double HeartToHeartDistance = SphereDiameter + OutsideSeparationDistance ;
//Nr of spheres wthin the ring
double A=BottomOuterPitchRadius-BottomInnerPitchRadius-2*OutsideSeparationDistance;
double B=HeartToHeartDistance;
int NrOfRings = int(floor(A/B));
//The Nr. of rings that fit in the given PitchHeight
int NrOfRows = int(floor((PitchHeight-2*OutsideSeparationDistance)/HeartToHeartDistance)) ;
//The remaining height as we only allow an integer number of spheres which won't fit exactly in Height
double Hoogte = 2*SphereRadius + (NrOfRows-1) * HeartToHeartDistance ;
double RestHoogte = PitchHeight - Hoogte ;
array<solid@> balls;
for(int r=0; r<NrOfRings; r++) {
double PitchRadius = BottomOuterPitchRadius - SphereRadius - (BottomOuterPitchRadius - BottomInnerPitchRadius) * r/(NrOfRings) ;
int NrBalls = int(floor(2*PI*PitchRadius/HeartToHeartDistance)) ;
for(int h=1; h<NrOfRows; h++) {
for(int a=1; a<=NrBalls; a++) {
solid@ ball =
rotate_z((a * 360/NrBalls))*
translate(PitchRadius, 0, RestHoogte/2 + SphereRadius + (h-1)*HeartToHeartDistance)*
sphere(r:SphereRadius);
balls.push_back(ball);
}
}
}
return union3d(balls);
}
shape@ main_shape()
{
solid@ balls = DrawBallPattern(SphereDiameter:12,
DistanceFactor:0.0813,
BottomOuterPitchDiameter:150,
BottomInnerPitchDiameter:120,
TopOuterPitchDiameter:100,
TopInnerPitchDiameter:60,
PitchHeight:255);
solid@ box = translate(-100,0,6)*cuboid(200,48,244);
return box & balls;
}
void main()
{
shape@ obj = main_shape();
obj.write_xcsg(GetInputFullPath(),secant_tolerance:-1.0);
}
$fn=25;
module DrawBallPattern (SphereDiameter, DistanceFactor, BottomOuterPitchDiameter, BottomInnerPitchDiameter, TopOuterPitchDiameter, TopInnerPitchDiameter, PitchHeight, MyColor)
{
//****************** intermediate variables ******************************
BottomOuterPitchRadius = BottomOuterPitchDiameter/2 ;
BottomInnerPitchRadius = BottomInnerPitchDiameter/2 ;
//radius of sphere (r)
SphereRadius = SphereDiameter/2 ;
//Distance between the outsides of two spheres
OutsideSeparationDistance = DistanceFactor * SphereRadius;
//Heart to heart distance between two spheres
HeartToHeartDistance = SphereDiameter + OutsideSeparationDistance ;
//Nr of spheres wthin the ring
A=BottomOuterPitchRadius-BottomInnerPitchRadius-2*OutsideSeparationDistance;
B=HeartToHeartDistance;
NrOfRings = floor(A/B);
//The Nr. of rings that fit in the given PitchHeight
NrOfRows = floor((PitchHeight-2*OutsideSeparationDistance)/HeartToHeartDistance) ;
//The remaining height as we only allow an integer number of spheres which won't fit exactly in Height
Hoogte = 2*SphereRadius + (NrOfRows-1) * HeartToHeartDistance ;
RestHoogte = PitchHeight - Hoogte ;
//******************* Export some intermediate results ***************************
echo("**************************************") ;
echo("OutsideSeparationDistance=",OutsideSeparationDistance) ;
echo("HeartToHeartDistance=",HeartToHeartDistance) ;
echo("NrRows=",NrOfRows) ;
echo("NrRings=",NrOfRings) ;
echo("RestHoogte=",RestHoogte) ;
echo("**************************************") ;
//******************* DRAW SPHERE RING PATTERN ***************************
for (r = [0 : 1 : NrOfRings-1]) {
PitchRadius = BottomOuterPitchRadius - SphereRadius - (BottomOuterPitchRadius - BottomInnerPitchRadius) * r/(NrOfRings) ;
NrBalls = floor(2*PI*PitchRadius/HeartToHeartDistance) ;
echo ("ring ", r, ": ", NrBalls, " balls") ;
for (h = [1 : 1 : NrOfRows]) {
for (a = [1 : 1 : NrBalls]) {
rotate([0, 0, (a * 360/NrBalls)]){
translate([PitchRadius, 0, RestHoogte/2 + SphereRadius + (h-1)*HeartToHeartDistance]){
color(MyColor) sphere(r=SphereRadius);
}
}
}
}
}
} //End of mocule
//************** MAIN CALCULATION PARAMETERS ********************
SphereDiameter = 12 ; //Diameter of the spheres
DistanceFactor = 0.0813 ; //Fraction of the sphere diameter that the sprehes are separates from each other
BottomOuterPitchDiameter = 150; //de diameter van onderste buitenste cirkel waarbinnen de bolletjes kunnen liggen
BottomInnerPitchDiameter = 120 ; //de diameter van onderste binnenste cirkel waarbinnen de balletjes kunnen liggen (r1)
TopOuterPitchDiameter = 100; //de diameter van bovenste buitenste cirkel waarbinnen de bolletjes kunnen liggen
TopInnerPitchDiameter = 60 ; //de diameter van bovenste binnenste cirkel waarbinnen de balletjes kunnen liggen
PitchHeight = 255 ; //De hoogte waarbinnen de balletjes kunnen liggen (H)
cBallColor = "Blue";
cCubeColor = "Red" ;
CubeSize = [200,48,244]; //The intersection cube
CubePosition = [-100,0,6] ; //Translation vector of intersection cube
DrawIntersection = true ; //if true, the intersection is drawn. If false, the union is drawn
//Draw intersection or union by calling the module
if (DrawIntersection) {
intersection(){
color (cBallColor) DrawBallPattern (SphereDiameter, DistanceFactor, BottomOuterPitchDiameter, BottomInnerPitchDiameter, TopOuterPitchDiameter, TopInnerPitchDiameter, PitchHeight, cBallColor);
translate (CubePosition) color ("red") cube (CubeSize, false);
}
}
else {
color (cBallColor) DrawBallPattern (SphereDiameter, DistanceFactor, BottomOuterPitchDiameter, BottomInnerPitchDiameter, TopOuterPitchDiameter, TopInnerPitchDiameter, PitchHeight, cBallColor);
translate (CubePosition) color ("red") cube (CubeSize, false);
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment