/r/openscad

Photograph via snooOG

Share news, tips and tricks, and ask questions about how to use 3D CAD modelers for programmers, such as OpenSCAD, CoffeeSCAD, and ImplicitCAD

Quick Links

/r/openscad

5,820 Subscribers

1

Render vs Preview

I don't see a difference between the Render and Preview buttons. They seem to do the same thing.

6 Comments
2024/12/02
22:29 UTC

5

Built my first part and printed it

This is a rite of passage rant, please feel free to delete if unnecessary.

I am building a fixed wing glider and needed some supports for the wing, usually I'd fire up Fusion 360, draw sketches and extrude it out, incrementally drawing on faces to get what I want, then eventually running into a bottleneck when I realize I want to change some intrinsic param like material thickness (I know you can make this into params)

With openscad, I struggled a lot at the start - I couldn't get shapes to land where I wanted them to. I struggled through and finished one side of my symmetric part. I was dreading the idea of building the other side, having to compute all of those points by hand, then I remembered, hey I can maybe try mirror and call this module again and - WHAM!

It was so satisfying, I printed it - and half way through realized I wanted a different material thickness, and change the size one of the dimensions, easy peasy, just change the params and the model refreshed - my model is quite simple, so it might not be so easy but still.

In summary, it is so satisfying to programmatically compose shapes into objects that you want, and furthermore - printing it, it's the physical realization of code, which doesn't happy very often in programming.

End of rant! :)

10 Comments
2024/12/02
18:43 UTC

2

Getting points from a solid?

I’m wondering if there is a method to retrieve the vertices from a solid, e.g. as created by torus().

I wish to deform the points in a non-linear way, and I can’t figure out a good way to do it with CSG. If I can get the vertices, I would operate on them point by point, and save myself the trouble of creating a non-linear solid with appropriate vertices and faces.

16 Comments
2024/12/01
19:58 UTC

1

facing issue with my code to create a vase..

I want to be able to create a vase quickly by changing the parameters such as height, neck diameter, shoulder diameter, location of neck and shoulder etc.

I am trying to get it looking like this with ribbed effect

https://preview.redd.it/kirfuzywj94e1.png?width=512&format=png&auto=webp&s=3b86a7d44b1109d58879185937a681074fd0f207

my code

// Parameters

vase_height = 240; // Total height of the vase

base_radius = 50; // Radius of the base

neck_radius = 25; // Radius of the neck opening

rib_count = 55; // Number of ribs

rib_depth = 3; // Depth of each rib

wall_thickness = 3; // Thickness of the vase walls

shoulder_height = 150; // Height of the shoulder (widest part)

// Main Module to Create the Ribbed Vase

module ribbed_vase() {

difference() {

// Outer vase shape

rotate_extrude($fn = 360)

translate([0, 0, 0])

polygon([

[0, 0], // Base center

[base_radius, 0], // Base radius

[base_radius, shoulder_height], // Shoulder height

[neck_radius, vase_height], // Neck opening

[0, vase_height] // Top center

]);

// Hollow out the inside

rotate_extrude($fn = 360)

translate([0, 0, 0])

polygon([

[0, 0], // Base center

[base_radius - wall_thickness, 0], // Inner base radius

[base_radius - wall_thickness, shoulder_height], // Inner shoulder

[neck_radius - wall_thickness, vase_height], // Inner neck

[0, vase_height] // Inner top center

]);

}

// Add ribs to the vase

for (angle = [0 : 360 / rib_count : 360 - 360 / rib_count]) {

rotate([0, 0, angle])

for (z = [0 : 10 : vase_height]) {

radius_at_z = base_radius - ((base_radius - neck_radius) * (z / vase_height));

translate([radius_at_z, 0, z]) {

rotate([90, 0, 0])

cylinder(r = rib_depth, h = 1, center = true);

}

}

}

}

// Render the Ribbed Vase

ribbed_vase();

I am getting:

https://preview.redd.it/6wshbgo0k94e1.png?width=1013&format=png&auto=webp&s=15721d68eff32c77a4ab4606e82df76935bdf76f

6 Comments
2024/12/01
16:26 UTC

10

Giants Causeway Generator

Hi everyone,

Long time lurker, first time poster here! I have wanted to learn OpenSCAD for a long time but didn't have any idea that made me really want to dive in. I made a small little tray with pockets but that wasn't fulfilling and I left the app for a while...

