AutoCAD내에 프로그램을 개발하다보면, 선과 선의 연결과 연결 된 선들을 찾을 때가 있습니다.
선택한 Line(선)을 기준으로 앞뒤로 연결된 선을 찾는 기능입니다.
AutoCAD에서 이 코드를 사용하려면 다음 단계로 실행해 보세요.
1. Visual Studio에서 새 C# 프로젝트를 만듭니다.
참조에 accoremgd.dll, acdbmgd.dll, AcDbMgd.dll, AcMgd.dll을 추가합니다.
2. 아래의 코드를 복사하여 프로젝트에 붙여넣습니다.
빌드하여 .dll 파일을 생성합니다.
3. AutoCAD에서 NETLOAD 명령을 실행하여 .dll 파일을 로드합니다.
4. CAL 명령을 실행합니다.
5. AutoCAD에서 원하는 선을 선택하면 연결된 선을 노란색으로 변경해 줍니다.
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Test
{
public class Util
{
[CommandMethod("CAL")]
public void CheckAdjacentLines()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 선 선택
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a line: ");
peo.SetRejectMessage("\nPlease select a line.");
peo.AddAllowedClass(typeof(Line), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// 선택한 선 가져오기
Line selectedLine = trans.GetObject(per.ObjectId, OpenMode.ForRead) as Line;
if (selectedLine == null)
{
ed.WriteMessage("\nFailed to get the selected line.");
return;
}
// 선택한 선의 근처 선 찾기
List<Line> adjacentLines = FindAdjacentLines(selectedLine, trans);
if (adjacentLines.Count > 0)
{
ed.WriteMessage("\nAdjacent lines found:");
foreach (Line line in adjacentLines)
{
ed.WriteMessage("\nLine ID: " + line.ObjectId.ToString()); //연결된 Line ID 출력
ANetCore.acEnt.SetColor(line.ObjectId, 2); //연결된 선 노란색으로 변경
}
}
else
{
ed.WriteMessage("\nNo adjacent lines found.");
}
trans.Commit();
}
}
private List<Line> FindAdjacentLines(Line selectedLine, Transaction trans)
{
List<Line> adjacentLines = new List<Line>();
BlockTable bt = trans.GetObject(trans.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId objId in btr)
{
Entity ent = trans.GetObject(objId, OpenMode.ForRead) as Entity;
if (ent is Line && ent.ObjectId != selectedLine.ObjectId)
{
Line line = (Line)ent;
if (selectedLine.StartPoint.IsEqualTo(line.StartPoint) || selectedLine.StartPoint.IsEqualTo(line.EndPoint) ||
selectedLine.EndPoint.IsEqualTo(line.StartPoint) || selectedLine.EndPoint.IsEqualTo(line.EndPoint))
{
adjacentLines.Add(line);
}
}
}
return adjacentLines;
}
}
}
결과:
'AutoCAD C#' 카테고리의 다른 글
[AutoCAD C#]오토캐드에서 선택한 객체 정보를 보여준다. (0) | 2024.02.28 |
---|---|
[AutoCAD C#]오토캐드에서 블럭(Block)들을 수량과 레이어(Layer) 구분하여 출력하기 (1) | 2024.02.27 |
[AutoCAD C#]AutoCAD에서 두 선이 연결되어 있는지 확인하는 C# 코드 (0) | 2024.02.26 |
[AutoCAD C#]오토캐드에서 블럭(Block)을 선택, 레이어 변경하기 (0) | 2024.02.25 |
[AutoCAD C#]오토캐드에서 객체를 선택하여 Color, Layer 변경하기 (0) | 2024.02.24 |