C# SolidWorks 二次开发 API —批量修改工程图视角

C# SolidWorks 二次开发 API —批量修改工程图视角

这是前两周帮一个朋友做的一个小功能。仅仅是转换工程图中的视角选项。全部改为第一或者第三视角。
本来开始写好了exe,本地测试无问题。结果他们有加密软件,无法正常运行。又试了dll,还是不行。最后搞了个swp宏给他。
这真是要全能的节奏。
分析下功能:
要改视角,就是修改Soidworks中文档属性里面的一个选项。
然后为了实现批量,就是可以遍历文件夹下面的所有工程图,当然还要考虑工程图中有多个图纸的情况。但是好像文档属性定义了所有图纸的设置。我就没去细看了。

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200711084742103.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plbmdxaDAzMTQ=,size_16,color_FFFFFF,t_70)
  private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            //选择文件夹对话框
            FolderSelectDialog fsd = new FolderSelectDialog();
            fsd.Title = "选择需要调整的工程图所在路径:";
            fsd.Multiselect = false;

            if (fsd.ShowDialog())
            {
                string[] dirs = fsd.FileNames;
                txtDrawomgPath.Text = dirs[0];
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (radioFirst.Checked == false && radioThird.Checked == false)
            {
                MessageBox.Show("请先选择视角!");

                return;
            }

            string[] files = Directory.GetFiles(txtDrawomgPath.Text, "*.slddrw", SearchOption.AllDirectories);

            SldWorks swApp = PStandAlone.GetSolidWorks();

            if (swApp == null)
            {
                swApp = PStandAlone.RunSolidWorks(-1, true, false, false, 30, 15);
            }

            swApp.CommandInProgress = true;
            progressBar1.Maximum = files.Length;

            int nowFileCount = 0;

            foreach (string file in files)
            {
                progressBar1.Value = nowFileCount;

                Debug.Print(file);

                swApp.OpenDoc(file, (int)swDocumentTypes_e.swDocDRAWING);

                ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;

                DrawingDoc drawingDoc = default(DrawingDoc);

                Sheet swSheet = default(Sheet);
                double[] sheetProperties = null;

                string activedSheetName = "";

                drawingDoc = (DrawingDoc)swApp.ActiveDoc;

                if (drawingDoc != null)
                {
                    activedSheetName = ((Sheet)drawingDoc.GetCurrentSheet()).GetName();
                    var sheetNames = (string[])drawingDoc.GetSheetNames();
                    for (int i = 0; i < sheetNames.Length; i++)
                    {
                        //var sheet = drawingDoc.Sheet[sheetNames[i]];

                        drawingDoc.ActivateSheet(sheetNames[i]);
                        // Active sheet is Sheet2
                        swSheet = (Sheet)drawingDoc.GetCurrentSheet();
                        sheetProperties = (double[])swSheet.GetProperties2();
                        int prop1;
                        int prop2;
                        bool prop5;
                        bool prop8;
                        prop1 = (int)System.Convert.ToInt32(sheetProperties[0]);
                        prop2 = (int)System.Convert.ToInt32(sheetProperties[1]);

                        if (radioFirst.Checked == true)
                        {
                            prop5 = true;
                        }
                        else
                        {
                            prop5 = false;
                        }

                        //prop5 = (bool)System.Convert.ToBoolean(sheetProperties[4]);

                        prop8 = (bool)System.Convert.ToBoolean(sheetProperties[7]);
                        prop8 = true;

                        swSheet.SetProperties2(prop1, prop2, sheetProperties[2], sheetProperties[3], prop5, sheetProperties[5], sheetProperties[6], prop8);

                        swModel.EditRebuild3();
                    }
                }
                drawingDoc.ActivateSheet(activedSheetName);
                swModel.EditRebuild3();
                swModel.Save();

                swApp.CloseDoc(file);
                nowFileCount = nowFileCount + 1;
            }

            swApp.CommandInProgress = false;
            progressBar1.Value = progressBar1.Maximum;

            swApp.SendMsgToUser("视角转换完成!");
        }

宏的链接:
https://download.csdn.net/download/zengqh0314/12600462

posted @
2023-02-10 07:30 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源