MatterHackers FREE, FAST Shipping in the U.S. Customer Service +1 (949) 613-5838
Cart ...
Script In c#

MatterCAD - Design Your 3D Parts In C#

MatterCAD is a simple parametric CAD tool that allows you to create 3D parts using C#. There is a fully functional command line tool that you can use today. We are also starting work on a graphical version similar to OpenSCAD.
Script In c#

Any programmer wanting or needing to model custom 3D parts has probably stumbled across OpenSCAD, a program that allows you to generate parts using basic scripting.  OpenSCAD is a wonderful tool and for a while we used it exclusively in designing parts, but we missed the ability to use a fully featured programming language.  Eventually we started creating a homegrown utility that would allow us to write a part using C# - we call the utility MatterCAD.  For our design workflow we use MatterCAD to output a .scad file which we can then open in OpenSCAD.  OpenSCAD is used to do the visualization and to do the final outputting of a printable .stl file.

Over time MatterCAD has continued to advance to the point were it is probably useful and stable enough to be made generaly available.

MatterCAD Code


Features:

MatterCAD is designed to make the creation of parts easier with two main features.  The first feature is the ability to treat each part, or collection of parts, as distinct objects in the code.  Treating each part as an object allows information to be stored about the part - attributes like its bounding box, position and transforms.  The other main feature is a rich set of positioning tools, such as 'align' and 'set center'.  With these positioning tools you can define parts relative to each other or to a specific coordinate position in the world.  For example you can align the bottom of a part with the Z 0 plane of the world, that way you can be sure that part will always stay at Z 0 regardless of the changes you make to its design.  With 'align' you can also layout parts relative to other parts.  Again, when you change the size or position of one part, all the parts that are aligned to it simply move to their correct positions relative to the one that has changed.

  • Primitives
    • Box
    • Cylinder
    • NGonExtrusion
    • RotateEtrude
    • Round
    • Solid
    • Sphere
  • Operations
    • Union
    • Difference
    • Intersection
  • Transforms
    • Align
    • Rotate
    • Scale
    • SetCenter
    • Translate

 

Resources You Will Need:

You can download the source code from our bitbucket account.  The file you will want is MatterCadCommandLine.zip

You will also need an IDE (integrated development environment) to edit the code.  We recomend you use MonoDevelop.  MonoDevelop is available for Windows, Mac and Linux.  You can also use VisualStudio if you have it but it is not as easy for everyone to get.

You will want to look at the API for MatterCAD.

And finally you will need a copy of OpenSCAD to view your part and to output it to an .stl for printing.

Work Flow:

Here is a quick breakdown of the procedure we take when creating a new part.  This is assuming you have already downloaded MatterCAD and an IDE (integrated development environment) like MonoDevelop.

  1. Go into the 'MatterCadCommandLine' directory
  2. Copy one of the folders, we'll use the TrackConnector folder (select it and hit copy paste)
  3. Rename the folder to the new part you want to create.  We'll use '10mmBox' for now
  4. Go into the new folder (double click '10mmBox')
  5. Open 'MatterCad.sln' (this should launch MonoDevelop)
  6. Open 'MatterCad.cs' from within the MonoDevelop
  7. Select the contents of the TrackConnector() function and replace them with 'return new Box(10, 10, 10);'
  8. Build and Run the program (we will assume you are making a debug build for now)
  9. Back in your file manager, go into the '10mmBox\bin\Debug' folder and open the file 'TrackConnector.scad' with OpenSCAD (you can give a differnt output file name in Main() later).
  10. Now to improve the design you would edit the code, run the program again and then just reload (F4) the part in OpenSCAD.  Wash, Rinse and Repeat (do that over and over :).

Sample Script:

Here is a very simple script.  It is a track connector for a wooden train set.

using System; // we need some usings to get started
using System.Diagnostics;
using MatterHackers.Csg; // our constructive solid geometry base classes
using MatterHackers.VectorMath; // helper math functions
namespace SimplePartScripting
{
    static class SimplePartTester
    {
        static CsgObject TrackConnecor()
        {
            // CsgObject is our base class for all constructive solid geometry primitives
            CsgObject total; 
            // we create a box object and name it bar, the "link" is the name it will
// have in OpenSCAD
            CsgObject bar = new Box(20, 5.8, 12, "link");  
            // we set it's center to the center of the coordinate system
            bar = new SetCenter(bar, Vector3.Zero);

            // and we make the total = bar, the only object we have at this time
            total = bar;

            // now we make a cyliner for one side
            CsgObject leftHold = new Cylinder(11.7 / 2, 12, Alignment.z); 
            // position it where we want it
            leftHold = new SetCenter(leftHold, bar.GetCenter() + new Vector3(12, 0, 0)); 
            // and make another one on the other side by mirroring
            CsgObject rightHold = leftHold.NewMirrorAccrossX(); 
            // this is the way we actually decide what boolean operation to use to put them together
            total += leftHold; 
            total += rightHold; 
            return total;  // and pass it back to the caller
        } 
        static void Main()
        {
            // an internal function to save as an .scad file
            OpenSCadOutput.Save(TrackConnecor(), "TrackConnector.scad");
        }
    }
}