usingSystem;usingSolidWorks.Interop.sldworks;usingSolidWorks.Interop.swconst;usingViewSolidWorks.Interop.sldworks.View;usingSystem.Diagnostics;namespacetools{publicclassselect_face_recognize{/// summary/// 获取当前选中的面并分析面积/// /summarystaticpublicvoidrun(ModelDoc2swModel){if(swModelnull){Console.WriteLine(错误没有打开的活动文档。);return;}Debug.WriteLine(\n 开始分析选中的面 );// 获取选择管理器SelectionMgrselectionMgr(SelectionMgr)swModel.SelectionManager;if(selectionMgrnull){Debug.WriteLine(错误无法获取选择管理器。);return;}// 获取选中对象数量intselectCountselectionMgr.GetSelectedObjectCount();if(selectCount0){Debug.WriteLine(警告没有选中的对象请先选择面。);return;}Debug.WriteLine($选中对象总数{selectCount});// 遍历所有选中的对象for(inti1;iselectCount;i){try{// 获取选中对象的类型intobjectTypeselectionMgr.GetSelectedObjectType3(i,-1);Debug.WriteLine($\n--- 选中对象{i}---);Debug.WriteLine($对象类型{(swSelectType_e)objectType});// 判断是否为面if(objectType(int)swSelectType_e.swSelFACES){Face2selectedFace(Face2)selectionMgr.GetSelectedObject6(i,-1);if(selectedFace!null){AnalyzeFace(selectedFace,i);}else{Debug.WriteLine($ 错误无法获取面对象{i});}}elseif(objectType(int)swSelectType_e.swSelEDGES){// 如果选中的是边获取相邻面EdgeselectedEdge(Edge)selectionMgr.GetSelectedObject6(i,-1);if(selectedEdge!null){Debug.WriteLine( 选中的是边获取相邻面:);varadjacentFaces(object[])selectedEdge.GetTwoAdjacentFaces();if(adjacentFaces!nulladjacentFaces.Length0){for(intj0;jadjacentFaces.Length;j){if(adjacentFaces[j]isFace2adjFace){Debug.WriteLine($ 相邻面{j1}:);AnalyzeFace(adjFace,i);}}}else{Debug.WriteLine( 该边没有相邻面。);}}}else{Debug.WriteLine($ 跳过非面对象 (类型{(swSelectType_e)objectType}));}}catch(Exceptionex){Debug.WriteLine($ 处理选中对象{i}时出错{ex.Message});}}Debug.WriteLine(\n 分析完成 );}/// summary/// 分析面的详细信息/// /summarystaticprivatevoidAnalyzeFace(Face2face,intindex){if(facenull){Debug.WriteLine($ 面对象为空);return;}try{// 计算面积 (mm²)doubleareaMath.Round(face.GetArea()*1000000,2);// 获取曲面类型varsurfaceface.IGetSurface();stringfaceType未知;if(surface.IsPlane())faceType平面;elseif(surface.IsCylinder())faceType圆柱面;elseif(surface.IsCone())faceType圆锥面;elseif(surface.IsSphere())faceType球面;elseif(surface.IsTorus())faceType圆环面;elsefaceType其他曲面;// 获取面的法向double[]normal(double[])face.Normal;stringnormalStr;if(normal!nullnormal.Length3){normalStr$, 法向({normal[0]:F3},{normal[1]:F3},{normal[2]:F3});}// 获取面的边界信息object[]edges(object[])face.GetEdges();intedgeCountedges?.Length??0;// 输出面的信息Debug.WriteLine($ 面{index}:);Debug.WriteLine($ 面积{area}mm²);Debug.WriteLine($ 类型{faceType}{normalStr});Debug.WriteLine($ 边数{edgeCount});// 如果是圆柱面显示半径if(surface.IsCylinder()){double[]cylinderParams(double[])surface.CylinderParams;if(cylinderParams!nullcylinderParams.Length7){doubleradiusMath.Round(cylinderParams[6]*1000,2);Debug.WriteLine($ 圆柱半径{radius}mm);}}// 如果是圆锥面显示角度elseif(surface.IsCone()){double[]coneParams(double[])surface.ConeParams;if(coneParams!nullconeParams.Length7){doubleangleMath.Round(coneParams[6]*180/Math.PI,2);Debug.WriteLine($ 圆锥半角{angle}°);}}}catch(Exceptionex){Debug.WriteLine($ 获取面信息时出错{ex.Message});}}}}
solidworks获得工程图选中面selectionMgr.GetSelectedObjectType3(i, -1)
usingSystem;usingSolidWorks.Interop.sldworks;usingSolidWorks.Interop.swconst;usingViewSolidWorks.Interop.sldworks.View;usingSystem.Diagnostics;namespacetools{publicclassselect_face_recognize{/// summary/// 获取当前选中的面并分析面积/// /summarystaticpublicvoidrun(ModelDoc2swModel){if(swModelnull){Console.WriteLine(错误没有打开的活动文档。);return;}Debug.WriteLine(\n 开始分析选中的面 );// 获取选择管理器SelectionMgrselectionMgr(SelectionMgr)swModel.SelectionManager;if(selectionMgrnull){Debug.WriteLine(错误无法获取选择管理器。);return;}// 获取选中对象数量intselectCountselectionMgr.GetSelectedObjectCount();if(selectCount0){Debug.WriteLine(警告没有选中的对象请先选择面。);return;}Debug.WriteLine($选中对象总数{selectCount});// 遍历所有选中的对象for(inti1;iselectCount;i){try{// 获取选中对象的类型intobjectTypeselectionMgr.GetSelectedObjectType3(i,-1);Debug.WriteLine($\n--- 选中对象{i}---);Debug.WriteLine($对象类型{(swSelectType_e)objectType});// 判断是否为面if(objectType(int)swSelectType_e.swSelFACES){Face2selectedFace(Face2)selectionMgr.GetSelectedObject6(i,-1);if(selectedFace!null){AnalyzeFace(selectedFace,i);}else{Debug.WriteLine($ 错误无法获取面对象{i});}}elseif(objectType(int)swSelectType_e.swSelEDGES){// 如果选中的是边获取相邻面EdgeselectedEdge(Edge)selectionMgr.GetSelectedObject6(i,-1);if(selectedEdge!null){Debug.WriteLine( 选中的是边获取相邻面:);varadjacentFaces(object[])selectedEdge.GetTwoAdjacentFaces();if(adjacentFaces!nulladjacentFaces.Length0){for(intj0;jadjacentFaces.Length;j){if(adjacentFaces[j]isFace2adjFace){Debug.WriteLine($ 相邻面{j1}:);AnalyzeFace(adjFace,i);}}}else{Debug.WriteLine( 该边没有相邻面。);}}}else{Debug.WriteLine($ 跳过非面对象 (类型{(swSelectType_e)objectType}));}}catch(Exceptionex){Debug.WriteLine($ 处理选中对象{i}时出错{ex.Message});}}Debug.WriteLine(\n 分析完成 );}/// summary/// 分析面的详细信息/// /summarystaticprivatevoidAnalyzeFace(Face2face,intindex){if(facenull){Debug.WriteLine($ 面对象为空);return;}try{// 计算面积 (mm²)doubleareaMath.Round(face.GetArea()*1000000,2);// 获取曲面类型varsurfaceface.IGetSurface();stringfaceType未知;if(surface.IsPlane())faceType平面;elseif(surface.IsCylinder())faceType圆柱面;elseif(surface.IsCone())faceType圆锥面;elseif(surface.IsSphere())faceType球面;elseif(surface.IsTorus())faceType圆环面;elsefaceType其他曲面;// 获取面的法向double[]normal(double[])face.Normal;stringnormalStr;if(normal!nullnormal.Length3){normalStr$, 法向({normal[0]:F3},{normal[1]:F3},{normal[2]:F3});}// 获取面的边界信息object[]edges(object[])face.GetEdges();intedgeCountedges?.Length??0;// 输出面的信息Debug.WriteLine($ 面{index}:);Debug.WriteLine($ 面积{area}mm²);Debug.WriteLine($ 类型{faceType}{normalStr});Debug.WriteLine($ 边数{edgeCount});// 如果是圆柱面显示半径if(surface.IsCylinder()){double[]cylinderParams(double[])surface.CylinderParams;if(cylinderParams!nullcylinderParams.Length7){doubleradiusMath.Round(cylinderParams[6]*1000,2);Debug.WriteLine($ 圆柱半径{radius}mm);}}// 如果是圆锥面显示角度elseif(surface.IsCone()){double[]coneParams(double[])surface.ConeParams;if(coneParams!nullconeParams.Length7){doubleangleMath.Round(coneParams[6]*180/Math.PI,2);Debug.WriteLine($ 圆锥半角{angle}°);}}}catch(Exceptionex){Debug.WriteLine($ 获取面信息时出错{ex.Message});}}}}