C# SolidWorks 二次开发 API --- 实例:自增文本标注

相信大家在工程图中难免需要手动标注一些东西,而且这些东西需要按顺序变化。

如有时候需要按如下顺序标注:A1 A2 A3 A4

我们公司常用的标法还有A1 B1 A2 B2 A3 B3 这种.

而Solidworks自动的标注只能默认与上一次写的内容一致,这样总是需要手动编辑,容易出错,还效率低。

  1. 问题:如何快速的进行上述方式的标注.
  2. 思路:捕获Solidworks中鼠标事件,在点击图纸的时候按设定好的方式进行标注。 这里面涉及到一些事件的委托,以及工程图里面一的些API,其次就是图纸和模型的一些处理。
  3. 效果(之前做好的): ~

   关键代码:

   这是之前做好的,所以没有做什么注释,有空会补上。

   代码下载:https://gitee.com/painezeng/CSharpAndSolidWorks

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharpAndSolidWorks
{
    public class mouseClass
    {
        private FrmNote _frmNote;

        public mouseClass(FrmNote frmNote)
        {
            _frmNote = frmNote;
        }

        public int ms_MouseSelectNotify(int ix, int iy, double X, double Y, double Z)
        {
            try
            {
                Debug.Print("Mouse loc ix = " + ix + " iy = " + iy + " x= " + X);

                SldWorks swApp;
                swApp = Comm.ConnectToSolidWorks();

                bool boolstatus;
                long longstatus;

                ModelDoc2 swModel;
                swModel = swApp.ActiveDoc;

                Note myNote;
                Annotation myAnnotation;

                myNote = swModel.InsertNote(_frmNote.activeNote);
                if (myNote != null)
                {
                    myNote.Angle = 0;
                    boolstatus = myNote.SetBalloon(0, 0);
                    myAnnotation = myNote.GetAnnotation();
                    if (myAnnotation != null)
                    {
                        longstatus = myAnnotation.SetLeader3((int)swLeaderStyle_e.swSTRAIGHT, 0, true, false, false, false);

                        boolstatus = myAnnotation.SetPosition(X + FrmNote.x / 1000, Y + FrmNote.y / 1000, Z);

                        TextFormat txtFormat = default(TextFormat);

                        txtFormat = myAnnotation.GetTextFormat(0);

                        txtFormat.Bold = true;
                        txtFormat.CharHeight = 0.01;

                        boolstatus = myAnnotation.SetTextFormat(0, false, txtFormat);
                    }
                }

                swModel.ClearSelection2(true);
                swModel.WindowRedraw();

                _frmNote.NextNote();

                if (FrmNote.haveNextNote == false)
                {
                    FrmNote.TheMouse.MouseSelectNotify -= ms_MouseSelectNotify;
                }
                else
                {
                    Frame swFrame = swApp.Frame();

                    swFrame.SetStatusBarText("Next Click  to insert " + _frmNote.activeNote);
                }

                return 1;
            }
            catch (Exception)
            {
                MessageBox.Show("Error!");

                return 0;
            }
        }
    }
}
扫描二维码关注公众号,回复: 9372519 查看本文章
发布了51 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zengqh0314/article/details/103169921