Exception: eInvalidInput Surface.CreateFrom(pl3d)

Exception: eInvalidInput Surface.CreateFrom(pl3d)

wokeyiyognshenme
Advocate Advocate
363 Views
2 Replies
Message 1 of 3

Exception: eInvalidInput Surface.CreateFrom(pl3d)

wokeyiyognshenme
Advocate
Advocate
            Point3d pt_left_down = new Point3d(432536.587782616, 2687388.25631493, 878.298840506535);
            Point3d pt_right_down = new Point3d(432550.741108191, 2687388.25631493, 870.545233657537);

            Point3d pt_left_up = new Point3d(432537.786447037, 2687391.85152945, 880.486865788067);

            Vector3d vec = pt_right_down - pt_left_down;

            Point3d pt_right_up = pt_left_up + vec;

            Point3dCollection pt3ds = new Point3dCollection();

            pt3ds.Add(pt_left_down);
            pt3ds.Add(pt_left_up);
            pt3ds.Add(pt_right_up);
            pt3ds.Add(pt_right_down);

            Polyline3d pl3d = new Polyline3d(Poly3dType.SimplePoly, pt3ds, true);

            //Error
            //Autodesk.AutoCAD.Runtime.Exception: eInvalidInput
            //Autodesk.AutoCAD.DatabaseServices.Surface.CreateFrom(Entity fromEntity)

            Autodesk.AutoCAD.DatabaseServices.Surface surface = Autodesk.AutoCAD.DatabaseServices.Surface.CreateFrom(pl3d);

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            doc.AddToModelSpace(surface);
0 Likes
364 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

It seems there's an accuracy issue with the input points and the computed one (pt_right_up) which generate a Polyline 3d which is not exactly planar (e.g. it cannot be converted into a Region).

A workaround should be using a Polyline instead.

Point3d pt_left_down = new Point3d(432536.587782616, 2687388.25631493, 878.298840506535);
Point3d pt_right_down = new Point3d(432550.741108191, 2687388.25631493, 870.545233657537);
Point3d pt_left_up = new Point3d(432537.786447037, 2687391.85152945, 880.486865788067);

Vector3d normal = (pt_right_down - pt_left_down).CrossProduct(pt_left_up - pt_left_down).GetNormal();
Plane plane = new Plane(Point3d.Origin, normal);
double elevation = pt_left_down.TransformBy(Matrix3d.WorldToPlane(plane)).Z;
Point2d pt1 = pt_left_down.Convert2d(plane);
Point2d pt2 = pt_right_down.Convert2d(plane);
Point2d pt4 = pt_left_up.Convert2d(plane);
Point2d pt3 = pt2 + (pt4 - pt1);

using (var tr = db.TransactionManager.StartTransaction())
{
    var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

    using (var pline = new Polyline(4))
    {
        pline.AddVertexAt(0, pt1, 0.0, 0.0, 0.0);
        pline.AddVertexAt(1, pt2, 0.0, 0.0, 0.0);
        pline.AddVertexAt(2, pt3, 0.0, 0.0, 0.0);
        pline.AddVertexAt(3, pt4, 0.0, 0.0, 0.0);
        pline.Closed = true;
        pline.Normal = normal;
        pline.Elevation = elevation;

        var surface = Autodesk.AutoCAD.DatabaseServices.Surface.CreateFrom(pline);
        curSpace.AppendEntity(surface);
        tr.AddNewlyCreatedDBObject(surface, true);
    }
    tr.Commit();
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

_gile
Consultant
Consultant

Or, simpler, use Surface.CreateExtrudedSurface.

 

Point3d pt_left_down = new Point3d(432536.587782616, 2687388.25631493, 878.298840506535);
Point3d pt_right_down = new Point3d(432550.741108191, 2687388.25631493, 870.545233657537);
Point3d pt_left_up = new Point3d(432537.786447037, 2687391.85152945, 880.486865788067);

using (var tr = db.TransactionManager.StartTransaction())
{
    var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
    using (var line = new Line(pt_left_down, pt_right_down))
    {
        var surface = Autodesk.AutoCAD.DatabaseServices.Surface.CreateExtrudedSurface(
            new Profile3d(line),
            pt_left_up - pt_left_down,
            new SweepOptions());
        curSpace.AppendEntity(surface);
        tr.AddNewlyCreatedDBObject(surface, true);
    }
        tr.Commit();
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes