Code Red > .NET

How do you get a pickpoint on a line as a 3d point ?

(1/3) > >>

Peter Laker:
Just started getting into C# & can't work out how to get a 3d point from a pick point on a 3d line.
I have worked out this so far which works ok in 2d but not 3d, in 3d it looks like it returns a point in the DCS (it's got no Z)

Point3d pickPt = entitySelectionResult.PickedPoint;

I have seen some reference made to PointOnLine but can't work out how to use it.

Any help would be appreciated, Thanks...

Kerry:

--- Code - C#: --- Hello Peter ;

Have a play with something like  this
..

--- Code - C#: ---//// CodeHimBelongaKwb ©  Feb 2008          [AcRx.CommandMethod("ListIt")]        static public void ListIt()        {            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;            AcEd.PromptEntityResult resEnt = ed.GetEntity("\nSelect an Object: ");            if (resEnt.Status != AcEd.PromptStatus.OK)            {                ed.WriteMessage("\nNothing selected");                return;            }            // Something selected, so ..            // do come stuff ..            ed.WriteMessage("\n PromptEntityResult : " + resEnt.ToString());            ed.WriteMessage("\n PickedPoint : " + resEnt.PickedPoint.ToString());             // Get the ObjectId            AcDb.ObjectId ObjId = (resEnt.ObjectId);            ed.WriteMessage("\n Object Id : " + resEnt.ObjectId.ToString());             // Add Breakpoint here            Point3d pickPt = resEnt.PickedPoint;             // Get the Entity            AcDb.Entity ent = kdubGetEntity(resEnt.ObjectId);            ed.WriteMessage("\n Entity : " + ent.ToString() + "\n");            ent.List();             Extents3d extents3D = ent.GeometricExtents;            ed.WriteMessage("\n extents3D : " + extents3D.ToString());            ed.WriteMessage("\n extents3D.LLPoint : " + extents3D.MinPoint.ToString());            ed.WriteMessage("\n extents3D.URPoint : " + extents3D.MaxPoint.ToString());             // PROBLEM : how to locate the Entity WCS location ??             Matrix3d ecsPossy = ent.Ecs;            ed.WriteMessage("\n ECS : " + ecsPossy.ToString());         }         public static Entity kdubGetEntity(AcDb.ObjectId id)        {            AcDb.Entity ent;            AcDb.TransactionManager tm =                 AcadApp.DocumentManager.MdiActiveDocument.TransactionManager;                using (AcDb.Transaction tr = tm.StartTransaction())            {                ent = (AcDb.Entity)tm.GetObject(id, AcDb.OpenMode.ForRead, true);            }            return ent;        }

Kerry:

--- Quote ---Command: list
Select objects: 1 found

Select objects:

                  LINE      Layer: "0"
                            Space: Model space
                   Handle = 7f
              from point, X= 223.7900  Y=   0.0000  Z= 222.1967
                to point, X=1538.5387  Y= 867.8355  Z=-368.4467
Extrusion direction relative to UCS:
                   X=   0.0000  Y=   1.0000  Z=   0.0000
          In Current UCS, Length =1575.3420,  Angle in XY Plane =     33
                  3D Length  =1682.4275,  Angle from XY Plane =    339
                  Delta X =1314.7487, Delta Y =  867.8355, Delta Z =-590.6434

Command: listit

Select an Object: _MID of
 PromptEntityResult :
((OK,),(2130436152),(881.164319159307,433.917747505122,-73.125000386091))
 PickedPoint : (881.164319159307,433.917747505122,-73.125000386091)
 Object Id : (2130436152)
 Entity : Autodesk.AutoCAD.DatabaseServices.Line
                  LINE      Layer: "0"
                            Space: Model space
                   Handle = 7f
              from point, X= 223.7900  Y=   0.0000  Z= 222.1967
                to point, X=1538.5387  Y= 867.8355  Z=-368.4467
Extrusion direction relative to UCS:
                   X=   0.0000  Y=   1.0000  Z=   0.0000
          In Current UCS, Length =1575.3420,  Angle in XY Plane =     33
                  3D Length  =1682.4275,  Angle from XY Plane =    339
                  Delta X =1314.7487, Delta Y =  867.8355, Delta Z =-590.6434

 extents3D :
((223.789977837311,-222.196720773481,0),(1538.5386604813,368.446721545663,867.835495010243))
 extents3D.LLPoint : (223.789977837311,-222.196720773481,0)
 extents3D.URPoint : (1538.5386604813,368.446721545663,867.835495010243)
 ECS : ((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1))
--- End quote ---

and the piccy

Kerry:
This may help too ..

Add these helpers  to a static class

--- Code - C#: --- // CodeHimBelongaKwb ©  Feb 2008 public static partial class dbExtensions    {        public static AcGe.Point3d UcsToWcs(this AcGe.Point3d pt, AcDb.Database db)        {            return pt.TransformBy(db.GetUcsMatrix());        }        public static bool IsPaperSpaceActive(this AcDb.Database db)        {            if (db.TileMode)            { return false; }            return (db.PaperSpaceVportId == AcadApp.DocumentManager.MdiActiveDocument.                Editor.CurrentViewportObjectId);        }        public static AcGe.Matrix3d GetUcsMatrix(this AcDb.Database db)        {            AcGe.Point3d ucsorgin = db.Ucsorg;             AcGe.Vector3d ucsxdir = db.Ucsxdir;            AcGe.Vector3d ucsydir = db.Ucsydir;            // re-assign variables if PaperSpace is Active                        if (db.IsPaperSpaceActive())            {                ucsorgin = db.Pucsorg;                ucsxdir = db.Pucsxdir;                 ucsydir = db.Pucsydir;            }            // Vector3d newZAxis = ucsxdir.CrossProduct(ucsydir);                       return Matrix3d.AlignCoordinateSystem(                AcGe.Point3d.Origin,                AcGe.Vector3d.XAxis,                 AcGe.Vector3d.YAxis,                 AcGe.Vector3d.ZAxis,                ucsorgin,                 ucsxdir,                 ucsydir,                 ucsxdir.CrossProduct(ucsydir)             );        }    }
Amend the code to return the point in WCS as well as UCS

--- Code - C#: ---         // // CodeHimBelongaKwb ©  Feb 2008               [AcRx.CommandMethod("ListIt")]        static public void ListIt()        {            AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;            AcEd.Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;            AcEd.PromptEntityResult resEnt = ed.GetEntity("\nSelect an Object: ");            if (resEnt.Status != AcEd.PromptStatus.OK)            {                ed.WriteMessage("\nNothing selected");                return;            }            // Something selected, so ..            // do come stuff ..            ed.WriteMessage("\n PromptEntityResult : " + resEnt.ToString());                        AcGe.Point3d pickPt = resEnt.PickedPoint;            ed.WriteMessage("\n PickedPoint in UCS : " + pickPt.ToString());             AcGe.Point3d WCSPoint = pickPt.UcsToWcs(db);            ed.WriteMessage("\n Picked Point in WCS : " + WCSPoint.ToString());             // Get the ObjectId            AcDb.ObjectId ObjId = (resEnt.ObjectId);                        ed.WriteMessage("\n Object Id : " + resEnt.ObjectId.ToString());             // Get the Entity            AcDb.Entity ent = kdubGetEntity(resEnt.ObjectId);            ed.WriteMessage("\n Entity : " + ent.ToString() + "\n");            ent.List();             AcGe.Extents3d extents3D = ent.GeometricExtents;            ed.WriteMessage("\n extents3D : " + extents3D.ToString());            ed.WriteMessage("\n extents3D.LLPoint : " + extents3D.MinPoint.ToString());            ed.WriteMessage("\n extents3D.URPoint : " + extents3D.MaxPoint.ToString());                         AcGe.Matrix3d ecsPossy = ent.Ecs;            ed.WriteMessage("\n ECS : " + ecsPossy.ToString());        }
and the result :-
Command: ListIt

Select an Object: _MID of
 PromptEntityResult :
((OK,),(2130436152),(106.529729152112,1.92859694466527E-14,-86.8562848134239))
PickedPoint in UCS : (106.529729152112,1.92859694466527E-14,-86.8562848134239)
Picked Point in WCS : (106.529729152112,86.8562848134239,0)
 Object Id : (2130436152)
..>>>>>>>>>>

Kerry:
And this is the typical header I use ...
may clarify some of the class addresses listed :-)

--- Code - C#: ---// CodeHimBelongaKwb ©  Feb 2008  using System;using System.Collections;using System.Collections.Generic;using System.Text;using System.Data;using System.Linq;using System.Windows.Forms; // using System.Runtime.InteropServices; using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.Geometry;using Autodesk.AutoCAD.Runtime;//using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;using AcAp = Autodesk.AutoCAD.ApplicationServices;using AcDb = Autodesk.AutoCAD.DatabaseServices;using AcEd = Autodesk.AutoCAD.EditorInput;using AcGe = Autodesk.AutoCAD.Geometry;using AcRx = Autodesk.AutoCAD.Runtime;//using AcCm = Autodesk.AutoCAD.Colors;using AcGi = Autodesk.AutoCAD.GraphicsInterface;using AcLy = Autodesk.AutoCAD.LayerManager;using AcPl = Autodesk.AutoCAD.PlottingServices;using AcUi = Autodesk.AutoCAD.Windows; using WinForms = System.Windows.Forms;

Navigation

[0] Message Index

[#] Next page

Go to full version