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;
            }
        }
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章