C# SolidWorks 二次开发 API —dll插件如何让 winform 类似ShowDialog,但还能操作solidworks

C# SolidWorks 二次开发 API —dll插件如何让 winform 类似ShowDialog,但还能操作solidworks

这篇文章记录一下,这次看api帮助解决到的一个问题:

          由于之前我都是先做好的exe独立开发,后来改成插件形式后遇到的问题。以前经常利用窗体的ShowDialog特性,让程序暂停,让用户进行对象的自定义选择操作,但是到了dll中出现了问题,因为dll和solidworks主进程是一个,所以当用了页面的ShowDialog之后,solidworks就再也无法操作了。这个问题之前花了一天多时间都没搞定,想了好多办法,最后利用的doevents解决的,但是效果不是特别好。

         原来solidworks的帮助文件中已经有了解决方案: 只需要使用 Application.Run(winform)就可以了,我刚才进行了测试,很好用,至于还有没有别的问题,后面再测试一下。

        

         参考链接:

http://help.solidworks.com/2018/English/api/sldworksapiprogguide/overview/Keystrokes_and_Accelerator_Keys_and_ActiveX_Modeless_Dialogs_and_PropertyManager_Pages.htm

posted @
2020-02-22 18:12 
painezeng  阅读(
131)  评论(
0
编辑 
收藏 
举报

C# SolidWorks 二次开发 API — 2018版 中文-完整版共享

C# SolidWorks 二次开发 API — 2018版 中文-完整版共享

由于还在湖北老家,暂时无法回去工作,休息期间我又把2018的api帮助文档看了一下,我把之前翻译的文件免费共享下,希望能对大家有所帮助。

https://download.csdn.net/download/zengqh0314/12170828

posted @
2020-02-19 12:56 
painezeng  阅读(
208)  评论(
0
编辑 
收藏 
举报

C# SolidWorks 二次开发 API — 实例:随机上色

C# SolidWorks 二次开发 API — 实例:随机上色

随机上色这个功能做起来不算复杂,但是要想做的比较完美也不简单。

这一篇文章我就只做随机给装配体中的零件上色。

上色前:

 上色后:

 

简单的代码:

private void btn_setcolor_Click(object sender, EventArgs e)
        {
            ISldWorks swApp = Utility.ConnectToSolidWorks();

            if (swApp != null)
            {
                ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;

                Configuration swConf = swModel.GetActiveConfiguration();

                Component2 swRootComp = swConf.GetRootComponent();

                //遍历

                Utility.TraverseCompXform(swRootComp, 0, true);

                swModel.WindowRedraw();

                swModel.EditRebuild3();
            }
        }



以下是上色代码:
 //如果要设定颜色
                    if (setcolor == true)
                    {
                        double[] matPropVals = swModel.MaterialPropertyValues;
                        var tempC = GetRadomColor(System.IO.Path.GetFileNameWithoutExtension(swModel.GetPathName()));
                        matPropVals[0] = Convert.ToDouble(tempC.R) / 255;
                        matPropVals[1] = Convert.ToDouble(tempC.G) / 255;
                        matPropVals[2] = Convert.ToDouble(tempC.B) / 255;
                        swModel.MaterialPropertyValues = matPropVals;

                        swModel.WindowRedraw();
                    }


 public static System.Drawing.Color GetRadomColor(string name)
        {
            Random rnd = new Random();

            //这里可以根据需要指定颜色。
            if (name.Contains("m1"))
            {
                return System.Drawing.Color.Red;
            }

            return System.Drawing.Color.FromArgb(
                 rnd.Next(0, 255), /*红色*/
                 rnd.Next(0, 255), /*绿色*/
                 rnd.Next(0, 255)  /*蓝色*/ );
        }

具体代码见github或者码云,链接可以在之前的博文中找到。

posted @
2020-01-14 15:58 
painezeng  阅读(
227)  评论(
0
编辑 
收藏 
举报

C# SolidWorks 二次开发 API — 插入图块和属性块

C# SolidWorks 二次开发 API — 插入图块和属性块

在还没有取消所有2D图纸时,我们难免需要在图纸中插入一些标准图块,或者像AutoCAD那个的属性块。

 

如下图: 

 

 

直接上代码:

private void btn_Insert_Block_Click(object sender, EventArgs e)
        {
            //连接到Solidworks
            ISldWorks swApp = Utility.ConnectToSolidWorks();

            ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;

            DrawingDoc dc = (DrawingDoc)swModel;

            // SelectionMgr selectionMgr = (SelectionMgr)swModel.SelectionManager;

            double[] nPt = new double[3];

            nPt[0] = 0;
            nPt[1] = 0;
            nPt[2] = 0;

            MathUtility swMathUtil = swApp.GetMathUtility();

            MathPoint swMathPoint = swMathUtil.CreatePoint(nPt);

            double blockScale = 1;

            string blockPath = @"D:\09_Study\CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\TestBlock.SLDBLK";

            //插入图块
            var swBlockInst = InsertBlockReturnInst(swApp, swModel, swMathPoint, blockPath, blockScale);

            //  swModel.SketchManager.MakeSketchBlockFromFile(mathPoint, blockPath, false, blockScale, 0);

            //修改块的属性(如果只是普通的图块,则无需要这一步。直接使用上面的一行插入图块即可)
            swBlockInst.SetAttributeValue("Title1", "Paine");
        }

        /// <summary>
        /// 插入并返回最后一个图块实例
        /// </summary>
        /// <param name="sldWorks"></param>
        /// <param name="modelDoc2"></param>
        /// <param name="mathPoint"></param>
        /// <param name="blockPath"></param>
        /// <param name="blockScale"></param>
        /// <returns></returns>
        private SketchBlockInstance InsertBlockReturnInst(ISldWorks sldWorks, ModelDoc2 modelDoc2, MathPoint mathPoint, String blockPath, double blockScale)
        {
            SketchBlockInstance swBlockInst;
            List<String> NowBlockName = new List<String>();
            var swModel = modelDoc2;
            Boolean boolstatus = swModel.Extension.SelectByID2(System.IO.Path.GetFileNameWithoutExtension(blockPath), "SUBSKETCHDEF", 0, 0, 0, false, 0, null, 0);

            if (boolstatus == true)
            {
                Feature swFeat = swModel.SelectionManager.GetSelectedObject6(1, 0);
                var swSketchBlockDef = swFeat.GetSpecificFeature2();

                var nbrBlockInst = swSketchBlockDef.GetInstanceCount;

                if (nbrBlockInst > 0)
                {
                    var vBlockInst = swSketchBlockDef.GetInstances();

                    for (int i = 0; i < nbrBlockInst; i++)
                    {
                        swBlockInst = vBlockInst[i];

                        NowBlockName.Add(swBlockInst.Name.ToString());
                    }

                    swModel.SketchManager.MakeSketchBlockFromFile(mathPoint, blockPath, false, blockScale, 0);

                    swModel.ClearSelection2(true);

                    boolstatus = swModel.Extension.SelectByID2(System.IO.Path.GetFileNameWithoutExtension(blockPath), "SUBSKETCHDEF", 0, 0, 0, false, 0, null, 0);

                    swFeat = swModel.SelectionManager.GetSelectedObject6(1, 0);
                    swSketchBlockDef = swFeat.GetSpecificFeature2();

                    nbrBlockInst = swSketchBlockDef.GetInstanceCount;

                    if (nbrBlockInst > 0)
                    {
                        vBlockInst = swSketchBlockDef.GetInstances();

                        for (int j = 0; j < nbrBlockInst; j++)
                        {
                            swBlockInst = vBlockInst[j];
                            if (!NowBlockName.Contains(swBlockInst.Name.ToString()))
                            {
                                swModel.Extension.SelectByID2(swBlockInst.Name, "SUBSKETCHINST", 0, 0, 0, false, 0, null, 0);
                                swBlockInst = GetSketchBlockInstanceFromSelection();
                                return swBlockInst;
                            }
                        }
                    }
                    return null;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                var swSketchBlockDef = swModel.SketchManager.MakeSketchBlockFromFile(mathPoint, blockPath, false, blockScale, 0);
                swBlockInst = swSketchBlockDef.GetInstances()[0];

                return swBlockInst;
            }
        }

        private SketchBlockInstance GetSketchBlockInstanceFromSelection()
        {
            ISldWorks swApp = Utility.ConnectToSolidWorks();

            ModelDoc2 swModel;
            ModelDocExtension swModelDocExt;
            SketchBlockInstance SketchBlockInstance;

            DateTime time = DateTime.Now;

            try
            {
                swModel = swApp.ActiveDoc;
                swModelDocExt = swModel.Extension;

                SelectionMgr swSelectionMgr;
                swSelectionMgr = swModel.SelectionManager;

                string SelectByString = "";
                string ObjectType = "";
                int type;
                double x;
                double y;
                double z;

                if (swSelectionMgr.GetSelectedObjectCount2(-1) > 1)
                {
                    // Return only a SketchblockInstance when only one is selected...
                    // modify if you want return more than one (or only the first) selected Sketchblockinstance
                    return null;
                }

                swSelectionMgr.GetSelectionSpecification(1, out SelectByString, out ObjectType, out type, out x, out y, out z);
                Debug.WriteLine(SelectByString + " " + ObjectType + " " + type);

                if (type == (int)swSelectType_e.swSelSUBSKETCHINST)
                {
                    SketchBlockInstance = swSelectionMgr.GetSelectedObject6(1, -1);
                    Debug.WriteLine("Found:" + SketchBlockInstance.Name);
                    return SketchBlockInstance;
                }
                else if (type == (int)swSelectType_e.swSelSKETCHSEGS | type == (int)swSelectType_e.swSelSKETCHPOINTS)
                {
                    // Show if a sketchblockinstance has the same name
                    SketchManager SwSketchMgr;
                    SwSketchMgr = swModel.SketchManager;

                    object[] blockDefinitions = (object[])SwSketchMgr.GetSketchBlockDefinitions();
                    foreach (SketchBlockDefinition blockDef in blockDefinitions)
                    {
                        foreach (SketchBlockInstance blockInstance in blockDef.GetInstances())
                        {
                            if (SelectByString.EndsWith(blockInstance.Name))
                            {
                                Debug.WriteLine("Found:" + blockInstance.Name);
                                return blockInstance;
                            }
                        }
                    }
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                Debug.WriteLine(DateTime.Now.Subtract(time).Milliseconds);
            }

            return null;
        }
  

 

posted @
2020-01-06 13:30 
painezeng  阅读(
225)  评论(
0
编辑 
收藏 
举报

C# SolidWorks 二次开发 API — 把零件中的坐标点转换到总装配中

C# SolidWorks 二次开发 API — 把零件中的坐标点转换到总装配中

今天我们来看下solidworks中的坐标矩阵转换,这个例子是把子零件中的一个基准轴的两个端点读出来,并转换到总装配的坐标系中,得到在总装配体坐标系的位置,可以进一步判断轴的真实安装方向。

直接上代码:

 private void btn_Transform_PartToAsm_Click(object sender, EventArgs e)
        {
            //连接到Solidworks

            //这个例子是把零件中的一个基准轴 的两个点的坐标转换到装配体中

            //请打开装配体,并在某个零件下选择一下基准轴

            ISldWorks swApp = Utility.ConnectToSolidWorks();

            ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;

            SelectionMgr swSelMgr = swModel.ISelectionManager;

            Feature swFeat = (Feature)swSelMgr.GetSelectedObject6(1, 0);

            String sAxisName = swFeat.Name;

            RefAxis RefAxis = swFeat.GetSpecificFeature2();

            var vParam = RefAxis.GetRefAxisParams();

            Component2 inletPart = swSelMgr.GetSelectedObjectsComponent4(1, 0);

            double[] nPt = new double[3];
            double[] nPt2 = new double[3];

            object vPt;
            object vPt2;

            nPt[0] = vParam[0]; nPt[1] = vParam[1]; nPt[2] = vParam[2];
            nPt2[0] = vParam[3]; nPt2[1] = vParam[4]; nPt2[2] = vParam[5];

            vPt = nPt;
            vPt2 = nPt2;

            MathUtility swMathUtil = (MathUtility)swApp.GetMathUtility();

            MathTransform mathTransform = inletPart.Transform2;

            MathTransform swXform = (MathTransform)mathTransform;

            MathPoint swMathPt = (MathPoint)swMathUtil.CreatePoint((vPt));

            MathPoint swMathPt2 = (MathPoint)swMathUtil.CreatePoint((vPt2));

            //swXform.Inverse(); 反转的话就是把装配体中的点坐标转到零件对应的坐标系统中

            swMathPt = (MathPoint)swMathPt.MultiplyTransform(swXform);

            swMathPt2 = (MathPoint)swMathPt2.MultiplyTransform(swXform);

            var x = swMathPt.ArrayData[0];
            var y = swMathPt.ArrayData[1];
            var z = swMathPt.ArrayData[2];
            var x2 = swMathPt2.ArrayData[0];
            var y2 = swMathPt2.ArrayData[1];
            var z2 = swMathPt2.ArrayData[2];

            var v1 = x2 - x;
            var v2 = y2 - y;
            var v3 = z2 - z;

            if (Math.Round(v3, 4) != 0 && Math.Round(v1, 4) == 0 && Math.Round(v2, 4) == 0)
            {
                MessageBox.Show("此轴在Z方向上");
            }

          
        }

比较全的关于矩阵转换的一篇文章:

https://cadbooster.com/complete-overview-of-matrix-transformations-in-the-solidworks-api/#available-transformations-solidworks

 

 

posted @
2019-12-31 10:12 
painezeng  阅读(
233)  评论(
0
编辑 
收藏 
举报