c#获得solidworks零件的 面邻接矩阵
总共找到 23 个面 邻接关系: 面 0: 1 2 3 14 面 1: 0 12 13 11 面 2: 0 7 8 13 面 3: 0 6 8 12 面 4: 5 6 7 8 面 5: 4 6 7 15 面 6: 5 4 3 17 面 7: 5 4 2 16 面 8: 4 3 2 10 面 9: 10 11 12 13 面 10: 9 12 13 8 面 11: 9 12 13 1 面 12: 9 10 11 3 1 面 13: 9 10 11 1 2 面 14: 0 16 17 20 面 15: 5 16 17 19 面 16: 14 7 15 21 面 17: 14 6 15 22 面 18: 19 20 21 22 面 19: 18 21 22 15 面 20: 18 21 22 14 面 21: 20 18 19 16 面 22: 20 18 19 17 完整邻接矩阵: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 0: 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1: 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 2: 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 3: 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 4: 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5: 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 6: 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 7: 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 8: 0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 9: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0: 0 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1: 0 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 2: 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0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0 0 0 0 1 1 1 1 4: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 5: 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 6: 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 7: 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 4: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 5: 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 4: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 4: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0 0 5: 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 6: 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 7: 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 8: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 9: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 1 1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0 2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0根据您提供的邻接矩阵结果,这是SolidWorks模型中23个面之间的邻接关系的数学表示。让我详细解释一下:
邻接矩阵含义
基本概念
- 面的数量:您的模型共有23个面(编号0-22)
- 邻接定义:两个面如果通过一条公共边相连,则称为相邻面
邻接关系解析
1. 第一部分:邻接列表
面 0: 1 2 3 14
表示面0与面1、面2、面3、面14相邻
2. 第二部分:邻接矩阵
- 矩阵大小:23×23
- 数值含义:
1表示两个面对应位置相邻0表示两个面不相邻- 矩阵是对称的(因为邻接关系是双向的)
模型拓扑特征分析
高度连接的面
- 面12和面13:各与5个面相邻,是模型中连接最广泛的面
- 面0:与4个面相邻,也是重要连接点
连接模式
您的模型似乎具有以下结构特点:
- 前部区域(面0-13):形成一个密集连接的子图
- 后部区域(面14-22):另一个密集连接的子图
- 中间连接:面4-8作为两个区域之间的桥梁
拓扑意义
这种邻接关系反映了SolidWorks模型的几何结构:
- 面与面的共享边界决定了邻接关系
- 复杂几何体会有更多邻接关系
- 简单几何体(如立方体)通常有较少的邻接关系
实际应用价值
- 模型分析:了解几何体的拓扑结构
- 特征识别:帮助识别模型的重要几何特征
- 算法优化:为后续的几何处理算法提供基础数据
- 质量检查:验证模型的拓扑完整性
这实际上是一个图论中的无向图,其中每个面是节点,邻接关系是边。这种表示在计算机辅助设计(CAD)和计算机图形学中非常有用。
using System; using System.IO; using System.Runtime.InteropServices; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; using View = SolidWorks.Interop.sldworks.View; namespace tools { class get_all_edges { /// <summary> /// 获取工程图中的尺寸信息 /// </summary> static public void run() { // 连接到 SolidWorks 应用程序 SldWorks? swApp = Connect.run(); if (swApp == null) { Console.WriteLine("错误:无法连接到 SolidWorks 应用程序。"); return; } // 获取活动文档并转换为 DrawingDoc 类型 ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc; if (swModel == null) { Console.WriteLine("错误:没有打开的活动文档。"); return; } PartDoc partDoc = (PartDoc)swModel; // 获取所有实体 object[] vBodies = (object[])partDoc.GetBodies2((int)swBodyType_e.swSolidBody, false); // 存储邻接矩阵的字典:面索引 -> 相邻面集合 var adjacencyMatrix = new System.Collections.Generic.Dictionary<int, System.Collections.Generic.HashSet<int>>(); var faceIndexMap = new System.Collections.Generic.Dictionary<Face2, int>(); // 面对象到索引的映射 var facesList = new System.Collections.Generic.List<Face2>(); // 面对象列表 int faceCount = 0; foreach (Body2 body in vBodies) { object[] vedges = (object[])body.GetEdges(); foreach (Edge edge in vedges) { var twoAdjacentFaces = (object[])edge.GetTwoAdjacentFaces(); if (twoAdjacentFaces != null && twoAdjacentFaces.Length >= 2) { Face2 face1 = (Face2)twoAdjacentFaces[0]; Face2 face2 = (Face2)twoAdjacentFaces[1]; // 为每个面分配唯一索引 if (!faceIndexMap.ContainsKey(face1)) { faceIndexMap[face1] = faceCount++; facesList.Add(face1); } if (!faceIndexMap.ContainsKey(face2)) { faceIndexMap[face2] = faceCount++; facesList.Add(face2); } int index1 = faceIndexMap[face1]; int index2 = faceIndexMap[face2]; // 初始化邻接矩阵条目 if (!adjacencyMatrix.ContainsKey(index1)) adjacencyMatrix[index1] = new System.Collections.Generic.HashSet<int>(); if (!adjacencyMatrix.ContainsKey(index2)) adjacencyMatrix[index2] = new System.Collections.Generic.HashSet<int>(); // 添加邻接关系 adjacencyMatrix[index1].Add(index2); adjacencyMatrix[index2].Add(index1); } } } // 输出邻接矩阵信息 Console.WriteLine($"总共找到 {faceCount} 个面"); Console.WriteLine("邻接关系:"); for (int i = 0; i < faceCount; i++) { Console.Write($"面 {i}: "); if (adjacencyMatrix.ContainsKey(i)) { foreach (int adjacentFace in adjacencyMatrix[i]) { Console.Write($"{adjacentFace} "); } } Console.WriteLine(); } // 如果需要完整的邻接矩阵形式,可以创建二维数组 bool[,] fullAdjacencyMatrix = CreateFullAdjacencyMatrix(adjacencyMatrix, faceCount); // 打印完整的邻接矩阵 PrintAdjacencyMatrix(fullAdjacencyMatrix, faceCount); } /// <summary> /// 创建完整的邻接矩阵 /// </summary> private static bool[,] CreateFullAdjacencyMatrix( System.Collections.Generic.Dictionary<int, System.Collections.Generic.HashSet<int>> adjacencyDict, int size) { bool[,] matrix = new bool[size, size]; foreach (var kvp in adjacencyDict) { int faceIndex = kvp.Key; foreach (int adjacentFace in kvp.Value) { matrix[faceIndex, adjacentFace] = true; matrix[adjacentFace, faceIndex] = true; // 确保对称性 } } return matrix; } /// <summary> /// 打印邻接矩阵 /// </summary> private static void PrintAdjacencyMatrix(bool[,] matrix, int size) { Console.WriteLine("\n完整邻接矩阵:"); Console.Write(" "); for (int j = 0; j < size; j++) { Console.Write($"{j % 10} "); } Console.WriteLine(); for (int i = 0; i < size; i++) { Console.Write($"{i % 10}: "); for (int j = 0; j < size; j++) { Console.Write($"{(matrix[i, j] ? "1" : "0")} "); } Console.WriteLine(); } } } }点击链接加入群聊【solidworks神经网络自动标注小白群】:https://qm.qq.com/q/RSpuwGqQYq
