Saturday, November 17, 2018

Composing multimaterial 3D objects with OpenSCAD and the command line

I'm a fan of OpenSCAD. I'm also a gardener. My sharpie based plant labels were becoming difficult to read after a season on the ground. Worse though, some of the plants that I keep in my bog garden were becoming completely unreadable. Especially since they can sit outdoors in soggy or wet acidic peat moss for years before I decide that I need to remind myself exactly which cultivar I am looking at. So, having recently acquired the Prusa MMU upgrade for MK3, I naturally decide to 3d print the labels, so that they will never fade, and it will indeed be impossible for them to do so, since the lettering will be in a completely different color of plastic. This brings us to the first bit of code, the 3d printable multi-material plant label written in OpenSCAD:
 
letter="<Main Text>";
smallText="<Small Text>";
    
length = 90;    
textStart = 0; 
height = 2; 

module GetText(bigText, littleText) { 
    union() {         
        translate([textStart, 0, 1.7])
            color("black", 1)
                linear_extrude(height=.301, convexity=4)
                    text(bigText, 
                    font="Bitstream Vera Sans",
                    halign="center",
                    size=7
                    );
        
        translate([textStart, -8, 1.7])
            color("black", 1)
                linear_extrude(height=.301, convexity=4)
                    text(littleText, 
                    font="Bitstream Vera Sans",
                    halign="center",
                    size=5// 3 is too small, doesn't extrude
                          // 4 is readable, but leaves gaps
                    );
            }
}

// First extrusion 
/*marker_one*/difference () { 
    color("white", 1.0)
        union() { 
            //stem
            translate([-length/2 - 100, -2, 0])
                cube([100, 4, height]);
            
            // tapered neck 
            linear_extrude(height) { 
                polygon(points=[[-length/2 -30, 0], [-length / 2, +10], [-length /2, -10], [-length / 2 - 30, 0]]);
            } 
            
            //Broad flat area for text
            translate([-length/2, -10, 0])
                cube([length, 20, height], center=false) ;
        }
    
    // holes for text 
    color("black", 1) 
        union() { 
            GetText(letter, smallText); 

            rotate([180, 0, 0])
                translate([textStart, 1, -2])
                    GetText(letter, smallText); 
        }
}

// Second Extrusion
/*marker_two*/union() { 
    GetText(letter, smallText); 

    rotate([180, 0, 0])
        translate([textStart, 1, -2])
            GetText(letter, smallText); 
}
With this file in hand, I first replace <Main Text> and <Small Text> with something appropriate for the label. I then replace /*marker_two*/ with an asterisk and export the object to an *.amf file. I then repeat the process with /*marker_one*/, after deleting the first asterisk. So I have two *.amf files which I will then load into slic3r and convert into gcode. I print a lot of these labels, and I only print one or two of each of them. So this turns out to be more labor intensive than I'd like. After some research, I created the following powershell function to do the work for me. All from a single command.
function Get-3dMMPlantLabel { 
    Param($mainText, $smallText="")
    process {
        $template = [System.IO.File]::ReadAllText("PlantLabelTemplate.scad")
        $output = $template.Replace("<Main Text>", $mainText).Replace("<Small Text>", $smallText)
        $outputOne = $output.Replace("/*marker_two*/", "*")
        $outputTwo = $output.Replace("/*marker_one*/", "*")
        $outputOneFileName = "$($mainText)1.scad"
        $outputTwoFileName = "$($mainText)2.scad"
    
        $outputOne | Out-File $outputOneFileName -Encoding ascii
        $outputTwo | Out-File $outputTwoFileName -Encoding ascii
    
        & "C:\Program Files\OpenSCAD\openscad.exe" -o "$($mainText)1.amf" $outputOneFileName
        & "C:\Program Files\OpenSCAD\openscad.exe" -o "$($mainText)2.amf" $outputTwoFileName
    
    }
}

Get-3dMMPlantLabel "Swamp Milkweed" "Somewhere in OH"
It outputs two files: "Swamp Milkweed1.amf" and "Swamp Milkweed2.amf" in this example. These are exactly what I need to orchestrate the 3d print in Slic3r. Now if only I could automate the gcode creation in Slic3r. Maybe next Saturday.

Monday, January 1, 2018

3d printing

I've dabbled in 3d printing for a few years now. I just got an awesome new Prusa Mk3. Love it.

I also started updating my OpenSCAD skills.

My latest creation is a simple flower pot.
The whole file on GitHub is here.

