Close



Page 5 of 8 FirstFirst ... 34567 ... LastLast
Results 41 to 50 of 72

Hybrid View

  1. #1
    Super Moderator Roxy's Avatar
    Join Date
    Apr 2014
    Location
    Lone Star State
    Posts
    2,182
    Quote Originally Posted by old man emu View Post
    I was reading this, http://blog.cubehero.com/2013/12/18/...d-code-part-i/ and came across this example of a module,

    module ring(height, radius, radial_width) {
    difference() {
    cylinder(h = height, r = radius + radial_width / 2);
    translate([0, 0, -1])
    cylinder(h = height + 2, r = radius - radial_width / 2);
    }}

    After studying it for a while, it dawned on me that the module was like an icon on the object creation toolbar of a GUI based progam like Rhino.
    Here is a piece of OpenScad code I wrote to make ridged beams. I was making a Pump Jack for my brother that just graduated from University of Texas as a Petroleum Engineer. I wanted things as big as they could be because this was going to sit on his desk. But I ran into problems with the beams warping when they were long.

    This is a good example of how to use modules to insulate yourself from a problem. I already had a module that made a ridged_beam() and let me set numbers to build it. But then I defined a module to make a stress_relieved_ridged_beam() that took some input numbers and generated an 'appropriate' beam from them.

    And at a higher level, it only takes a few lines of code to make the base() with all these fancy holes. If I need to do further work to reduce stress on the model, I just have to do it in the one routine stress_relieved_ridged_beam() and it will automatically get replicated everywhere that function is used. If the topic is of interest to you, you can zoom in and see the miniature cuts between each big hole to eliminate stress build up on the early layers.

    PumpJack_Base.jpg

    Proper and judicious use of 'module' can help make your code more maintainable and easier to reuse. (Not to mention... Easier for other people to understand!!!!)
    Attached Files Attached Files

  2. #2
    Not to hijack, but I just want to put in a quick plug for openjscad instead of openscad. It performs way better (render time), doesn't have the wonky syntax and variable scoping oddities. I have switched over completely.






  3. #3
    Super Moderator curious aardvark's Avatar
    Join Date
    Jul 2014
    Posts
    8,818
    download link ?

    I'm all for anything that's quicker. I have no problem with the syntax. Like I said after a fairly short while you just type the brackets automatically.

    Let's each download the OpenScad Cheat Sheet and, starting with Syntax , go through it explaining what each of the underlined words means, and the correct way to write it into some code.
    That could get complicated really quickly. A lot of the commands on there are stupidly complicated and rely on others to do anything at all - half of them. (or more) I haven't managed to get my head around at all.

    Take for example the Minkowski hull.
    On the surface it blends two shapes so you can get round cornered boxes, oval trays etc. I've used it for some oval screw trays.
    I looked at how it works the other day and it's all about how a robot drives around a space - wtf ??????
    It's also really tricky to work out how big the final shape is going to be.

    Likewise the rotate extrude command. I've used it to make a chapati press using donut shapes. Looks pretty cool. But I have no clue how the numbers you put into the equation relate to the size of the final donut. In the end I made a flat cube of known size and just kept changing numbers till the donut was the same size as the cube.

    I definitely think it's easier to start with an actual task you're likely to want to do and then explain how it's done.

    But we can try both methods :-)

    ps. Ah right openjscad is the online webpage version.
    Nah I like programs that run on my actual computer. Plus some of the designs I want to keep to myself. :-)
    Last edited by curious aardvark; 12-03-2014 at 05:44 AM.

  4. #4
    Staff Engineer old man emu's Avatar
    Join Date
    Oct 2013
    Location
    Narellan, New South Wales, Australia
    Posts
    912
    Here's the link: http://www.openscad.org/cheatsheet/index.html

    Yes! Yes! Hasten slowly.

    I did a bit of that tonight, and here's the little bit of code I ended up with. The object is a toddler's Duplo building block. It took about half an hour to do, and that involved going back and forth to the cheat sheet.

    Here's my notes for the object, except that I was going to make a four peg block when I pinched the Duplo from my grandson. The first design decision I made was to make a block with a single peg, then multiply and join the single one.

    Duplo Sketch.jpg

    After some reading; some cut and paste, and some delete and replace work, I came up with this:

    Single Duplo Block.scad

    Critique appreciated.

    How do you get a grid displayed for measurement purposes? Working out where the centres of the spheres and cylinder was what took most of the time with this exercise.

    Old Man Emu
    Last edited by old man emu; 12-05-2014 at 02:15 AM.

  5. #5
    Super Moderator Roxy's Avatar
    Join Date
    Apr 2014
    Location
    Lone Star State
    Posts
    2,182
    Quote Originally Posted by old man emu View Post
    How do you get a grid displayed for measurement purposes? Working out where the centres of the spheres and cylinder was what took most of the time with this exercise.
    A sphere and cylinder are always generated at (0,0). So when you translate them... what ever amount you translated them... Is where the center's are. I warmed over your code so you can see how to make your code easier to understand:
    Code:
    singleduploblock();
    
    module singleduploblock () {
    $fn=100;
    cube (32,32,20); color("gray", 0.5) translate([16,16,20]) cylinder(36, d=30); difference() {
    color("red" ) translate([16,16,55]) sphere(d=30); color ("lime") translate ([ 8,8,68 ]) cube([16,16,8]);
    }
    }
    First, in OpenScad you should have a 'Top Level' geometry. That is what that first singleduploblock(); is. It references other stuff, but it is at the top level.

    singleduploblock() does not have any paremeters, and certainly does not need its name passed in as a parameter.

    It is typical, and usually better to put modifiers like color() in the front of the line. And it is usually best to combine things that go together like color(), translate() and cube() so it is easy to pick out what everything is doing.

    And of course, indenting really helps too. For example, having things indented makes it easier to see what the difference() is operating on. The code directive isn't working perfectly on this post... But you can see what I mean even though I'm restricted on how to indent 'properly'. You only have one module in this file so this won't be as clear. But a lot of people indent everything within a module so it is clear where a module starts and ends. The module declarations are part of the 'top level' so they are pushed as far to the left as possible. As you get nested deeper and deeper, you indent more and more to show that.

    Even though what I posted is almost exactly what you had... most people would prefer to work with my version.
    Last edited by Roxy; 12-03-2014 at 09:47 AM.

  6. #6
    Staff Engineer printbus's Avatar
    Join Date
    May 2014
    Location
    Highlands Ranch, Colorado USA
    Posts
    1,437
    Add printbus on Thingiverse
    Quote Originally Posted by Roxy View Post
    ...And of course, indenting really helps too...
    Another good habit for anywhere brackets are used is to comment the closing bracket to explain what it closes, exactly like CA did in his fully annotated script.

  7. #7
    Super Moderator curious aardvark's Avatar
    Join Date
    Jul 2014
    Posts
    8,818
    I like the indenting idea.
    cylinder(36, d=30)
    What's the '36' refer to ?
    I know there are a lot of compact ways to write the parameters for a command - but can we stick to longhand for the time being ?

  8. #8
    Super Moderator Roxy's Avatar
    Join Date
    Apr 2014
    Location
    Lone Star State
    Posts
    2,182
    Quote Originally Posted by curious aardvark View Post
    I like the indenting idea.

    What's the '36' refer to ?
    I know there are a lot of compact ways to write the parameters for a command - but can we stick to longhand for the time being ?
    cylinder(h=36, d=30);

    The way OpenScad works is default numbers can be specified for a module. And if the caller over rides that number, then something else can be used. H (Height) is the first parameter to cylinder, so you can be lazy and just give it a number and it will use that for height. But that is frowned up greatly.

  9. #9
    Super Moderator curious aardvark's Avatar
    Join Date
    Jul 2014
    Posts
    8,818
    Well I frowned at it :-)

    We're pretty much beginners here - so be gentle with us :-)

    Okay i copied and pasted the script - weird how the formatting just doesn't survive a copy and paste.

    Hate the colours - why ?????
    And are the plugs in duplo blocks really as long as that ?

    Looks really weird.

    Now that's better :-)
    duploblock-mono.JPG
    But the really weird thing is that you used a cube command with no square brackets. And it worked.

    cube (32,32,20);

    That should save some time.

    Also curious about this odd habit of putting modules before the module script.

    I tend to put modules at the start of the script to get the bulk out of the way and then use them in the script as a normal command.

    I've noticed a couple of times this habit of putting them at the end of the script. Just seems back to front to me.

  10. #10
    Super Moderator Roxy's Avatar
    Join Date
    Apr 2014
    Location
    Lone Star State
    Posts
    2,182
    Quote Originally Posted by curious aardvark View Post
    But the really weird thing is that you used a cube command with no square brackets. And it worked.

    cube (32,32,20);
    I suggest you put the [32,32,20] inside of brackets. If for no other reason than everybody else does and expects to see that. But now you are changing your tune. You said you wanted the h=36 instead of a simple 36 for the cylinder(). So... ????

    Quote Originally Posted by curious aardvark View Post
    Also curious about this odd habit of putting modules before the module script.

    I tend to put modules at the start of the script to get the bulk out of the way and then use them in the script as a normal command.

    I've noticed a couple of times this habit of putting them at the end of the script. Just seems back to front to me.
    OpenScad is very loosely typed and doesn't care about forward references. Mostly, you want the code to be manageable and easy for other people to understand. So, my preference is to put equates up at the top, and invoke the modules I want very early. Then, I tend to use the bottom part of the file as a library to define the stuff that I used up at the top of the file.

    But that is me.... As long as it is well organized, clean and easy to understand, nobody is going to complain.

Page 5 of 8 FirstFirst ... 34567 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •