eInvalidInput when change SectionSettings' values

eInvalidInput when change SectionSettings' values

Anonymous
Not applicable
949 Views
5 Replies
Message 1 of 6

eInvalidInput when change SectionSettings' values

Anonymous
Not applicable

I use the GetClosestPointTo method to make sure the point on the curve

then I get a eInvalidInput error when using GetParameterAtPoint for a LWPolyline。

 

here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
[assembly: CommandClass(typeof(test.Class1))]
namespace test
{
    public class Class1
    {
        [CommandMethod("eg1")]
        public static void eg1()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityOptions peop = new PromptEntityOptions("\nchoose a polylne:");
            peop.SetRejectMessage("\n polyline only!");
            peop.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult res = ed.GetEntity(peop);
            if (res.Status != PromptStatus.OK)
                return;
            PromptPointOptions ppt = new PromptPointOptions("\nchoose a point:");
            PromptPointResult pptRes = ed.GetPoint(ppt);
            if (pptRes.Status != PromptStatus.OK)
                return;
            Polyline txt0 = null;
            using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
            {
                txt0 = trans.GetObject(res.ObjectId, OpenMode.ForWrite) as Polyline;
                trans.Commit();
            }
            Point3d pt = txt0.GetClosestPointTo(pptRes.Value, true);
            double param1 = txt0.GetParameterAtPoint(pt);
            ed.WriteMessage("\nparam1=" + param1.ToString());
            double dis = txt0.GetDistAtPoint(pt);
            ed.WriteMessage("\ndis=" + dis.ToString());

        }
    }
}

and here is my drawing

 

0 Likes
Accepted solutions (1)
950 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

You should call GetClosestPointTo(), GetParameterAtPoint() and GetDistAtPoint() within the transaction scope because when the transaction is disposed (at the end of the using code block), the polyline (which was opened by the transaction) will also be disposed.

 

To insure the point is on the curve, you should not call GetClosestPointTo() with the extend param set to true.

 

 

using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
{
    Polyline txt0 = (Polyline)trans.GetObject(res.ObjectId, OpenMode.ForWrite);
    Point3d pt = txt0.GetClosestPointTo(pptRes.Value, false);
    double param1 = txt0.GetParameterAtPoint(pt);
    double dis = txt0.GetDistAtPoint(pt);
    ed.WriteMessage("\ndis=" + dis.ToString());
    trans.Commit();
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

Anonymous
Not applicable

thank u so much.

but came with the same mistake after a few ‎Amendments

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
[assembly: CommandClass(typeof(test.Class1))]
namespace test
{
    public class Class1
    {
        [CommandMethod("eg1")]
        public static void eg1()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityOptions peop = new PromptEntityOptions("\nchoose a polylne:");
            peop.SetRejectMessage("\n polyline only!");
            peop.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult res = ed.GetEntity(peop);
            if (res.Status != PromptStatus.OK)
                return;
            PromptPointOptions ppt = new PromptPointOptions("\nchoose a point:");
            PromptPointResult pptRes = ed.GetPoint(ppt);
            if (pptRes.Status != PromptStatus.OK)
                return;             
            using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
            {
                Polyline txt0 = trans.GetObject(res.ObjectId, OpenMode.ForWrite) as Polyline;
                Point3d pt = txt0.GetClosestPointTo(pptRes.Value, false);
                double param1 = txt0.GetParameterAtPoint(pt);
                ed.WriteMessage("\nparam1=" + param1.ToString());
                double dis = txt0.GetDistAtPoint(pt);
                ed.WriteMessage("\ndis=" + dis.ToString());
                trans.Commit();
            }
        }
    }
}

 

0 Likes
Message 4 of 6

_gile
Consultant
Consultant

I think this is due to this drawing.

AutoCAD looses its accuracy when entities are drawn far from the origin.

The coordinates of the block reference are around (35639000000, 2897900000).

If there's no imperative reason to draw so far from the origin, just move all entities nearer to (0,0).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 6

_gile
Consultant
Consultant
Accepted solution

If there any reason you can't move the entities closer to the origin, you can just move the polyline, do the computation and move it back when done. You can use the picked point as displacement vector.

 

        [CommandMethod("eg1")]
        public static void eg1()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityOptions peop = new PromptEntityOptions("\nchoose a polylne:");
            peop.SetRejectMessage("\n polyline only!");
            peop.AddAllowedClass(typeof(Polyline), true);
            PromptEntityResult res = ed.GetEntity(peop);
            if (res.Status != PromptStatus.OK)
                return;
            PromptPointOptions ppt = new PromptPointOptions("\nchoose a point:");
            PromptPointResult pptRes = ed.GetPoint(ppt);
            if (pptRes.Status != PromptStatus.OK)
                return;
            using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
            {
                Polyline txt0 = trans.GetObject(res.ObjectId, OpenMode.ForWrite) as Polyline;
                Vector3d displacement = pptRes.Value.GetAsVector();
                txt0.TransformBy(Matrix3d.Displacement(displacement.Negate()));
                Point3d pt = txt0.GetClosestPointTo(Point3d.Origin, false);
                double param1 = txt0.GetParameterAtPoint(pt);
                ed.WriteMessage("\nparam1=" + param1.ToString());
                double dis = txt0.GetDistAtPoint(pt);
                ed.WriteMessage("\ndis=" + dis.ToString());
                txt0.TransformBy(Matrix3d.Displacement(displacement));
                trans.Commit();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

Anonymous
Not applicable
Thank u so much.
0 Likes