So after visiting Ireland and a place called the Giants Causeway and I thought it was perfect for something that OpenSCAD could generate for me because I love the look of the hexagons.

So with my dusted off trig knowledge from high-school, the OpenSCAD cheatsheet, and some help from ChatGPT with the tricky maths...

I present the Giants Causeway Generator!

I would love any feedback :)

https://preview.redd.it/4kek4i55044e1.png?width=1893&format=png&auto=webp&s=2b61d090a2d3c303d0050b60022055bc730be5ba

Edit: grammar

4 Comments
2024/11/30
21:47 UTC

7

Any way to do a "pillowing" effect?

We all know about the linear_extrude(...) function that turns 2d things into 3d things.

I sometimes have found myself wishing for a similar function which could make a more rounded, result.

Just to illustrate what I'm hoping to achieve, my dream solution would, given this 2d outline:

https://lemmy.world/pictrs/image/1e3f6c90-485a-4aeb-b9be-d9d5ba7dd3e0.png

would give me something like the following:

https://lemmy.world/pictrs/image/9e47b16c-84ca-45d8-83a3-678974b5c2ca.png

https://lemmy.world/pictrs/image/3dc8f48a-48f5-413f-b873-17127097fb4a.png

Just to further illustrate, the code I used to generate outline above:

hull() {
	for (i=[-1:1])
		translate([i*15, 0])
			circle(d=10);
}

And the "pillowed" version that shows the desired result giving the above outline:

$fn=64;
rotate([0, 90, 0])
	linear_extrude(30, center=true)
		scale([4, 10])
			difference() {
				circle(d=1);
				translate([0.5, 0])
					square(1, center=true);
			}
for (i = [-1, 1])
	translate([i*15, 0, 0])
		scale([10, 10, 4])
			difference() {
				sphere(d=1);
				translate([0, 0, -0.5])
					cube(1, center=true);
			}

The outline I actually want to pillow for my particular current use case looks like this:

https://lemmy.world/pictrs/image/7a65eed3-8fca-4c2a-b534-4edc8123c9c6.png

(Credit to this person on Printables for the Talavera tile pattern.)

I'm hoping there's a way to do this I'm not thinking of, or a library I'm not familiar with. The example above makes an elliptical curve, but I'm not married to elliptical in particular. Anything somewhat similar would be fine.

Thanks in advance for any help!

Edit:

Thanks for all the replies, folks!

I've looked through most of what folks had to say and here's what I've got so far:

  • Rebuilding The 2D Outline/Geometry From Scratch - Yeah, theoretically I see how that could help, but it's not really a full solution, and I'm not sure what the rest of the solution would be unless it was one of the two following options.
  • Round Anything's polyRoundExtrude() - It looks like this would require recreating from scratch using Round Anything's way of doing things.
  • BOSL2's offset_sweep() - Similarly would require rebuilding from scratch using BOSL2's way of doing things.
  • roof() - Yeah, the chunkiness and unavailability in the latest release are both drawbacks, but theoretically it could be the best option.
  • minkowski() - You weren't joking that this was slow!

But! I think I've found a solution I'm happy with. It also has drawbacks, but they're drawbacks I'm more willing to live with than those from the above options.

Ultimately, I'm planning to 3d-print the result on an FFF printer. (A Creality Ender 3 Pro and/or Ender 3 V2 Neo specifically.) I'm probably going to use the thinnest available layer height, which in Cura is 0.12mm.

The reason I went into all of that is just to say that while I want it smooth, I don't actually need it any smoother than my printer's best/smallest layer height. If it's only smooth to a resolution of 0.12mm, that's fine.

So, the solution I came to is to construct the elliptical curve like a stepped pyramid, which layers 0.12mm thick. To make it elliptical, I just used the equation for a unit circle: y=sqrt(1-x^2). Here's the code for this relatively simple solution:

module pillow(height, delta) {
	for (i = [0:floor(height/delta)])
		linear_extrude(i*delta)
			let(
				x=i/floor(height/delta),
				c=(1-sqrt(1-x^2))*height
			)
				offset(delta=-c)
					children();
}

pillow(1.5, 0.12)
	hull()
		for (i=[-1:1])
			translate([i*15, 0])
				circle(d=10);