Some notes:
1) module -- this seems to be what I, as a programmer, would normal call a function. In the context of OpenSCAD they are modules. Not sure why the change in syntax, it's just something to remember.
2) for loops -- another new thing thing that I have learned, and an immensely useful one in this context.
3) hull() -- this is what I used to achieve the rounded edges. I'm not sure I grok it completely, but it is certainly another useful tool. Here is the article that led me to it.
4) GitHub has a great STL Viewer. The rendered object is here.
5) Oooh, and one more. The MCAD libraries are full of good stuff.
use <MCAD/--filename-->
To include them in a normal scad file.

Wednesday, October 4, 2017

Powershell, Adding Autocomplete to a Function

tldr: Add the ValidateSet attribute to a parameter. Like this: [ValidateSet("value 1", "value 2", "value etc")]$parameter or more verbosely:
function Get-Something(
    ValidateSet("value 1", "value 2", "value etc")]
    $parameter) {
    Write-Host "hi"
}
Just figured that out. Much bigger post here.

Tuesday, September 5, 2017

A tale of Mardown and browser based code editors.

I like Markdown. I want to use Markdown as much as possible. I also enjoy embedding LaTeX and graphs. I like using a chromebook.
First, I used visual studio and Mads’ Markdown editor extension for it. I usually had a copy or three of visual studio open so that didn’t matter much. But I started extending Markdown with my own javascript functions. I also started trying to embedd LaTeX.
An example of LaTeX:
\Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.

Which renders as:

Then I found Markdown-plus which, is pretty awesome. I bought a copy. It works great in windows. I started writing LaTeX and graphs fluently. So I had to figure out a way to make it work on my chromebook. I started running Linux on it. Linux on a chromebook deserves a post or more in its own right. It is a very cool thing.
I got many things set up on the Linux-chromebook, and I learned many cool things. I pulled the markdown-plus code from GitHub and served it up to localhost. I learned how to start a web server from the command line. I learned how to use yarn, and npm. I started learning modern javascript functions and extensions, like ‘require’ and ‘let’, among others. I then forked the code and began dissecting it to see if I could add the “Save” functionality that would make it a viable tool on my chromebook. I ran into CodeMirror. I was awed by CodeMirror. I added “Save” capability, that seemed to be missing from markdown-plus. Perhaps the author didn’t want it competing with his 15$ windows app? Perhaps I was just missing something. Anyway, I created a pull request and called that piece done-ish. I now had a good flavor of Markdown running on my awesome chromebook. I was feeling pretty good about that.
However, then I ran into StackEdit. It is also open source on GitHub. It is also dissectable. From it I’ve been learning about even more javascript stacks. For its editor it uses prismjs. I also see Gulp, Bower, and some others that I’m not yet familiar with.
Anyway, so much awesome here, so little time. I’ll probably be abandoning markdown-plus in favor of StackEdit. Ahhh, there’s an easter egg here when you type ‘StackEdit’.
Written with StackEdit.

Sunday, September 3, 2017

Command Line web servers

The ability to serve up a random directory for http content is something that I learned quite recently. The first way that I did it:

Python3 -m http.server

From the angular cli, there is also:

ng-serve

And one more:

sudo npm install http-server -g
http-server


There are others as well, some of them are listed here:
Link to stackexchange

no need to run some heavyweight tool like IIS, Visual Studio, Apache, etc.

Monday, August 21, 2017

Fun with Angular and an HTML 5 canvas.

I updated my canvas-drawing post from 2014. Now it uses AngularJS instead of JQuery. Angular rocks.

Fiddle, GitHub

It doesn't embed well in blogger though.
The HTML: The javascript:

Wednesday, March 23, 2016

Don't use the 64 bit task manager to create a dump of a 32 bit process.

Yeah, that didn't work. The problem and the solution are both in the post title.

VS 2015 wouldn't let me look at the heap and didn't give me enough information about why, so I downloaded WinDbg and tried to get more information. At least with WinDbg I could make some progress.

New command: .cordll -ve -u -l

That gave me some good information, but it didn't work. What I did get from it was a path forward.

I ended up copying mscordacwks from the problem computer to the WinDbg directory on the computer that I was using for debugging. I also had to rename it to mscordacwks_AMD64_x86_4.0.3.319.18034.dll. Then, I did the same thing with SOS.dll.

Then when this didn't work, I found a stack overflow post suggesting the 32 bit Task Manager solution.