njsgcs 2024-05-28 16:35 采纳率: 100%
浏览 30
已结题

c#solidworks 二次开发 工程图标折弯尺寸 ,目前可以标

c#solidworks 二次开发 工程图标边线法兰 折弯尺寸标注

img

谢谢各位佬,看大家的回答有很多启发,目前可以标出来




using netDxf.Entities;
using NPOI.SS.Formula.Functions;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

using Dimension = SolidWorks.Interop.sldworks.Dimension;
using Edge = SolidWorks.Interop.sldworks.Edge;
using View = SolidWorks.Interop.sldworks.View;

namespace solidgai
{
    public static class sw方法
    {

        public static void 标折弯尺寸(ISldWorks SwApp)
        {

            ModelDoc2 swModel = (ModelDoc2)SwApp.ActiveDoc;
            DrawingDoc swDrawing = (DrawingDoc)swModel;
            Sheet sheet = (Sheet)swDrawing.GetCurrentSheet();

            var swMathUtils = SwApp.IGetMathUtility();

            string partname = Path.GetFileNameWithoutExtension(swModel.GetPathName());
            var Views = (object[])sheet.GetViews();

            foreach (var view2 in Views)
            {
                View view = (View)view2;
                double Offset = 0.05;

                if (view.GetName2().Contains("图"))
                {

                    List<(Face, string, bool, double)> faces = new List<(Face, string, bool, double)>();

                    string viewname = view.GetName2();
                    Debug.WriteLine("视图名称为:" + viewname);
                    var swViewXform = (MathTransform)view.ModelToViewTransform;
                    var ViewTransformDATA = (double[])swViewXform.ArrayData;

                    Debug.WriteLine($":{Math.Round(ViewTransformDATA[0], 2)},{Math.Round(ViewTransformDATA[1], 2)},{Math.Round(ViewTransformDATA[2], 2)},{Math.Round(ViewTransformDATA[13], 2)}");
                    Debug.WriteLine($":{Math.Round(ViewTransformDATA[3], 2)},{Math.Round(ViewTransformDATA[4], 2)},{Math.Round(ViewTransformDATA[5], 2)},{Math.Round(ViewTransformDATA[14], 2)}");
                    Debug.WriteLine($":{Math.Round(ViewTransformDATA[6], 2)},{Math.Round(ViewTransformDATA[7], 2)},{Math.Round(ViewTransformDATA[8], 2)},{Math.Round(ViewTransformDATA[15], 2)}");
                    Debug.WriteLine($":{Math.Round(ViewTransformDATA[9], 2)},{Math.Round(ViewTransformDATA[10], 2)},{Math.Round(ViewTransformDATA[11], 2)},{Math.Round(ViewTransformDATA[12], 2)}");


                    var vBounds = (double[])view.GetOutline();
                    view.SetDisplayMode3(false, (int)swDisplayMode_e.swFACETED_HIDDEN_GREYED, false, true);
                    Console.WriteLine("view.GetDisplayMode()=" + view.GetDisplayMode());
                    swModel.EditRebuild3();
                    PartDoc partDoc = (PartDoc)view.ReferencedDocument;
                    object[] vBodies = (object[])partDoc.GetBodies2((int)swBodyType_e.swSolidBody, false);
                    Body2 body = (Body2)vBodies[0];
                    object[] vAffectedFaces = (object[])body.GetFaces();
                    List<(Face, string, double, Edge)> 面集 = vAffectedFaces
                        .Where(item =>
                        {
                            Surface plansurface = (Surface)((Face)item).GetSurface();
                            object[] edges = (object[])((Face)item).GetEdges();
                            return plansurface.IsPlane() && ((Face)item).GetArea() * 1000000 > 10 && edges.Length > 3;
                        })
                        .Select(item =>
                        {
                            Surface plansurface = (Surface)((Face)item).GetSurface();

                            double[] Params = (double[])plansurface.PlaneParams;
                            (double, double, double) 平面朝向 = (Math.Round(Params[0]), Math.Round(Params[1]), Math.Round(Params[2]));
                            string 方向 = "";
                            double 位置值 = 0;
                            object[] edges = (object[])((Face)item).GetEdges();
                            Debug.WriteLine("edges.count=" + edges.Length + ",面积为=" + ((Face)item).GetArea() * 1000000 + $"平面朝向={平面朝向.Item1},{平面朝向.Item2},{平面朝向.Item3}");

                            Edge eedge = (Edge)edges[0];


                            Vertex StartVertex = (Vertex)eedge.GetStartVertex();

                            double[] pParams = (double[])StartVertex.GetPoint();


                            if (平面朝向.Item1 != 0)
                            {
                                位置值 = Math.Round((double)pParams[0] * 1000, 1);
                                方向 = "x";
                            }
                            else if (平面朝向.Item2 != 0)
                            {
                                位置值 = Math.Round((double)pParams[1] * 1000, 1);
                                方向 = "y";
                            }
                            else if (平面朝向.Item3 != 0)
                            {
                                位置值 = Math.Round((double)pParams[2] * 1000, 1);
                                方向 = "z";
                            }




                            Debug.WriteLine("面位置为:" + 位置值);
                            return ((Face)item, 方向, 位置值, eedge);
                        }).ToList();
                    Feature firstfeature = (Feature)partDoc.FirstFeature();

                    while (firstfeature != null)
                    {

                        List<Face> 平面集 = new List<Face>();
                        if (firstfeature.GetTypeName2() == "EdgeFlange")
                        {
                            EdgeFlangeFeatureData swEdgeFlange = default(EdgeFlangeFeatureData);
                            swEdgeFlange = (EdgeFlangeFeatureData)firstfeature.GetDefinition();
                            double 折弯半径 = swEdgeFlange.BendRadius * 1000;
                            bool 激活成功 = swDrawing.ActivateView(viewname);

                            string 特征名称 = firstfeature.Name + "@" + partname + "@" + viewname;
                            激活成功 = swModel.Extension.SelectByID2(特征名称, "BODYFEATURE", 0, 0, 0, false, 0, null, 0);
                            Console.WriteLine("特征名称=" + 特征名称 + ",激活成功?" + 激活成功);
                            var Annotations = (object[])swDrawing.InsertModelAnnotations3(1, 32776, true, false, false, true);

                            if (Annotations != null)
                            {
                                foreach (var Annotation2 in Annotations)
                                {
                                    Annotation Annotation = (Annotation)Annotation2;
                                    var swDisplayDimension = (DisplayDimension)Annotation.GetSpecificAnnotation();
                                    if (swDisplayDimension.Type2 == (int)swDimensionType_e.swAngularDimension)
                                    { Annotation.Select(true); swModel.EditDelete(); }
                                    swModel.ClearSelection2(true);
                                }
                            }
                            object[] faces2 = (object[])firstfeature.GetFaces();

                            var facelist = faces2.Select(item => (Face)item).OrderByDescending(item => item.GetArea()).ToList();
                            if (facelist.Skip(1).FirstOrDefault() != default)
                            {
                                平面集.Add(facelist.FirstOrDefault());
                                平面集.Add(facelist.Skip(1).FirstOrDefault());
                            }
                            foreach (var face in 平面集)
                            {

                                Surface plansurface = (Surface)face.GetSurface();
                                double[] Params = (double[])plansurface.PlaneParams;
                                (double, double, double) 平面朝向 = (Math.Round(Params[0]), Math.Round(Params[1]), Math.Round(Params[2]));
                                object[] Edges = (object[])face.GetEdges();
                                foreach (var edge2 in Edges)
                                {
                                    Edge edge = (Edge)edge2;
                                    object[] twofaces = (object[])edge.GetTwoAdjacentFaces();
                                    Curve linecurve = (Curve)edge.GetCurve();
                                    double[] lineParams = (double[])linecurve.LineParams;
                                    foreach (var face2 in twofaces)
                                    {
                                        Face oneface = (Face)face2;
                                        Surface surface = (Surface)oneface.GetSurface();
                                        if (surface.IsCylinder())
                                        {
                                            double[] CylinderParams = (double[])surface.CylinderParams;


                                            if (Math.Round(CylinderParams[6] * 1000 - 折弯半径) != 0)
                                            {
                                                bool 计过了 = false;
                                                object[] edges = (object[])oneface.GetEdges();
                                                foreach (var eedge2 in edges)
                                                {
                                                    Edge eedge = (Edge)eedge2;

                                                    Curve ccurve = (Curve)eedge.GetCurve();
                                                    if (ccurve.IsCircle() && !计过了)
                                                    {
                                                        计过了 = true;
                                                        bool 正方向 = false;
                                                        string 方向 = "";
                                                        double[] CircleParams = (double[])ccurve.CircleParams;
                                                        if (平面朝向.Item1 != 0)
                                                        {
                                                            正方向 = lineParams[0] < CircleParams[0];
                                                            方向 = "x";
                                                            faces.Add((face, 方向, 正方向, Math.Round(lineParams[0] * 1000, 1)));
                                                        }
                                                        else if (平面朝向.Item2 != 0)
                                                        {
                                                            正方向 = lineParams[1] < CircleParams[1];
                                                            方向 = "y";
                                                            faces.Add((face, 方向, 正方向, Math.Round(lineParams[1] * 1000, 1)));
                                                        }
                                                        else if (平面朝向.Item3 != 0)
                                                        {
                                                            正方向 = lineParams[2] < CircleParams[2];
                                                            方向 = "z";
                                                            faces.Add((face, 方向, 正方向, Math.Round(lineParams[2] * 1000, 1)));
                                                        }

                                                        Console.WriteLine("面积=" + face.GetArea() * 1000000 + $"    ,正方向?  {正方向}");

                                                    }
                                                }


                                            }
                                        }
                                    }
                                }
                            }
                        }


                        firstfeature = (Feature)firstfeature.GetNextFeature();
                    }

                    Console.WriteLine("faces.count=" + faces.Count);
                    Dictionary<Face, Face> 标过的面 = new Dictionary<Face, Face>();
                    foreach (var face实例 in faces)
                    {
                        string 放置方式 = "无";
                        if (face实例.Item2 == "x")
                        {
                            if (ViewTransformDATA[0] != 0) { 放置方式 = "水平"; }
                            else if (ViewTransformDATA[1] != 0) { 放置方式 = "竖直"; }
                        }
                        else if (face实例.Item2 == "y")
                        {
                            if (ViewTransformDATA[3] != 0) { 放置方式 = "水平"; }
                            else if (ViewTransformDATA[4] != 0) { 放置方式 = "竖直"; }
                        }
                        else if (face实例.Item2 == "z")
                        {
                            if (ViewTransformDATA[6] != 0) { 放置方式 = "水平"; }
                            else if (ViewTransformDATA[7] != 0) { 放置方式 = "竖直"; }
                        }
                        Debug.WriteLine("放置方式为:" + 放置方式);
                        if (放置方式 == "无") continue;
                        List<Face> 同向量集 = 面集.Where(item => item.Item2 == face实例.Item2).OrderByDescending(item => item.Item3)
                            .Select(item =>
                            {

                                return item.Item1;
                            }).ToList();

                        Face 匹配面 = face实例.Item3 ? 同向量集.FirstOrDefault() : 同向量集.LastOrDefault();
                        Face 标过此面;
                        if (标过的面.TryGetValue(匹配面, out 标过此面) && 标过此面 == face实例.Item1)
                        {
                            Debug.WriteLine("标过此面"); continue;
                        }
                        else
                        {
                            标过的面.Add(face实例.Item1, 匹配面);
                            Debug.WriteLine("没标过此面");
                        }
                        object[] edges = (object[])匹配面.GetEdges();
                        object[] edges2 = (object[])face实例.Item1.GetEdges();

                        double 相差 = 0; int i = 0;
                        while (true)
                        {
                            Vertex vertex = (Vertex)((Edge)edges2[i]).GetStartVertex();
                            Vertex endvertex = (Vertex)((Edge)edges2[i]).GetEndVertex();
                            SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
                            SelectData swSelData = swSelMgr.CreateSelectData();

                            //((Entity)vertex).Select4(false, swSelData);
                            //((Entity)face实例.Item5).Select4(true, swSelData);

                            var vPt = (double[])vertex.GetPoint();
                            var swMathPt = (MathPoint)swMathUtils.CreatePoint(vPt);
                            swMathPt = (MathPoint)swMathPt.MultiplyTransform(swViewXform);
                            var MathPtData = (double[])swMathPt.ArrayData;

                            var endvPt = (double[])endvertex.GetPoint();
                            var endswMathPt = (MathPoint)swMathUtils.CreatePoint(endvPt);
                            endswMathPt = (MathPoint)endswMathPt.MultiplyTransform(swViewXform);
                            var endMathPtData = (double[])endswMathPt.ArrayData;
                            Console.WriteLine($"swMathPt1:{Math.Round((MathPtData[0] + endMathPtData[0]) / 2, 3)},{Math.Round((MathPtData[1] + endMathPtData[1]) / 2, 3)},{Math.Round((MathPtData[2] + endMathPtData[2]) / 2, 3)}");
                            if (放置方式 == "水平")
                            {
                                相差 = Math.Round(MathPtData[1] * 1000 - endMathPtData[1] * 1000, 1);
                                Console.WriteLine("相差=" + 相差 + "," + Math.Round(MathPtData[1] * 1000, 3) + "," + Math.Round(endMathPtData[1] * 1000, 3));
                            }
                            else if (放置方式 == "竖直")
                            {
                                相差 = Math.Round(MathPtData[0] * 1000 - endMathPtData[0] * 1000, 1);
                                Console.WriteLine("相差=" + 相差 + "," + Math.Round(MathPtData[0] * 1000, 3) + "," + Math.Round(MathPtData[0] * 1000, 3));
                            }
                            else
                            {
                                Console.WriteLine("放置方式为其他");
                            }
                            if (相差 != 0) { swModel.Extension.SelectByRay((MathPtData[0] + endMathPtData[0]) / 2, (MathPtData[1] + endMathPtData[1]) / 2, 0, 0, 0, -1, 0.0001, 1, false, 0, 0); break; }
                            i++;
                        }

                        相差 = 0; i = 0;
                        while (true)
                        {
                            Vertex vertex = (Vertex)((Edge)edges[i]).GetStartVertex();
                            Vertex endvertex = (Vertex)((Edge)edges[i]).GetEndVertex();
                            SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
                            SelectData swSelData = swSelMgr.CreateSelectData();

                            //((Entity)vertex).Select4(false, swSelData);
                            //((Entity)face实例.Item5).Select4(true, swSelData);

                            var vPt = (double[])vertex.GetPoint();
                            var swMathPt = (MathPoint)swMathUtils.CreatePoint(vPt);
                            swMathPt = (MathPoint)swMathPt.MultiplyTransform(swViewXform);
                            var MathPtData = (double[])swMathPt.ArrayData;

                            var endvPt = (double[])endvertex.GetPoint();
                            var endswMathPt = (MathPoint)swMathUtils.CreatePoint(endvPt);
                            endswMathPt = (MathPoint)endswMathPt.MultiplyTransform(swViewXform);
                            var endMathPtData = (double[])endswMathPt.ArrayData;
                            Console.WriteLine($"swMathPt1:{Math.Round((MathPtData[0] + endMathPtData[0]) / 2, 3)},{Math.Round((MathPtData[1] + endMathPtData[1]) / 2, 3)},{Math.Round((MathPtData[2] + endMathPtData[2]) / 2, 3)}");
                            if (放置方式 == "水平")
                            {
                                相差 = Math.Round(MathPtData[1] * 1000 - endMathPtData[1] * 1000, 1);
                            }
                            else if (放置方式 == "竖直")
                            {
                                相差 = Math.Round(MathPtData[0] * 1000 - endMathPtData[0] * 1000, 1);
                            }

                            if (相差 != 0) { swModel.Extension.SelectByRay((MathPtData[0] + endMathPtData[0]) / 2, (MathPtData[1] + endMathPtData[1]) / 2, 0, 0, 0, -1, 0.0001, 1, false, 0, 0); break; }
                            i++;
                        }
                        double Xpos, Ypos;

                        if (放置方式 == "水平")
                        {
                            Xpos = (vBounds[0] + vBounds[2]) / 2;
                            Ypos = (vBounds[3] - Offset);
                            var myDisplayDim = swModel.AddDimension2(Xpos, Ypos, 0);
                        }
                        else if (放置方式 == "竖直")
                        {
                            Ypos = (vBounds[1] + vBounds[3]) / 2;
                            Xpos = (vBounds[0] + Offset);
                            var myDisplayDim = swModel.AddDimension2(Xpos, Ypos, 0);
                        }
                        Offset = Offset + 0.005;

                        swModel.ClearSelection2(true);

                    }
                    Debug.WriteLine("标过的面个数=" + 标过的面.Count);
                }
            }

        }


    }
}

  • 写回答