And the result looks like:

https://lemmy.world/pictrs/image/2e1be23f-2cd6-45a4-af50-3cd64f26d776.png

https://lemmy.world/pictrs/image/f0185323-cbd2-454e-bba5-6f4523aa0432.png

Drawbacks:

  • This solution is a little slow. Nowhere near as slow as the minkowski() solution, but kinda slow.
  • The stepping probably better be tailored to your specific planned layer height. Which means if you want to reprint with a different layer height, you're probably best off to start by making a change (a small change, but a change none the less) to the .scad file.

Just as one final point, while I haven't yet had occasion to mess with roof() to really know the details of how it works and what it can do, I think the solution I came up with could be adapted to use roof() to make a much smoother and less stepped look. (Only in a version of OpenSCAD that supports roof() of course.)

That's it. I figured I'd detail my solution in case it'd help anyone else.

Thanks again for the input!

8 Comments
2024/11/29
23:06 UTC

0

Normalized tree is growing past 200000 elements. Aborting normalization.

Why won't this render? I borrowed the rounded rectangle code from somewhere. This code makes custom-sized pegboards, which rounded rectangle holes. It started working, and then it just stopped.

    /* [General Settings] */
    // Width of the board
    Width = 200;
    // Height of the domain
    Height = 200;

    /* [Layout Setting] */
    // Thickness of the board
    Thickness = 4.5;
    // Rounded corner radius
    Radius = 10;

    X_Count = round(Width / 40);
    Y_Count = round(Height / 40);

    echo(str("X_Count: ", X_Count));
    echo(str("Y_Count: ", Y_Count));

    $fn = 10;

    module rounded_edge(r, h) 
    {
        translate([r / 2, r / 2, 0])
        {
            difference() 
            {
                cube([r + 0.01, r + 0.01, h], center = true);
                translate([r/2, r/2, 0])
                    cylinder(r = r, h = h + 1, center = true);
            }
        }
    }

    //creates two rounded edges
    module semi_rounded_rectangle(x,y,z,roundness) 
    {
        difference() 
        {	
            cube([x,y,z],center=true);
            translate([-x/2,-y/2,0]) 
                rounded_edge(roundness,z);
            rotate([0,0,-180]) 
                translate([-x/2,-y/2,0]) 
                    rounded_edge(roundness,z);
            rotate([0,0,90]) 
                translate([x/2,y/2,0]) 
                    rounded_edge(roundness,z);
        }
    }

    module rounded_rectangle(x, y, z, roundness) 
    {
        intersection() 
        {
            semi_rounded_rectangle(x, y, z, roundness);
            mirror([1,0,0]) 
                semi_rounded_rectangle(x, y, z, roundness);
        }
    }

    module hole(x, y)
    {
        translate([x, y, 0])
            rounded_rectangle(4.5, 10, Thickness, 2);
    }

    difference() 
    {
        translate([Width / 2, Height / 2, 0])
            rounded_rectangle(Width, Height, Thickness, Radius);
        for (x = [0:X_Count])
        {
            for (y = [0:Y_Count])
            {
                hole(20 + x * 40, 20 + y * 40);
    //            translate([40 + x * 40, 40 + y * 40, 0])
    //                hole();
            }
        }
    }
6 Comments
2024/11/29
20:20 UTC

1

roof with shape other than straight line

I just discovered how to use roof().

Roof() grows a roof along a straight line at 45 degrees from horizontal from all perimeters to meet in center of perimeters. Using difference() allows one to turn that into a very nice chamfer on an arbitrary shape!

However, I'd like to use something other than a straight line at 45 degrees. Straight line at 20 degrees, or any arbitrary angle. Or an arbitrary function (circular path, polynomial) rather than a straight line.

Is something that the developers could code easily? Seems like it should be.

I the roof() code available to us?

2 Comments
2024/11/29
18:53 UTC

4

proof of concept tool on top of OpenSCAD

https://preview.redd.it/90diavy2nt3e1.png?width=1118&format=png&auto=webp&s=48680f5e4d2f52fd5b68ad4e514a42845ee55480

hi r/openscad,

I am working on a tool to extend OpenSCAD functionalities and I've made this proof of concept model to figure out if I am on the right track or not.

