1 、界址点起点位置
C# CAD2016 多边形顶点按方向重新排序
2、 界址点顺时针逆时针走向
C# CAD2016 判断多边形的方向正时针或逆时针旋转
3、块文件插入
//已知块文件名称 GXGLQTC
//块文件需要插入的坐标点 scaledPoint
// 插入块到当前图纸中的指定位置
ObjectId newBlockId;
BlockTable currentBlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
newBlockId = currentBlockTable["GXGLQTC"];
using (Transaction transaction = db.TransactionManager.StartTransaction())
{
BlockReference blockRef = new BlockReference(scaledPoint, newBlockId);
BlockTable bt = transaction.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord activeSpace = transaction.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
activeSpace.AppendEntity(blockRef);
transaction.AddNewlyCreatedDBObject(blockRef, true);
transaction.Commit();
}
4、主要步骤
- 获取当前活动文档、数据库和编辑器对象。
- 设置一个选择过滤器,只允许用户选择”宗地”图层上的LWPOLYLINE对象。
- 用户根据过滤规则进行实体选择后,程序处理所选中的每个闭合多段线。
- 对于每个闭合多段线,首先确保它是闭合的且至少有一个顶点,并将所有顶点存储到一个列表中。
- 注释部分:这部分原来包含计算多边形方向和排序顶点的逻辑,但后来被注释掉,实际代码中并没有执行这部分操作。
- 遍历排序后的顶点列表,为每个顶点创建一个文本标签(DBText)并在模型空间中绘制,标签内容是其顺序编号,位置基于顶点坐标并进行了缩放和平移处理。
- 同样针对每个顶点,在模型空间中插入名为“GXGLQTC”的块参照,并将其定位在与文本标签相同的位置。
另外,还提供了一个辅助方法GetCenterOfPolyline
,用于服务器托管网计算给定多段线的中心点,但在当前代码片段中并未使用此方法来确定文本标签或块参照的位置。
5、完整代码
namespace cad自定义面板集.jzd
{
internal class jzd01
{
// 选定"宗地"图层上的封闭对象(例如闭合多段线)
[CommandMethod("GenerateBoundaryPoints")]
public static void GenerateBoundaryPoints()
{
// 获取当前活动文档和数据库
// 获取当前AutoCAD应用中的活动文档、数据库和编辑器对象
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 创建一个选择过滤器,限制用户只能选择"宗地"图层上的LWPOLYLINE对象作为外部边界
SelectionFilter outerFilter = new SelectionFilter(new TypedValue[] {
new TypedValue((int)DxfCode.Start, "LWPOLYLINE"),
new TypedValue((int)DxfCode.LayerName, "宗地")
});
// 提示用户根据上述规则进行实体选择,并获取选择结果
PromptSelectionResult outerSelRes = ed.GetSelection(outerFilter);
// 检查用户是否成功选择了实体
if (outerSelRes.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())// 开始事务处理以确保数据一致性
{
foreach (ObjectId outerId in outerSelRes.Value.GetObjectIds())// 遍历所有被选中的外部多段线
{
Polyline outerPolyline = (Polyline)tr.GetObject(outerId, OpenMode.ForRead);
// 确保所选多段线是闭合的且至少有一个顶点
if (outerPolyline.Closed && outerPolyline.NumberOfVertices > 0)
服务器托管网 {
List sortedOuterPoints = new List();
for (int i = 0; i
// {
// return new
// {
// MinX = Math.Min(a.MinX, b.X),
// MaxX = Math.Max(a.MaxX, b.X),
// MinY = Math.Min(a.MinY, b.Y),
// MaxY = Math.Max(a.MaxY, b.Y)
// };
// });
找到左上角的顶点作为候选西北角
//Point2d topLeftCorner = new Point2d(boundingBox.MinX, boundingBox.MaxY);
找到最接近左上角(西北方向)的顶点索引
//int startVertexIndex = outerPoints.IndexOf(outerPoints.OrderBy(p => Math.Pow(p.X - topLeftCorner.X, 2) + Math.Pow(p.Y - topLeftCorner.Y, 2)).First());
确保起始顶点是真正的西北角,并按照顺时针排序
//bool isClockwiseFromNorthwest = true;
//int j = outerPoints.Count - 1;
//double sum = 0;
//for (int i = 0; i 0)
//{
// isClockwiseFromNorthwest = false;
//}
//else
//{
// isClockwiseFromNorthwest = true;
//}
//ed.WriteMessage(isClockwiseFromNorthwest + "方向n");
创建一个新的列表,包含从西北角开始按顺时针顺序排列的顶点
//List sortedOuterPoints = new List();
//if (!isClockwiseFromNorthwest)
//{
// ed.WriteMessage(isClockwiseFromNorthwest + "方向1");
// // 如果原始顺序不是从西北角开始顺时针排列,则反转整个列表并重新调整起始点位置
// var reversedOuterPoints = outerPoints.ToList();
// reversedOuterPoints.Reverse();//反序
// sortedOuterPoints.AddRange(reversedOuterPoints);
// sortedOuterPoints.RemoveAt(sortedOuterPoints.Count - 1); // 移除最后一个顶点(与第一个顶点重叠)
// sortedOuterPoints.Insert(0, outerPoints[startVertexIndex]);
//}
//else
//{
// ed.WriteMessage(isClockwiseFromNorthwest + "方向2");
// // 否则直接使用原始列表并调整起始顶点位置
// sortedOuterPoints.AddRange(outerPoints.Skip(startVertexIndex));
// sortedOuterPoints.Insert(0, outerPoints[startVertexIndex]);
//}
// 使用sortedOuterPoints进行后续操作
if (sortedOuterPoints.Count > 0)
{
Dictionary pointIndexDict = new Dictionary();
for (int i = 0; i
//感谢大家的点赞,收藏,转发,关注
//附送AI图片无版权 随意用 龙年大吉大利
通义万相 阿里最新推出的A绘画创作模型
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
相关推荐: 深度解读《深度探索C++对象模型》之C++虚函数实现分析(一)
接下来我将持续更新“深度解读《深度探索C++对象模型》”系列,敬请期待,欢迎关注!也可以关注公众号:iShare爱分享,自动获得推文和全部的文章列表。 假如有这样的一段代码,代码中定义了一个Object类,类中有一个成员函数print,通过以下的两种调用方式调…