Extruding DWG/DXF profiles

Here's a code snippet to extrude AutoCAD® profile. 

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

// 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)                                    
        curves.Add(curve);                
}

// 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));                    
    }
}

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

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

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

 

Original profile

Extruded profile

   
Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    James Trounson

    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.

Please sign in to leave a comment.