• SolidWorks二次开发 API-SOLIDWORKS Simulation分析参数修改


    今天我们来讲个小例子。
    是关于SOLIDWORKS Simulation的。
    先说明一点,这东西我也不熟。有问题别问我
    在这里插入图片描述
    首先,我做了一个很难的分析,条件也是很复杂,具体操作我就不说了,分析结果如下:
    在这里插入图片描述
    当然这个图和我们今天要做的事情 关系不大,我们主要了解 一下如何连接到SOLIDWORKS Simulation 上并修改载荷参数,重新运行分析,得到新的结果 。
    具体的api查找过滤和之前看帮助类似,我就直接上图了:
    在这里插入图片描述

    下面是关键代码:
    把上面的力的大小从100 改成150,然后重新进行风格的划分,运行计算。
    `
    private void btnSimStudy_Click(object sender, EventArgs e)
    {
    var actPath = RegDllPath(“”);

            var start = actPath.Substring(0,actPath.IndexOf("CSharpAndSolidWorks",0));
                    
    
            var partPath = $@"{start}CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\Simulation API Demo.SLDPRT";
    
            SldWorks swApp = PStandAlone.GetSolidWorks();
    
        
            CWModelDoc swsActDoc = default(CWModelDoc);
            CWStudyManager swsStudyMngr = default(CWStudyManager);
            CWStudy swsStudy = default(CWStudy);
            CWLoadsAndRestraintsManager swsLBCMgr = default(CWLoadsAndRestraintsManager);
            CWForce swsCWForce = default(CWForce); 
    
            int errors = 0;
            int warnings = 0;
       
    
            string fileName = partPath;
    
            // 打开模型
            var swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
    
            // Get the SOLIDWORKS Simulation object
            dynamic COSMOSWORKS = default(dynamic);
            dynamic COSMOSObject = default(dynamic);
    
            // Determine host SOLIDWORKS major version
            int swVersion = Convert.ToInt32(swApp.RevisionNumber().Substring(0, 2));
    
            // Calculate the version-specific ProgID of the Simulation add-in that is compatible with this version of SOLIDWORKS
            int cwVersion = swVersion - 15;
            String cwProgID = String.Format("SldWorks.Simulation.{0}", cwVersion);
            Debug.Print(cwProgID);
    
            // Get the SOLIDWORKS Simulation object
            COSMOSObject = swApp.GetAddInObject(cwProgID);
    
            COSMOSWORKS = COSMOSObject.CosmosWorks;
    
            // Open and get active document
            swsActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
    
            if (swsActDoc == null) ErrorMsg(swApp, "No active document");
    
            // Create new static study
            swsStudyMngr = (CWStudyManager)swsActDoc.StudyManager;
            if (swsStudyMngr == null) ErrorMsg(swApp, "No CWStudyManager object");
    
            //得到第一个算例对象
            swsStudy = (CWStudy)swsStudyMngr.GetStudy(0);
    
            //算例名称,可以区分多个算例   swsStudy.Name
    
    
    
            if (swsStudy == null) ErrorMsg(swApp, "No CWStudy object");
    
            swsLBCMgr = (CWLoadsAndRestraintsManager)swsStudy.LoadsAndRestraintsManager;
    
            //get the Force Feature
            //这是是第一个条件,所以参数写0
            var sCwForce = swsLBCMgr.GetLoadsAndRestraints(0, out int errcode);
    
            if (sCwForce != null)
            {
                var swForce = (CWForce)sCwForce;
            
                swForce.ForceBeginEdit();
    
                
    
                //修改值为150
                swForce.NormalForceOrTorqueValue = 150;  
                var res = swForce.ForceEndEdit();
    
            }
    
            //Create mesh
            var CwMesh = (CWMesh)swsStudy.Mesh;
    
            if (CwMesh == null) ErrorMsg(swApp, "No mesh object");
    
            CwMesh.Quality = 1;
            CwMesh.GetDefaultElementSizeAndTolerance(0, out double el, out double tl);
            var errCode = swsStudy.CreateMesh(0, el, tl);
            if (errCode != 0) ErrorMsg(swApp, "Mesh failed");
    
            //Run
            swsStudy.RunAnalysis();
        }
    
        public void ErrorMsg(object swApp, string Message)
        {
            MessageBox.Show(Message);
            MessageBox.Show("'*** WARNING - General");
            MessageBox.Show("'*** " + Message);
            MessageBox.Show("");
        }
    
    • 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

    `在这里插入图片描述
    这样结果出来了,至于怎么拿到最新的结果 ,或者说截图的话后面有机会再讲了。
    源码位置:
    https://gitee.com/painezeng/CSharpAndSolidWorks

  • 相关阅读:
    远程调试 idea配置remote debug、在远程服务器的程序中,添加JVM启动参数-Xdebug
    Angular:升级Angular 13到Angular 14
    Python 潮流周刊#21:如何提升及测量 Python 代码的性能?
    Springboot中使用@JsonProperty和@JSONField
    JS选项卡
    vue3 项目搭建流程
    选项卡控件的封装
    快来看看用FPGA做的开源示波器(二)
    java毕业设计海滨体育馆管理系统(附源码、数据库)
    CGAL 点云数据生成DSM、DTM、等高线和数据分类
  • 原文地址:https://blog.csdn.net/zengqh0314/article/details/128037914