.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

6 REPLIES 6
Reply
Message 1 of 7
waterharbin
487 Views, 6 Replies

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

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?

6 REPLIES 6
Message 2 of 7

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();
            }
        }

 

Message 3 of 7

Thank you for your post.But I don't know how to identify a polyliney entity via its ObjectId?
Message 4 of 7

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)
{ ...

 

 

 

 

Message 5 of 7

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.Smiley Happy

Message 6 of 7
matus.brlit
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#. Smiley Wink

Message 7 of 7
waterharbin
in reply to: matus.brlit

Thank you very much.Forget the certain programming language,I can speak both VB and C# now.Haha.Smiley Very Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost