Extruding DWG/DXF profiles 💪

Here's a code snippet to extrude a profile generated in AutoCAD. 

// Reads the Autodesk file
var ra = new ReadAutodesk("myfile.dxf");

// Extracts a collection of curve entities
List<ICurve> curves = new List<ICurve>();
foreach (Entity ent in ra.Entities)
    var curve = ent as ICurve;
    if (curve != null)

// Groups the curves that are connected into CompositeCurves. 
ICurve[] newCurves = UtilityEx.GetConnectedCurves(curves, 1);

// Checks all the inners curves to get closed contourns.
double gapTolerance = 3;
foreach (var myCurve in newCurves)
    CompositeCurve myCc = myCurve as CompositeCurve;
    if (myCc != null)
        // Checks only the first curve with the last one, but in some cases it could be necessary also to check curve-by-curve.
        double gap = Point3D.Distance(myCc.CurveList[0].StartPoint, myCc.CurveList[myCc.CurveList.Count - 1].EndPoint);

        if (!myCc.IsClosed && gap <= gapTolerance)
            myCc.CurveList.Add(new Line(myCc.CurveList[myCc.CurveList.Count - 1].EndPoint, myCc.CurveList[0].StartPoint));

// the chordalError is the regeneration tolerance and it's needed to tessellate and subdivide the curves
double chordalError = 0.01;

// Creates a Region with outer and inners profiles
var rr = UtilityEx.DetectRegionsFromContours(newCurves, chordalError);

// Extrudes the Region
Solid extruded = rr[0].ExtrudeAsSolid(0, 0, 30, chordalError);

// Adds the extruded profile to the Entities collection viewportLayout1.Entities.Add(extruded, 0, Color.Beige);


Original profile

Extruded profile

Was this article helpful?
3 out of 3 found this helpful
Have more questions? Submit a request


  • Hi, I'm new to Eyeshot and dwf/dfx and I'm trying to test this extrude feature but AutodeskReader Entities are always null. What am I doing wrong?

    Comment actions Permalink
  • Hi Marcello 

    Does this work only in XY Plane ?



    Comment actions Permalink
  • Very nice! I did not know about the 'UtilityEx.GetConnectedCurves()' until now. I've already developed my own 2d chain routine.

    Thanks for the code snippet. Very useful.

    Comment actions Permalink

Please sign in to leave a comment.