• C# SolidWorks二次开发---工程图简单版标注长宽



    工程图中标注长宽

    工程图中标注常用方法有哪些?请思考再继续查看。

    一、开发目标

    在常规矩形零件的工程图中标注最大外形尺寸用于下料(此次不考虑圆形,因为我也不会。)

    二、方案解析

    1.思路

    开始
    遍历视图
    遍历所有可见体
    遍历可见线
    排序点得到长宽
    标注长宽
    找对应投影视图
    视图投影视图
    视图投影视图中标注
    下一视图
    结束

    代码如下(示例):

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    import warnings
    warnings.filterwarnings('ignore')
    import  ssl
    ssl._create_default_https_context = ssl._create_unverified_context
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.关键代码

    代码如下(示例):

    namespace CSharpAndSolidWorks
    {
        /// <summary>
        /// 给工程图标注长宽
        /// </summary>
        internal class AddSizeDimensionForDrawing
        {
            public SldWorks swApp { get; set; }
            public ModelDoc2 swModel  { get; set; }
            public SelectData swSelData { get; set; }
            public string  strError { get; set; }
    
            //public string Options = "Two";
    
            /// <summary>
            /// 尺寸标注上方
            /// </summary>
            public bool DimOnTop = true;
    
            /// <summary>
            /// 尺寸标注左侧
            /// </summary>
            public bool DimOnLeft = true;
    
            /// <summary>
            /// 尺寸位置偏移量
            /// </summary>
            public double HorOffset = 0.005;
            /// <summary>
            /// 尺寸位置偏移量
            /// </summary>
            public double VerOffset = 0.005;
    
    
            public AddSizeDimensionForDrawing(SldWorks App, ModelDoc2 model)
            {
    
                swApp = App;
                swModel = model;
    
            }
    
            /// <summary>
            /// 增加长宽尺寸
            /// </summary>
            /// <param name="Top">尺寸位置放左?</param>
            /// <param name="Left">尺寸位置放右?</param>
            public void AutoAddSize(bool Top = true, bool Left = true) {
    
                try
                {
                    DimOnLeft = Left;
                    DimOnTop = Top;
                    AutoOveralDimensions();
                }
                catch (Exception)
                {
    
                    throw;
                }       
            
            
            }
    
            /// <summary>
            /// 标注
            /// </summary>
            public void AutoOveralDimensions() {
                         
    
                var swDraw = swModel as DrawingDoc;
    
                if (swDraw != null) 
                {
                    //当前图纸
                    Sheet swSheet = (Sheet)swDraw.GetCurrentSheet();
                    //所有视图
                    var swViews = (object[])swSheet.GetViews();
    
                    //循环
                    for (int i = 0; i < swViews.Length; i++) {
    
                        View swView = (View)swViews[i];
                        //可以排除关联视图
                        if (swView.Type==(int)swDrawingViewTypes_e.swDrawingNamedView)
                        {
                            if (swView.GetOrientationName() != "*Isometric" && swView.GetOrientationName() != "*Trimetric" && swView.GetOrientationName() != "*Dimetric")
                            {
    
                                ProcessView(swDraw, swView, true, true);
    
                                //找关联视图
                                var depHorView = GetAlignedDependantView(swView, 0);
    
                                if (depHorView != null)
                                {
                                    ProcessView(swDraw, depHorView, true, false);
                                }
                                var depVerView = GetAlignedDependantView(swView, 1);
    
                                if (depVerView != null)
                                {
                                    ProcessView(swDraw, depVerView, false, true);
                                }
                            }
    
                        }
    
    
                    }
    
                }
                swModel.ClearSelection2(true);
                swModel.GraphicsRedraw2();
    
            }
    
            /// <summary>
            /// 找关联视图
            /// </summary>
            /// <param name="swParentView"></param>
            /// <param name="intOrientation">方向,横向/纵向</param>
            /// <returns></returns>
            private View GetAlignedDependantView(View swParentView, int intOrientation)
            {
                var delta = 0.00001;
    
                var intDependantViews = swParentView.GetDependentViewCount(false, (int)swDrawingViewTypes_e.swDrawingProjectedView);
                var objDependantViews = (object[])swParentView.GetDependentViews(false, (int)swDrawingViewTypes_e.swDrawingProjectedView);
    
                var ParentPos =(double[]) swParentView.Position;
    
                for (int i = 0; i < intDependantViews; i++)
                {
                    var depView = (View)objDependantViews[i];
    
                    var DependantPos = (double[])depView.Position;
    
                    if (Math.Abs(ParentPos[Math.Abs(intOrientation - 1)] - DependantPos[Math.Abs(intOrientation - 1)])<delta) {
    
                        return depView;
    
                    }
                }
    
               return null;
            }
    
    
           /// <summary>
           /// 遍历 点 ,计算再标注
           /// </summary>
           /// <param name="swDraw"></param>
           /// <param name="swView"></param>
           /// <param name="PlaceHorDim"></param>
           /// <param name="PlaceVerDim"></param>
            private void ProcessView(DrawingDoc swDraw, View swView, bool PlaceHorDim, bool PlaceVerDim)
            {
    
                List<Vertex> swPcoll= new List<Vertex> ();
    
                swModel.ClearSelection2(true);
    
                var refModel= swView.ReferencedDocument;
    
                var vComps = (object[])swView.GetVisibleComponents();
    
                MathTransform swViewXform = ViewMathTransform(swDraw, swView);
    
                //获取所有直线边点
                for (int j = 0;j < swView.GetVisibleComponentCount(); j++)
                {
                    var vEdges = (object[])swView.GetVisibleEntities((Component2)vComps[j], (int)swViewEntityType_e.swViewEntityType_Edge);
                      swSelData = (SelectData)swModel.ISelectionManager.CreateSelectData();
                        for (int itr = 0; itr < vEdges.Length; itr++)
                        {
                            var swCurve = (Curve)((Edge)vEdges[itr]).GetCurve();
                            if (!swCurve.IsCircle())
                            {
                                var startVertex= (Vertex)((Edge)vEdges[itr]).GetStartVertex();
                                var endVertex = (Vertex)((Edge)vEdges[itr]).GetEndVertex();
    
                                if (startVertex !=null)
                                {
                                    swPcoll.Add(startVertex);
                                }
                                if (endVertex != null)
                                {
                                    swPcoll.Add(endVertex);
                                }
                            }
    
                        }
                }
    
                Entity swEntXmax, swEntXmin, swEntYmax, swEntYmin;
    
    
                if (swPcoll.Count>0)
                {
    
                    swEntXmax = FindExtremun(0, "max", swPcoll, refModel.GetType(), swViewXform);
                    swEntXmin = FindExtremun(0, "min", swPcoll, refModel.GetType(), swViewXform);
                    swEntYmax = FindExtremun(1, "max", swPcoll, refModel.GetType(), swViewXform);
                    swEntYmin = FindExtremun(1, "min", swPcoll, refModel.GetType(), swViewXform);
    
    
                    var vBounds = (double[])swView.GetOutline();
    
                    if (PlaceHorDim)
                    {
                        PlaceOverallDimension(swModel, swEntXmax, swEntXmin, "Horizontal", vBounds);
                    }
    
                    if (PlaceVerDim)
                    {
                        PlaceOverallDimension(swModel, swEntYmax, swEntYmin, "Vertical", vBounds);
                    }
    
                }
    
            }
            /// <summary>
            /// 放置尺寸
            /// </summary>
            /// <param name="swModel"></param>
            /// <param name="swVertex1"></param>
            /// <param name="swVertex2"></param>
            /// <param name="DimOrientation"></param>
            /// <param name="vBounds"></param>
            private void PlaceOverallDimension(ModelDoc2 swModel, Entity swVertex1, Entity swVertex2, string DimOrientation, double[] vBounds)
            {
                double dblConvFactor = GetUnitConvFactor("");
                if (DimOrientation == "Horizontal") {
    
                    HorOffset = HorOffset * dblConvFactor;
    
                }
                if (DimOrientation == "Vertical")
                {
                    VerOffset = VerOffset * dblConvFactor;
                }
                swModel.ClearSelection2(true);
    
                swVertex1.Select4(false, swSelData);
                swVertex2.Select4(true, swSelData);
                double Xpos, Ypos;
    
                if (DimOrientation == "Horizontal")
                {
                    Xpos = (vBounds[0] + vBounds[2]) / 2;
    
                    if(DimOnTop) //标注在顶部
                    {
                        Ypos = (vBounds[3] + HorOffset);
                    }
                    else
                    {
                        Ypos = (vBounds[1] - HorOffset);
                    }
    
                    var myDisplayDim = swModel.AddHorizontalDimension2(Xpos, Ypos, 0);
    
    
                } else if (DimOrientation == "Vertical")
                {
                    Ypos = (vBounds[1] + vBounds[3]) / 2;
    
                    if (DimOnLeft) 
                    {
                        Xpos = (vBounds[0] - HorOffset);
                    }
                    else
                    {
                        Xpos = (vBounds[2] + HorOffset);
                    }
    
                    var myDisplayDim = swModel.AddVerticalDimension2(Xpos, Ypos, 0);
    
    
                }
    
    
            }
    
    
    
            /// <summary>
            /// 获取单位
            /// </summary>
            /// <param name="v"></param>
            /// <returns></returns>
            private double GetUnitConvFactor(string v)
            {
                var LenUnit = swModel.GetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swUnitsLinear);
                switch (LenUnit)
                {
                    case 0  : return 0.001;
                    case 1  : return 0.01;
                    case 2  : return 1;
                    case 3  : return 0.0254;
                    case 4  : return 0.3048;
                    case 5  : return -1;
                    case 6  : return 0.0000000001;
                    case 7  : return 0.000000001;
                    case 8  : return 0.000001;
                    case 9  : return 0.00254;
                    case 10  : return 0.00000254;
                    default:
                        return -1;
                    
                }         
    
    
            }
    
            /// <summary>
            /// 找极点
            /// </summary>
            /// <param name="axis"></param>
            /// <param name="MinMax"></param>
            /// <param name="swPcoll"></param>
            /// <param name="swViewType"></param>
            /// <param name="swViewXform"></param>
            /// <returns></returns>
            private Entity FindExtremun(int axis, string MinMax, List<Vertex> swPcoll, int swViewType, MathTransform swViewXform)
            {
                var swVertex = swPcoll[0];
                var vPt = PointCoordinates(swVertex, swViewType, swViewXform);
                var Extr = vPt[axis];
    
                for (int i = 0; i < swPcoll.Count; i++)
                {
                    vPt = PointCoordinates(swPcoll[i], swViewType, swViewXform);
    
                    if (MinMax=="max" && vPt[axis]>Extr)
                    {
                        Extr = vPt[axis];
                        swVertex = swPcoll[i];
    
                    }
                    else if (MinMax == "min" && vPt[axis] < Extr)
                    {
                        Extr = vPt[axis];
                        swVertex = swPcoll[i];
    
                    }
    
                }
                return swVertex as Entity;
    
          
            }
    
            /// <summary>
            /// 坐标系转换
            /// </summary>
            /// <param name="swVertex"></param>
            /// <param name="swViewType"></param>
            /// <param name="swViewXform"></param>
            /// <returns></returns>
            private double[] PointCoordinates(Vertex swVertex, int swViewType, MathTransform swViewXform)
            {
                var swMathUtils = swApp.IGetMathUtility();
                var vPt = (double[])swVertex.GetPoint();
                var swMathPt = (MathPoint)swMathUtils.CreatePoint(vPt);
    
                if (swViewType==(int)swDocumentTypes_e.swDocASSEMBLY)
                {
                    var swComp =(Component2) (swVertex as Entity).GetComponent();
    
                    if (swComp!=null)
                    {
                        MathTransform swCompXform = swComp.Transform2;
    
                        MathTransform swTotalXForm = (MathTransform)swCompXform.Multiply(swViewXform);
    
                         swMathPt = (MathPoint)swMathPt.MultiplyTransform(swTotalXForm);
    
                    }
    
                }
                else
                {
                    swMathPt = (MathPoint)swMathPt.MultiplyTransform(swViewXform);
                }
    
                return (double[])swMathPt.ArrayData;
    
    
            }
            /// <summary>
            /// 视图转换
            /// </summary>
            /// <param name="swDraw"></param>
            /// <param name="swView"></param>
            /// <returns></returns>
            private MathTransform ViewMathTransform(DrawingDoc swDraw, View swView)
            {
                try
                {
                    var swViewXform = (MathTransform)swView.ModelToViewTransform;
                    var swSheet = (Sheet)swDraw.GetCurrentSheet();
                    var swSheetView = (View)swDraw.GetFirstView();
                    var swSheetXform = (MathTransform)swSheetView.ModelToViewTransform;
    
                    var vSheetPrps = (double[])swSheet.GetProperties();
                    var scaleNom = vSheetPrps[2];
                    var scaleDenom = vSheetPrps[3];
                    var ViewMatrix = (Double[])swSheetXform.ArrayData;
                    ViewMatrix[12] = 1; //'scaleNom / scaleDenom
                    var swMathUtil = (MathUtility)swApp.GetMathUtility();
                    swSheetXform = (MathTransform)swMathUtil.CreateTransform(ViewMatrix);
                    return (MathTransform)swViewXform.Multiply(swSheetXform.Inverse());
    
                }
                catch (Exception)
                {
    
                    return null;
                }
            }
    
    
    
    
    
    
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432

    3.调用

    在这里插入图片描述

                var swApp = PStandAlone.GetSolidWorks();
    
                var swModel = (ModelDoc2)swApp.ActiveDoc;
    
                AddSizeDimensionForDrawing addSizeDimensionForDrawing = new AddSizeDimensionForDrawing(swApp, swModel);
                addSizeDimensionForDrawing.AutoAddSize(false,false);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述


    总结

    今天代码有点多,也仅仅是针对规则的体进行处理,要达到生产可用状态还是有很路要走的,这里就靠大家发挥了。
    比如: 圆如何处理,不规则体又如何处理,如何处理重复尺寸,如何进行尺寸位置的避让或者自动整理?这些问题待大家研究。

  • 相关阅读:
    汇编语言程序设计 --- 一元二次方程ax2+bx+c=0求解(含注释详细源代码)
    location对象详解
    【CSDN21天学习挑战赛】第一天,配置环境外加实现mnist手写数字识别
    小程序uView2.X框架upload组件上传方法总结+避坑
    62. 不同路径
    C++ STL进阶与补充(map/multimap容器)
    C#服务器NFS共享文件夹搭建与上传图片文件
    JavaScript实现计数排序
    维格云轮播组件入门教程
    Ansible自动化运维工具
  • 原文地址:https://blog.csdn.net/zengqh0314/article/details/125614528