.NET

Reply
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 1 of 9 (213 Views)

polyline in 3d space

213 Views, 8 Replies
08-26-2013 01:03 AM

Hi everyone, I have the following points that when connected create a polyline (Px = X,Y,Z)

 

P1 = 149.941738,99.586521,6999.370079
P2 = 146.5869,91.262907,6999.250781
P3 = 143.883334,82.716622,6999.701863
P4 = 142.39435,83.120478,6998.429138
P5 = 141.276439,98.451501,6993.00069
P6 = 130.73509,105.487377,6982.747181
P7 = 131.486628,107.340559,6982.777312

 

The points are not lined up with any standard axes, but do have a common plane. I would like to create a polyline using the points above. Any advice would be very appreciated.

 

Thank you,

 

P.S. I have tryed playing with 'polyline3d' and 'transformBy' but could not get it to work.

 

-J

*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 2 of 9 (203 Views)

Re : polyline in 3d space

08-26-2013 02:08 AM in reply to: junoj

Hi,

 

Polyline vertices are 2d points defined in the polyline OCS.

To learn more about OCS, have a look at the Developer Documentation in AutoCAD Help > DXF References > Advanced DXF Issues > Object Coordinates System (OCS).

 

So, you have to define the polyline OCS from the points collection, mainly calculate its Normal. This can be done with the cross product of 2 non-colinear vectors. Then, you'll be able to calculate the polyline elevation.

 

Here's a little sample:

 

        void MakePolyline(Point3dCollection pts, bool closed)
        {
            if (pts.Count < 3)
                throw new ArgumentException("Needs at least 3 points");
            Vector3d normal = new Vector3d();
            Vector3d v1 = pts[0].GetVectorTo(pts[1]);
            for (int i = 1; i < pts.Count - 1; i++)
            {
                normal = v1.CrossProduct(pts[i].GetVectorTo(pts[i + 1]));
                if (normal != new Vector3d())
                    break;
            }
            if (normal == new Vector3d())
                throw new ArgumentException("All points are aligned");
            Matrix3d OCS = Matrix3d.WorldToPlane(new Plane(Point3d.Origin, normal));
            double elevation = pts[0].TransformBy(OCS).Z;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
                using (Polyline pline = new Polyline(pts.Count))
            {
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                for (int i = 0; i < pts.Count; i++)
                {
                    Point3d pt = pts[i].TransformBy(OCS);
                    pline.AddVertexAt(i, new Point2d(pt.X, pt.Y), 0.0, 0.0, 0.0);
                }
                pline.Normal = normal;
                pline.Elevation = elevation;
                pline.Closed = closed;
                btr.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }
        }

 

 

Gilles Chanteau
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 3 of 9 (178 Views)

Re: polyline in 3d space

08-26-2013 01:03 PM in reply to: junoj

Thank you Gilles, the code runs without any errors but nothing is created in the model space. I have tryed debuging but can't find any problems with the code.

Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 4 of 9 (155 Views)

Re: polyline in 3d space

08-27-2013 01:12 AM in reply to: junoj

I have been doing a lot of reading about Vector3d trying to understand how the Vector system works.

 

If I have a polyline and I change the Vector3d of the polyline like so   myPoly.Normal = New Vector(0,1,2)  this will change the polyline Z vector. It appears that in the above code I have no control over the X and Y vector.

 

Is it possible to make changes to two vectors, like say the Z and the X?

 

Thank you,

 

-J

*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 5 of 9 (148 Views)

Re: polyline in 3d space

08-27-2013 02:14 AM in reply to: junoj

I tried the above code with the points you provided in the first post. it worked fine for me.

Maybe you have to run a Zoom Extents to see the newly created polyline.

 

In this code the Normal of the polyline is calculated using the cross product of the first segment direction vector (vector from the first vertex to the second vertex) and the the following segment direction vector which is not aligned with the first one.

The cross product of two vectors returns a vector which is perpendicular to these vectors, in other words, the vector perpendicular to the plane defined by two non-colinear vectors. This is the definition of the Normal to a plane (the construcion plane of the polyline).

 

This vector is used to define the polyline OCS and to set the Normal property of the polyline. The resulting OCS is used to transform the 3d points defined in WCS to 2d points defined in OCS which are the polyline vertices.

 

Y cannot understand your last question.

Gilles Chanteau
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 6 of 9 (136 Views)

Re: polyline in 3d space

08-27-2013 08:25 AM in reply to: junoj

Thank you Gilles, perhaps something went wrong when I was translating your code to VB, I will have to investigate into that more . . .

 

Sorry about the last question, I am very rusty on vectors. What I am trying to determine is how I can change an existing polyline vector to: 

 

   Z ->   Vector (0,1,2)

   X ->   Vector (1,0,0)

 

If I use the code  myPoly.Normal = New Vector(0,1,2) that changes the Z vector, but I also would like to change the X vector of myPoly.

 

Let me know if I make any sense.

 

Thank you again,

 

-J

*Expert Elite*
_gile
Posts: 2,104
Registered: ‎04-29-2006
Message 7 of 9 (129 Views)

Re: polyline in 3d space

08-27-2013 10:26 AM in reply to: junoj

To my mind, what you're asking don't make sense.

A polyline (as many 2d entities) has a Normal property but none vector X, Y or Z.

If saying "change the X vector" of a polyline, you mean rotate the polyline about its Normal, you can use:

myPoly.TransformBy(Matrix3d.Rotation(angle, myPoly.Normal, center))

Gilles Chanteau
Distinguished Contributor
junoj
Posts: 116
Registered: ‎02-26-2009
Message 8 of 9 (121 Views)

Re: polyline in 3d space

08-27-2013 12:21 PM in reply to: _gile

Sorry Gilles, perhaps I am making it more complicating then it needs to be. Here is an example of what I am trying to understand.

 

I have a Polyline myPoly that has 3 points wcs: (1,2,0) (1,1,0)(3,1,0)

 

So if my understanding is correct the  OCS above is equal to WCS.

 

Now I would like to align/move-rotate the above polyline so that the old points of the myPoly lineup with the new ponts (All points are in WCS):

 

   (1,2,0) = (-4.5,-0.5,0.7071)

   (1,1,0) = (-4,0,0)

   (3,1,0) = (-3,1,1.4142)

 

What I was thinking about is to fist create the polyline in the WCS then aligning its OCS with where it needs to go in space.

 

Sorry about the frustration with the communication, I hope the above is a better explanation. Thank you for all your help.

 

-J

ADN Support Specialist
Balaji_Ram
Posts: 715
Registered: ‎03-21-2011
Message 9 of 9 (80 Views)

Re: polyline in 3d space

08-28-2013 10:50 PM in reply to: junoj

Hi,

 

Sorry, your question is not clear enough. 

 

Will creating a 3d polyline using those points be of any help to you ?

http://adndevblog.typepad.com/autocad/2012/06/creating-a-polyline3d-without-using-transactions.html

 

 

 

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.