18条回答 默认 最新

  • 叫兽-郭老师 Java领域新星创作者 2024-05-28 16:36
    关注

    🕒下午好,题主!目前:🟢[在线]
    📢参考通义千问、由郭老师整理提供!
    📣今日诗号:愿得一人心,白首不相离。

    C# Solidworks 二次开发:工程图自动标边线法兰等折弯尺寸

    以下是使用 C# 和 Solidworks API 自动标注边线法兰等折弯尺寸(例如 12.5)的解决方案和代码示例:

    步骤:

    1. 获取模型中的边线特征: 使用 GetEdges 方法获取法兰面的所有边线。
    2. 筛选所需的边线: 根据边线的属性(例如长度、方向)筛选出需要标注尺寸的边线。
    3. 创建尺寸标注: 使用 InsertDimension 方法创建线性尺寸标注,并设置尺寸线的起点和终点。
    4. 设置尺寸标注样式: 根据需要设置尺寸文本格式、箭头样式等。

    代码示例:

    using SolidWorks.Interop.sldworks;
    using SolidWorks.Interop.swdim;
    using System.Runtime.InteropServices;
    
    public class DimensionExample
    {
        public void AddBendDimension(SldWorks swApp, ModelDoc2 swModel, double bendLength)
        {
            // 获取当前工程图
            DrawingDoc swDrawing = (DrawingDoc)swModel;
    
            // 获取视图
            View swView = swDrawing.ActiveView;
    
            // 获取模型中的法兰面
            // 假设法兰面是模型的第一个特征
            Feature swFeature = swModel.FirstFeature(); 
    
            // 获取法兰面的边线
            object[] swEdges = (object[])swFeature.GetEdges();
    
            // 筛选需要标注尺寸的边线
            Edge swTargetEdge = null;
            foreach (Edge swEdge in swEdges)
            {
                // 检查边线长度是否与目标折弯长度相符
                if (Math.Abs(swEdge.GetLength() - bendLength) < 0.01)
                {
                    swTargetEdge = swEdge;
                    break;
                }
            }
    
            if (swTargetEdge != null)
            {
                // 获取边线的起点和终点坐标
                double[] startPt = (double[])swTargetEdge.GetStartVertex().GetPoint();
                double[] endPt = (double[])swTargetEdge.GetEndVertex().GetPoint();
    
                // 创建线性尺寸标注
                Dimension swDimension = (Dimension)swView.InsertDimension(
                    (int)swDimensionType_e.swLinearDimension, // 尺寸类型
                    startPt,                                      // 起点
                    endPt,                                        // 终点
                    null,                                       // 尺寸线位置
                    0                                          // 尺寸文本角度
                );
    
                // 设置尺寸标注样式
                swDimension.Style = (int)swDimensionStyle_e.swDimensionStyleArrow; // 箭头样式
    
                // 更新视图
                swView.Update();
            }
        }
    }
    

    使用方法:

    1. 将代码添加到 Solidworks C# 项目中。
    2. swApp 替换为您的 Solidworks 应用程序对象。
    3. swModel 替换为您的 Solidworks 模型对象。
    4. bendLength 替换为目标折弯长度(例如 12.5)。
    5. 调用 AddBendDimension 方法。

    注意事项:

    • 这只是一个示例代码,您可能需要根据您的具体需求进行修改。
    • 代码中假设法兰面是模型的第一个特征,您需要根据实际情况调整代码。
    • 您需要熟悉 Solidworks API 才能理解和修改代码。

    希望这个解决方案和代码示例能够帮助您自动标注边线法兰的等折弯尺寸。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(17条)

报告相同问题?

问题事件

  • 系统已结题 6月8日
  • 已采纳回答 5月31日
  • 修改了问题 5月31日
  • 修改了问题 5月31日
  • 展开全部

悬赏问题

  • ¥20 关于游戏c++语言代码问题
  • ¥15 如何制作永久二维码,最好是微信也可以扫开的。(相关搜索:管理系统)
  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