Basically, I am trying to do two things;

  1. Make the customizer more user friendly by adding performance parameters. So besides the usual geometric values (length, thicknesses and so on) you can specify what you want from the model (in the example glider above, how far it should glide/how fast it should be).
  2. Bundle all the collaterals (documentation, images etc) for a project in a single convenient place

What do you think? Do you see any value in it or am I focusing on the wrong problem?

Any feedback is valuable feedback!

27 Comments
2024/11/29
10:56 UTC

2

How to "remember" data from a previous loop?

In a loop I need to use a variable from the previous loop, then that variable needs to be updated for the next loop.

But I can't get this to work since it doesn't execute line by line. Compiled, not interpreted? You know what I mean, I hope.

Is there a way to achieve this? I can't seem use lists because I need the previous variable to create the next one.

14 Comments
2024/11/29
02:49 UTC

1

Object may not be a valid 2-manifold and need repair

// Pyramid Parameters

base_length = 1.59; // Length of the base

height = 2.25; // Height of the pyramid

module pyramid(base, height) {

// Define the vertices of the pyramid

hull() {

translate([-base / 2, -base / 2, 0]) sphere(0.01); // Bottom-left

translate([base / 2, -base / 2, 0]) sphere(0.01); // Bottom-right

translate([base / 2, base / 2, 0]) sphere(0.01); // Top-right

translate([-base / 2, base / 2, 0]) sphere(0.01); // Top-left

translate([0, 0, height]) sphere(0.01); // Apex

}

}

// Center the pyramid at the origin

pyramid(base_length, height);

Not sure what the issue is. I figured this would be straight forward using code instead of clunky CAD UI. Are these common bugs?

9 Comments
2024/11/28
22:10 UTC

0

List, what am I writing wrong?

listname[0]=[0,0,0];

This gives me a parsing/syntax error.

Does anyone know why? Is this not how you define an item in a list?

I also tried listname[0] =[[0,0,0]]; which didn't work either

I realize removing [0] will make it work, but I need to define different items at different points, so I have to be able to specify the item.

6 Comments
2024/11/28
20:15 UTC

0

Help me with my openSCAD school assigment

Hello, I need help solving this assignment for my school project. Any suggestions or ideas would be really helpful. Thank you, everyone! <3

https://preview.redd.it/k9t2a8qnuo3e1.png?width=500&format=png&auto=webp&s=31f7439f050846ec7825f6db798c8c7238589b26

11 Comments
2024/11/28
18:52 UTC

1

force openscad to use goldenfeather from command line

I fail to render from cmd on a virtual machine Windows server 2019. (it is ok on laptop)
But if I use GUI i can make it work by selecting Edit>Preferences>Advanced>Force Goldfeather. Then i renders ok there.
This setting is not used from the command line.

How can i make the command line openscad.exe use Goldenfeather?
Is there a switch that can be used or can i point it to the config that the GUI use?

ps. apparently the GUI stores these settings in the registry. Can the command line openscad.exe read the registry?

/Rullbandspelare

3 Comments
2024/11/28
11:18 UTC

2

Help me with rounded corners of a cylinder !! please

Hi all,

here is my problem :

I have a parametric cylinder with a cut inside (To make some caps)

I can control the Depth, inside Diameter and Thickness of the walls.

I would like to round the top and/or (with a parametric value) the bottom edges WITHOUT modifying the inside diameter and inside Depth

I play with minkowski, hull, but can't figure how to acheive that It should be easy but I'm not an expert.

here is my code:

/* [General settings] */
Diameter = 60.5; //[10:0.1:250]
Thickness = 1.8; //[1.5:0.1:10]
Depth = 6.5; //[3:0.1:100]
V4_Top_radius = 1.1; //[0:0.1:5]
V4_Bottom_radius = 1.1; //[0:0.1:5]

/* [Construction assistance] */
Cut = false;
$fn=180;
/////////////////////////////////////////////////////////

difference() {  //Cut
difference () {
translate([0,0,0]) cylinder(h= Depth+3, r= Diameter/2+Thickness);
translate([0,0,3]) color("gray") cylinder(h= Depth+0.1, d= Diameter);
}
//Cut ////////////////////
color("Red")
if (Cut)
translate([0, -40, 0]) cube([150,80,160],center=true);
}
17 Comments
2024/11/27
12:18 UTC

