using System;
using System.Collections.Generic;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;

namespace GMLC01HerramientasCad.Acad
{
    class PuntosEtiquetas
    {

        private PuntosEtiquetas()
        {
        }

        public static Point3d GetInsPntPUNTO(ObjectId oId)
        {
            Point3d pto = new Point3d(0,0,0);
            Transaction tr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
            try
            {
                using (tr)
                {
                    Entity entidad = (Entity)tr.GetObject(oId, OpenMode.ForRead);
                    if (entidad != null)
                    {
                        //pto = new Point3d(entidad.Position.x, entidad.Position.y, entidad.Position.z);
                        if (entidad.ToString() == "Autodesk.AutoCAD.DatabaseServices.DBPoint")
                        {
                            // La entidad es un punto.
                            DBPoint pun = (DBPoint)entidad;
                            pto = new Point3d(pun.Position.X, pun.Position.Y, pun.Position.Z);
                        }
                        else if (entidad.ToString() == "Autodesk.AutoCAD.DatabaseServices.BlockReference")
                        {
                            // La entidad es un bloque.
                            BlockReference blo = (BlockReference)entidad;
                            pto = new Point3d(blo.Position.X, blo.Position.Y, blo.Position.Z);
                        }
                    }

                }
            }
            finally
            {
                tr.Dispose();
            }

            return pto;
        }

        public static Point3d GetInsPntLINEA(ObjectId oId)
        {
            Point3d pto = new Point3d(0, 0, 0);
            Transaction tr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
            try
            {
                using (tr)
                {
                    Entity entidad = (Entity)tr.GetObject(oId, OpenMode.ForRead);
                    if (entidad != null)
                    {
                        //pto = new Point3d(entidad.Position.x, entidad.Position.y, entidad.Position.z);
                        if (entidad.ToString() == "Autodesk.AutoCAD.DatabaseServices.Polyline3d")
                        {
                            // La entidad es un punto.
                            Polyline3d lin = (Polyline3d)entidad;
                            pto = lin.GetPointAtDist(lin.GetDistAtPoint(lin.EndPoint) / 2);
                            //pto = lin.GetPointAtDist(0.5);
                        }
                    }

                }
            }
            finally
            {
                tr.Dispose();
            }


            return pto;
        }

        public static Point3d GetInsPntAREA(ObjectId oId)
        {
            Point3d pto = new Point3d(0, 0, 0);
            Transaction tr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
            try
            {
                using (tr)
                {
                    Entity entidad = (Entity)tr.GetObject(oId, OpenMode.ForRead);
                    if (entidad != null)
                    {
                        Hatch htch = (Hatch)entidad;

                        double x = 0;
                        double y = 0;
                        double z = htch.Elevation;

                        for (int i = 0; i < htch.GetLoopAt(0).Curves.Count; i++)
                        {
                            x = x + htch.GetLoopAt(0).Curves[i].StartPoint.X;
                            y = y + htch.GetLoopAt(0).Curves[i].StartPoint.Y;
                        }
                        x = x / htch.GetLoopAt(0).Curves.Count;
                        y = y / htch.GetLoopAt(0).Curves.Count;

                        pto = new Point3d(x, y, z);

                    }
                }
            }
            finally
            {
                tr.Dispose();
            }


            return pto;
        }

    }
}
