.NET

Reply
Contributor
waterharbin
Posts: 14
Registered: ‎07-17-2011
Message 1 of 7 (246 Views)

How to obtain a polyline's certain vertexes via this polyline's objectId?

246 Views, 6 Replies
07-31-2011 04:38 AM

Hello,I create a polyline ,and then add it's objectId to a ObjectIdCollection defined by me.After the polyline is created,I would like to obtain it's second and sixth vertexes via this polyline's objectId.How to do this?

Active Member
rodrigo_gcmsoft
Posts: 8
Registered: ‎06-02-2010
Message 2 of 7 (243 Views)

Re: How to obtain a polyline's certain vertexes via this polyline's objectId?

07-31-2011 06:24 AM in reply to: waterharbin

Here is an excerpt of my code, hope it helps. The loop goes through each vertex of the polyline.

 

public static void GeraMemorial(string handle, string _inscricao_cadastral)
        {
            // Get the current document and database
            Autodesk.AutoCAD.ApplicationServices.Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            double Xinit = 0;
            double Yinit = 0;
            double Perimetro = 0;
            decimal perimetro_dec = 0;
            decimal area_dec = 0;
            long ln = Convert.ToInt64(handle, 16);
            Handle hn = new Handle(ln);
            // Start a transaction
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                ObjectId obj = acCurDb.GetObjectId(false, hn, 0);
                DBObject objeto = acTrans.GetObject(obj, OpenMode.ForRead);
                Polyline acPoly = objeto as Polyline;
                StringBuilder sb = new StringBuilder();
                Point2d pt0 = acPoly.GetPoint2dAt(0);


                for (int i = 0; i < acPoly.NumberOfVertices; i++)                 {
                    if (i == 0)
                    {
                        Memorial = "Partindo do ";
                    }
                    else if (i != 0 && i < acPoly.NumberOfVertices - 1)
                    {
                        Memorial = Memorial + ", chega-se ao ";
                    }

                    // Could also get the 3D point here
                    if (acPoly.GetSegmentType(i) == SegmentType.Line)
                    {
                        LineSegment2d test = acPoly.GetLineSegment2dAt(i);
                        double D = test.Length;
                        Perimetro = Perimetro + D;
                        Point2d pt = test.EndPoint;
                        Vector2d vec = test.Direction;
                        double ang = vec.Angle; // angle in radians
                        double azimuth = 0;
                        double west = test.StartPoint.X;
                        double north = test.StartPoint.Y;
                        if (i == 0)
                        {
                            Xinit = west;
                            Yinit = north;
                        }

                        if (ang == 0) { azimuth = 90.0; }
                        if (ang > 0 && ang < Math.PI / 2) { azimuth = 90 - rtd(ang); }
                        if (ang == Math.PI / 2) { azimuth = 0.0; }
                        if (ang > Math.PI / 2 && ang < Math.PI) { azimuth = rtd(ang) + 180; }
                        if (ang == Math.PI) { azimuth = 270.0; }
                        if (ang > Math.PI && ang < Math.PI * 1.5) { azimuth = rtd(ang); }
                        if (ang == Math.PI * 1.5) { azimuth = 180.0; }
                        if (ang > Math.PI * 1.5 && ang < Math.PI * 2) { azimuth = rtd(ang) - 180.0; }
                        if (ang == Math.PI * 2) { azimuth = 90; }
                        Memorial = Memorial + "vértice V-";
                        Memorial = Memorial + (i + 1).ToString();
                        Memorial = Memorial + " de coordenadas N=";
                        Memorial = Memorial + String.Format("{0:0.0000}", north);
                        Memorial = Memorial + " e E=";
                        Memorial = Memorial + String.Format("{0:0.0000}", west);
                        Memorial = Memorial + " seguindo com distância ";
                        Memorial = Memorial + String.Format("{0:0.00}", D);
                        Memorial = Memorial + "m e azimute de ";
                        Memorial = Memorial + ConvertToDMSfromDecDeg(azimuth);
                    }
                    else if (acPoly.GetSegmentType(i) == SegmentType.Arc)
                    {
                        CircularArc2d curva = acPoly.GetArcSegment2dAt(i);
                        double raio = curva.Radius;
                        Point2d ptInicio = curva.StartPoint;
                        Point2d ptFim = curva.EndPoint;
                        double angulo = curva.EndAngle;
                        double comprimento = ptInicio.GetDistanceTo(ptFim);
                        double graus = 360 * angulo / (2 * Math.PI);
                        double desenvolvimento = 2 * Math.PI * raio * (graus / 360);
                        Perimetro = Perimetro + desenvolvimento;
                        double west = curva.StartPoint.X;
                        double north = curva.StartPoint.Y;
                        if (i == 0)
                        {
                            Xinit = west;
                            Yinit = north;
                        }
                        Memorial = Memorial + "vértice V-";
                        Memorial = Memorial + (i + 1).ToString();
                        Memorial = Memorial + " de coordenadas N=";
                        Memorial = Memorial + String.Format("{0:0.0000}", north);
                        Memorial = Memorial + " e E=";
                        Memorial = Memorial + String.Format("{0:0.0000}", west);
                        Memorial = Memorial + ", seguindo com raio de ";
                        Memorial = Memorial + String.Format("{0:0.00}", raio);
                        Memorial = Memorial + "m e desenvolvimento de ";
                        Memorial = Memorial + String.Format("{0:0.0000}", desenvolvimento);
                        Memorial = Memorial + "m";
                    }
                }
                Memorial = Memorial + " e chega-se ao vértice V-1 de coordenadas N=";
                Memorial = Memorial + String.Format("{0:0.0000}", Yinit);
                Memorial = Memorial + " e E=";
                Memorial = Memorial + String.Format("{0:0.0000}", Xinit);
                Memorial = Memorial + ", ponto inicial da descrição deste perímetro.";

                Area = Convert.ToDouble(String.Format("{0:0.00}", acPoly.Area));
                area_dec = Convert.ToDecimal(Area);
                Perimetro = Convert.ToDouble(String.Format("{0:0.00}", Perimetro));
                perimetro_dec = Convert.ToDecimal(Perimetro);

                acTrans.Commit();
                MySqlConnection ConexaoRemoto = new MySqlConnection(funcoes.BancoRemoto);
                ConexaoRemoto.Open();
                MySqlCommand CmdUpdate = new MySqlCommand("UPDATE LOT1100 SET area_total = '" + area_dec.ToString().Replace(',','.') + "', perimetro = '" + perimetro_dec.ToString().Replace(',','.') + "', descricao_perimetro = '" + Memorial + "' WHERE inscricao_cadastral = '" + _inscricao_cadastral + "'", ConexaoRemoto);
                CmdUpdate.ExecuteNonQuery();
                MySqlCommand CmdUpdate1 = new MySqlCommand("UPDATE LOT1000 SET memorial_exportado = 'S' WHERE inscricao_cadastral = '" + _inscricao_cadastral + "'", ConexaoRemoto);
                CmdUpdate1.ExecuteNonQuery();
                ConexaoRemoto.Close();
                ConexaoRemoto.Dispose();
            }
        }

 