5

Input from the Community about Development Builds

I run a hosted OpenSCAD environment at models.makewithtech.com. This site is designed to provide an OpenSCAD customizer environment for scripts hosted on the site and for those uploaded by users. It is completely free, and anyone can upload and share a script.

The scripts are rendered in a Linux environment using OpenSCAD releases obtained via:

apt-get install -y openscad-nightly or

apt-get install -y openscad

The images are updated nightly.

In addition to hosting, the site offers an online OpenSCAD-aware editor and several enhancements to the OpenSCAD customizer environment. These include the ability to bundle modules, fonts, and files into a ZIP file for use with your script during execution. Users can also flag favorite models (scripts) hosted on the site, browse and use models hosted on Thingiverse, and create scripts from scratch.

When performing a preview or render, users have the option of using the official 01/2021 release or a development build.

I have a question for the community: Since the latest development build might contain a bug that could affect your script, I want to provide an older development build in addition to the current one. For example, I could offer the official build, a build from the first of last month, and the current build.

Which three OpenSCAD executables would you recommend I offer to users?

8 Comments
2024/11/26
21:51 UTC

2

Help with this assignment

5 Comments
2024/11/26
08:16 UTC

0

Notepad++ Load Bypass

You make a change to your code, save, hit F6. Processor laughs at your polygon count and takes a vacation. Can’t change it back, OpenSCAD’s non-responsive, re-opening the file just repeats the problem.

If you need to break the cycle of this problem I have discovered Notepad++ will edit .SCAD files, so you can change your $fn=72 back to $fn=6 and continue work. This may be known or obvious already but it’s saved me a couple of times on my current job and thought users getting caught in this loop might appreciate the tip.

6 Comments
2024/11/25
16:57 UTC

1

Openscad Parser/ syntax error

Hi guys, new to Opens cad and I’m encountering several parser errors. I am trying to make a rectangular tube with the BOSL2 library.

It’s just a couple lines of code, I just don’t know what’s wrong, it worked in the video I was following. The code is:

Include <BOSL2/std.scad>

Rect_tube(isize=[65.5, 55.5], wall=1.5, h=1.65)

The parse and syntax error is the first “=“ on the second line.

***UPDATE - solved! **

I found the documentation here for anyone who is having issues with BOSL2 syntax (not sure how the video got it to work) :

GitHub.com/BelfrySCAD/BOSL2/wiki/shapes3d.scad

9 Comments
2024/11/25
13:12 UTC

7

How do I make a Tapered box in OpenSCAD?

Hey All,

Just looking for some help with OpenSCAD, specifically making a Tapered Box, which I will convert to a mesh for 3D printing a tray for an elderly person to use in their walker. Here is an image of the existing tray:

https://preview.redd.it/mcei15fv5y2e1.jpg?width=640&format=pjpg&auto=webp&s=9199741433cebed4556c07cd822b5fa1b43aae8b

Please note the taper of the cup and the box, I have already made the tapered cylinder for the cup in OpenSCAD, but I'm not sure how to make the tapered box. Just need a basic formula for the tapered box. Here is what I have put together in Prusa-Slicer to give an idea of where I'm going with this:

https://preview.redd.it/69sj4mfz9y2e1.png?width=964&format=png&auto=webp&s=70c91c8d4dd57d3c2ff4a867119e962d1858f018

As you can see, all I need is a basic shape formula for a tapered box in OpenSCAD, so I can render, and make an STL for it, and finally, plug in the measurements for the mesh in Prusa-Slicer and finish this project. Thanks in advance for help!

26 Comments
2024/11/25
01:33 UTC

2

Converting a cylinder cut-out to a flat cut out?

Hi all,

I am working on 3d printing a knitting machine and want to convert a scad model for a circular knitting machine into a flat one (basically if you were to just cut the cylinder vertically in one spot and lay it flat on a table is what I'm looking for). I have done some programming before but this is confusing me, so if anyone has tackled something similar in their projects I'd love to hear it.

For anyone interested, I found the file here: https://www.printables.com/model/355228-circular-sock-knitting-machine-for-my-mom-and-you

Here is the code with some of my comments:

$slots=64;

if($slots >= 128)
{
    assert(false, "Too many slots chosen, render time would be extensive");
}

difference(){
    //make the base cylinder..
  translate([0,0,-65]) 
  cylinder(d=120,h=80,$fn=200);
  $fn=60;
  //add needle slots
    translate([0,0,-66])
    for(i=[0:360/$slots:360]){  
      rotate([0,0,i])
      cube([62,2.2,83]); 
      } 
      //cut out the middle
  translate([0,0,-66]) 
    cylinder(d=105,h=82,$fn=200); 

    //cut out for holding the needles    
  rotate_extrude(){
    translate([60,0,0])
    resize([10,10])circle(d=4);  
    }   
 }
 //Points at top
 difference(){
  translate([0,0,15])
  for(i=[0:360/$slots:360]){ 
    rotate([0,0,i+((360/$slots)*0.54167)])
    cube([60,1.56,13]); 
   }  
   translate([0,0,9]) 
   cylinder(d=105,h=20,$fn=200);
}   
//cut out hole, final touches
 difference(){
   translate([0,0,-65]) 
   cylinder(d=105,h=80,$fn=200);
   translate([0,0,-66]) 
   cylinder(d=98,h=82,$fn=200);   
 } 

difference(){
   translate([0,0,15]) 
   cylinder(d=105,h=13,$fn=200);
   translate([0,0,14]) 
   cylinder(d1=98,d2=104,h=17,$fn=200);
} 
3 Comments
2024/11/24
04:09 UTC

6

Can OpenSCAD Generate Multiple STL Files from a List of Names?

Hello everyone!
I want to make 30 Christmas ornaments for my students, and I came across this design: https://www.thingiverse.com/thing:4681765.

I've downloaded OpenSCAD, and I was wondering: is it possible to input a list of their names and have it automatically generate all the STL files I need? If so, how can I do it?
thanks!

18 Comments
2024/11/23
17:33 UTC

3

BOSL2 preview OK but render sometimes fails

The following code previews fine, but renders fail with the error "No top level geometry to render".

include <BOSL2/std.scad>

full_path = [
    [0, 0, 0], [0.144619, 0, 2.57518], [0.576658, 0, 5.11798],
    [1.29068, 0, 7.59642], [2.27772, 0, 9.97933], [3.52534, 0, 12.2367],
    [5.01788, 0, 14.3403], [6.73654, 0, 16.2635], [16.0709, 0, 24.6682]
];
shape = ring(r1=20, r2=22, angle=[10, 30]);
yflip_copy() path_sweep(shape, full_path);

Oddly if I remove the "yflip_copy()" call then renders start working. Alternatively if I leave the "yflip_copy" call in, but change the X value in the final point in the path from "16.0709" to "15.0709" it also works. This happens with openscad version "2024.11.19.nightly" as well as "2021.01".

Can someone help me understand what's going on here? Why do renders fail in those specific circumstances only?

3 Comments
2024/11/22
00:40 UTC

14

General discussion: How do you make your code more readable?

Share your tricks!

Here are some of mine (I'll try to keep this OpenSCAD specific, if I dive into common good programming practices, it'll take a while...):

  • Make extensive use of modules inside modules. That allows you to break a problem apart, while still maintaining some order and separate namespaces.
  • Make things customizable from the start. That makes you think structured.
  • Don't overoptimize calculations. I frequently find myself stacking numbers, for example if I'm adding nuts, bolts and washers. Instead of just adding them up and translate the nut to 45, I type it out as 20+10+2+8+5 or whatever it happens to be, so that each part is still trackable. Preferably, of course, use variables such as "washerthickness" instead of 2.
  • I've made my one versions of translate, rotate and mirror, so I have xtran, xrot, xmirror and so on. As that keeps a "do one thing on each line", it makes it clearer just from the command, without looking at the parameters, what is being done. It also avoids "ascii art calculation lines" where you have a long line of calculations. Sure, more lines, but clearer lines.
  • Similar to above, I have my own version of cube, which can align on any edge, or center. Saves a shitload of work, and makes the code clearer.

box([100,100,100],xalign=0,yalign=-1,zalign=1); // centers on x, aligns wiyh negative edge on y, aligns with posiive edge on z

  • Make foldable sections with:

{ //Section name 

... 

}

  • Make your common parts in separate files. Screws, lumber, hinges and so on. Ideally, this makes the final code read almost an assembly instruction.
  • Use named arguments in non-trival calls. Longer lines, but readable, especially when you have calls with lots of parameters, or optional arguments. Example:

xmirror(copy=true)
xtran(100)
zrot(90)
hinge(angle=0,showscrews=true,color=cbrass);

  • Provide default values where it makes sense. Example:

module hinge(angle=0, showscrews=true, color=csilver)

  • If things get to complicated, consider starting over, and use what you've learned along the way. I made a couple of stairs for the porch, and and some point, the math for getting things in the right place just got too confusing. Simply by changing the origin to another point, and making the surface of the steps the reference, rather then the underlying framework, and saving some other reference points in variables, I went from about 600 lines non-working code to 100 lines clean, working code.
  • While you can make loops like:

for(angle=[0,120,240]){
    zrot(angle)
    square([1,100]);
}

it often ends up being awkward as things get complicated, so go for an index with calculations from the start:

for(index=[0:2]){
    zrot(index*120)
    square([1,100]);
}
  • Keep logic simple. Bad code:

if(!x){
    // Do if not x
}else{
    //do if x
}

Better code:

if(x){
    // Do if x
}else{
    //do if not x
}

Sure, both works, but it is a completely unnecessary risk of misunderstanding if you are just skimming the code. If you always follow this principle, it's one thing less to think about. I see even experience programmer do this, and it is bad.

  • Inline if. Instead of formatting it:

consider formatting it:

x=islong?
  1000:
  10;

Sure, in this example, it makes no difference, but stack some more inline ifs into it, and it gets messy.

x=islong?istest?999:1000:iscold?5:10;

This hurts to look at, but some clever formatting helps:

x=islong?
    istest?
        999:
        1000
    iscold?
        5:
        10;

These are some of my tricks to keep it clean. What are yours?

46 Comments
2024/11/21
08:41 UTC

1

Difference showing in preview but not in rendering

So, I am extremely new to openscad (which means that I just downloaded it today), and I'm trying to create a screw.

What I've come up with is the following: I create a cylinder (extruded circle) and difference a rotating triangle from it to create the thread. In the preview the design looks exactly like I want it to.

However, when I want to export the design as a stl, I have to render it first (F6) and when I do that I get the following warning: "WARNING: Scaling a 2D object with 0 - removing object"
My suspicion is that this is related to the triangles that are 2D objects, instead of 3D, however when I give them some thickness, the rendering just breaks down even more and disappears.

Preview

Rendering

With 3D cut-out, there is no rendered design

Code:

$fn = 1e2;

inner_radius = 2.5;
outer_radius = 3.5;
height = 30;
density = 1/2;
steps = 1e4-2;

translate([0,0,2*height]) {
    resize(newsize=[15,10,10])
    intersection() {
        cube(16,center=true);
        sphere(r=12);
    }
}

difference() {
    linear_extrude(height=2*height, center=false, convexity=10) {
        circle(r=outer_radius);
    }
    
    for (i = [0:steps]) {
        rotate([0,0,i]) {
            translate([0,0,i*height/steps]) {
                translate([-outer_radius,0,0]) {
                    rotate([90,0,0])
                    // linear_extrude(height=1,center=false,convexity=10) { 
                        polygon([[0,-1],[outer_radius-inner_radius,-1/2],[0,0]]);
                    //}
                }   
            }
        }
    }
}
5 Comments
2024/11/21
01:47 UTC

2

OpenSCAD fails to load ttf fonts in Debian 12

Hey All,

I'm having trouble with OpenSCAD, both with appimage and install from apt on Debian 12, It is failing to load library containing some ttf fonts I downloaded and have used with it in the past. I have the ttf files in the appropriate folders (both ~./local/share/fonts, and /usr/local/share/fonts) with the appropriate 644 permissions, yet both the appimage and the version installed from apt still fail to load the ttf's, with the following errors:

WARNING: Can't open library 'AllertaStencil-Regular.ttf'. ERROR: Can't read font with path 'AllertaStencil-Regular.ttf'  Execution aborted.

They worked (loaded) for me just fine before having to reinstall Debian a couple of months back. Also, it lists the fonts as available in the Edit>Preferences>Editor>fonts pull down menu. Any suggestions here? Thanks in advance.

9 Comments
2024/11/21
01:19 UTC

Back To Top