AutoCAD C#

[AutoCAD C#]오토캐드에서 선택한 객체 정보를 보여준다.

데브프로그라 2024. 2. 28. 19:48
반응형

AutoCAD에서 객체에 따라 다양하게 유틸리티를 개발할 수 있습니다.
객체를 선택하여 객체를 판단하고, 그에 따른 기능을 부여하는 것이죠.
아래의 코드는 실 사용할 수는 없지만, 어떤 식으로 개발이 되는 지 확인이 가능합니다.
물론, 개발자마다 사용하는 방식이 다르기 때문에 다양하게 구현할 수 있습니다.

[CommandMethod(" ENTINFO ", CommandFlags.Modal | CommandFlags.Session)]
        public void ENTINFO()
        {
            var ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
            var doc = AcadApp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;

            ed.WriteMessage("\n************************************************************");
            ed.WriteMessage("\n**객체 정보 (Layer, Color, 기타 정보)**");
            ed.WriteMessage("\n************************************************************");

            ObjectId objID = acSelect.GetSelectObjectID("객체 정보 - 객체선택:");
            ed.WriteMessage("\nDXF Name:{0}", objID.ObjectClass.DxfName);
            ed.WriteMessage("\nLayer:{0}", acEnt.GetLayer(objID));
            ed.WriteMessage("\nColor:{0}", acEnt.GetColor(objID));

            if(objID.ObjectClass.DxfName.Equals("LINE"))
            {
                ed.WriteMessage("\nLength:{0}mm, ({1})m", acLine.GetLength(objID), acLine.GetLength(objID)*0.01);
                ed.WriteMessage("\nAngle:{0} ({1})", acLine.GetAngle(objID), acMath.RtD(acLine.GetAngle(objID)));
            }

            if (objID.ObjectClass.DxfName.Equals("LWPOLYLINE"))
            {
                ed.WriteMessage("\nLength:{0}mm, ({1})m", acPolyline.GetLength(db, objID), acPolyline.GetLength(db, objID) * 0.01);
                Point3dCollection ptAr3d = acPolyline.GetPointList3D(objID);
                ed.WriteMessage("\n정점:{0}", ptAr3d.Count);
                ed.WriteMessage("\n면적:{0}", acUtil.GetArea(objID));
            }

            if (objID.ObjectClass.DxfName.Equals("INSERT"))
            {
                Point3d ptInsert = acBlock.GetPosition(objID);
                ed.WriteMessage("\n삽입점:{0}", ptInsert.ToString());
                ed.WriteMessage("\nAngle:{0} ({1})", acBlock.GetRotate(objID), acMath.RtD(acBlock.GetRotate(objID)));
            }

            if (objID.ObjectClass.DxfName.Equals("CIRCLE"))
            {
                Point3d ptCen = new Point3d();
                double dRadius = 0;
                acCircle.GetCenter(objID, out ptCen, out dRadius);
                ed.WriteMessage("\n중심점:{0}", ptCen.ToString());
                ed.WriteMessage("\n반지름:{0}", dRadius);
                ed.WriteMessage("\n원주:{0}", acUtil.GetLength(objID));
            }

            if (objID.ObjectClass.DxfName.Equals("ARC"))
            {
                Point3d ptCen = new Point3d();
                double dRadius = 0;
                double dSartAngle = 0;
                double dEndAngle = 0;
                double dLength = 0;
                acArc.GetCenter(objID, out ptCen, out dRadius, out dSartAngle, out dEndAngle, out dLength);
                
                ed.WriteMessage("\n중심점:{0}", ptCen.ToString());
                ed.WriteMessage("\n반지름:{0}", dRadius);
                ed.WriteMessage("\n호길이:{0}mm, ({1})m", dLength, dLength*0.01);
                ed.WriteMessage("\n호 시작각도:{0} ({1})", dSartAngle, acMath.RtD(dEndAngle));
                ed.WriteMessage("\n호 끝 각도:{0} ({1})", dEndAngle, acMath.RtD(dEndAngle));
            }

            if (objID.ObjectClass.DxfName.Equals("TEXT") || objID.ObjectClass.DxfName.Equals("MTEXT"))
            {
                acText.GetHeight(objID);

                Point3d ptInsert = new Point3d();
                acText.GetPosition(objID, ref ptInsert);

                double dAngle = 0;
                acText.GetRotation(objID, ref dAngle);

                string strText = acText.GetText(objID);
                ObjectId objTxtStyleID = acText.GetTextStyle(objID);

                ed.WriteMessage("\n문자:{0}", strText);
                ed.WriteMessage("\n삽입점:{0}", ptInsert.ToString());
                ed.WriteMessage("\nAngle:{0} ({1})", dAngle, acMath.RtD(dAngle));
                ed.WriteMessage("\n문자스타일:{0}", acTextStyle.GetTextStyleName(objTxtStyleID));
            }
        }

반응형