Search code examples
c#3dcad

Programmatically Creating CAD Files


I need to programmatically create a CAD model. Meaning: The user sets parameters for the model and the application outputs a (insert extension here) file to use in SolidWorks / 3DStudio / Sketchup.

The application generates shells (a tube, for example) with changing radiuses and I would like visualize the shells generated externally, usually in SolidWorks. I suppose it's something like the output of this robot.

I'm not completely sure about the output I need, I need to test a few options. So I'm looking for the technical solution. A good output file to start with will be a tube with a constant external radius, but the internal radius changes with Z (or the other way around).

In SolidWorks, I'd create a spline and use 'Revolved Boss' to extrude it to a shape. Then create another spline and use 'Revolved Cut' to remove the center, like in this picture (Red - the outline of the pipe - the outer radius. Green - The outline of the inner radius): Pipe

(This example is on a tube but the shapes (the intersections) are not really limited, they always consist of geometrical shapes though)

So, my questions are:

  1. Is there an (free if possible) infrastructure to do exactly that? Revloved boss and Revolved Cut?
  2. Does this infrastructure has an option to export to CAD files?
  3. I think I'm using solid modelling - am I?
  4. Should I just create a points cloud (model many intersections and join them together)? Which file format should I use then?

The main use will be for working with solidworks. I'm using C#, but anything goes.

I'm sorry for the vague question - I'm pretty new in CADing from code.


Solution

  • I ended up using Eyeshot SDK. The alternatives I tried:

    • OpenSCAD - Good, but I was looking for something that integrates with the code, and not as an external application
    • OpenCASCADE - It seems it can do anything, but it took me too much time to install and configure. Too messy in my opinion

    Eyeshot has a pretty simple SDK that is usable straight from C#. The documentation is awful, and I spent way too much time trying to figure out the exception it threw - But it has good code samples. And it's okay once you get to know it. A tad expensive though.

    I'm still working on the SolidWorks export. Eyeshot supports STL, IGEN, OBJ, and STEP - Solidworks handles them all okay, but the surfaces are not smooth (circles are not circles just many polygons). As I said - still at work.

    Anyway, for future references - Here is some code samples that create something similar (outer radius is constant, inner radius changing) to what I described in the question (Check out one of the samples, like Lego, for how to use WorkUnit):

    public class CBuildOutput : WorkUnit
    {
        EntityList entities = new EntityList();
    
        private void CreatePipe()
        {
            double outerRadius = 60;
    
            // First decide on accuracy
            double chordalError = 0.05;
            int slices = Utility.NumberOfSegments(outerRadius, chordalError);
    
            // Make a cylinder, the cut a hole from it
    
            // 1. Cylinder
            Solid cyl = Solid.CreateCylinder(outerRadius, 50, slices);
    
            // 2. Define the hole curve
            Curve innerCurve = new Curve(2, new List<Point3D>() {
                new Point3D(outerRadius - 20, 0, 0),
                new Point3D(outerRadius - 25, 0, 10),
                new Point3D(outerRadius - 15, 0, 20),
                new Point3D(outerRadius - 25, 0, 30),
                new Point3D(outerRadius - 15, 0, 40),
                new Point3D(outerRadius - 20, 0, 50)});
    
            // 3. Create an extrude-able sketch
            CompositeCurve holeSketch = new CompositeCurve(
                new Line(Point3D.Origin, new Point3D(40, 0, 0)),
                innerCurve,
                new Line(40, 0, 50, 0, 0, 50));
    
            // 4. Create a hole solid
            Solid hole = Solid.Revolve(holeSketch, chordalError, 0, 2 * Math.PI, Vector3D.AxisZ, Point3D.Origin, slices, true);
    
            // 5. Cut the hole from the cylinder    
            Solid[] final = Solid.Difference<Solid>(cyl, hole);
    
    //          entities.Add(cyl, 0, Color.Red);
    //          entities.Add(hole, 0, Color.Red);
            entities.Add(final[0], 0, Color.Red);
        }
    
        protected override void DoWork(System.ComponentModel.BackgroundWorker worker, System.ComponentModel.DoWorkEventArgs doWorkEventArgs)
        {
            CreatePipe();
        }
    
        protected override void WorkCompleted(ViewportLayout viewportLayout)
        {
            viewportLayout.Entities = entities;
            viewportLayout.ZoomFit();
    //          viewportLayout.WriteIGES("model.iges", false);
        }
    }