Contributor
waterharbin
Posts: 14
Registered: ‎07-17-2011
Message 3 of 7 (240 Views)

Re: How to obtain a polyline's certain vertexes via this polyline's objectId?

07-31-2011 06:43 AM in reply to: rodrigo_gcmsoft
Thank you for your post.But I don't know how to identify a polyliney entity via its ObjectId?
Active Member
rodrigo_gcmsoft
Posts: 8
Registered: ‎06-02-2010
Message 4 of 7 (237 Views)

Re: How to obtain a polyline's certain vertexes via this polyline's objectId?

07-31-2011 08:38 AM in reply to: waterharbin

As you will select the entity in the drawing, is that the user will select? Has a little to your routine will do.
You can be sure that the objectid of a polyline is trying to cast the object to the type polyline.
Forgive my English, I am Brazilian.


 

 

DBObject obj = tr.GetObject(per.ObjectId, OpenMode.ForRead);
Polyline pl = obj as Polyline;
if (pl != null)
{ ...

 

 

 

 

Contributor
waterharbin
Posts: 14
Registered: ‎07-17-2011
Message 5 of 7 (231 Views)

Re: How to obtain a polyline's certain vertexes via this polyline's objectId?

07-31-2011 09:41 AM in reply to: rodrigo_gcmsoft

Thank you very much.My situation is that,I use a ObjectIdCollection to contain a lot of Polyline's objectId.The ObjectIds are add to the ObjectIdCollection after the polylines are created by codes like:

ObjectIdCollection.Add(Polyline's Objectid)

For a short,I don't need user to select at all,and all the elements of the ObjectIdCollection are polyline's ObjectId.The problem is that a polyline could be erased by users.In this case,I can use the IsErased property of the objectId to tell which polyline has been erased.And I would not want the vertexes of the erased polyline.Just like this:

foreach (ObjectId id in ObjectIdCollection)

{

 if (!id.IsErased)

{

     //Get the second and the sixth vertexex of this polylne

}

}

 

By the way,forgive my poor English too,I don't speak English either,haha.:smileyhappy:

Mentor
matus.brlit
Posts: 246
Registered: ‎03-11-2008
Message 6 of 7 (197 Views)

Re: How to obtain a polyline's certain vertexes via this polyline's objectId?

08-05-2011 01:25 AM in reply to: waterharbin

//Get the second and the sixth vertexex of this polylne

 

Dim v2,v6 as Point3d

Using tr as Transaction = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction

Dim poly as Polyline = tr.GetObject(id,OpenMode.ForRead)
v2 = poly.GetPoint3dAt(1)
v6 = poly.GetPoint3dAt(5)

tr.Commit()

End Using

 

By the way,forgive my VB, I don't speak C#. :smileywink:

Contributor
waterharbin
Posts: 14
Registered: ‎07-17-2011
Message 7 of 7 (168 Views)

Re: How to obtain a polyline's certain vertexes via this polyline's objectId?

08-06-2011 10:17 PM in reply to: matus.brlit

Thank you very much.Forget the certain programming language,I can speak both VB and C# now.Haha.:smileyvery-happy:

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.