SolidWorks二次开发—简单的参数化示例

SolidWorks二次开发—简单的参数化示例

参数化设计? 我们看下百度的解释

哇,好像没几个字,你会了吗?

我用小白的口气解释一下: 就像我有个生产苹果的机器 ,你想要苹果的时候告诉我你想要多大的苹果就行。 这里面苹果的大小就是一个参数。小朋友 可能 只要直径10cm的苹果就能吃饱,但是大人就得要直径20cm的苹果。所以这个生产苹果的机器 就只需要这一个参数就行了。
这就参数化设计,元芳,你怎么看?
在SolidWorks中做参数化开发有不同的方式,大多都采用先建立好一套初始模型,然后通过程序 修改参数来得到新的模型,从而完成设计。
当然也有些是直接采用api接口直接进行绘制的,这种就相对来说比较复杂,不适合大量的操作。

下面我们就用一个简单的例子,来实战一下:

我们先弄一个简单的界面,里面有一些参数:
其实就是一个零件上有很多个小孔, 通过些小孔控制整个零件的尺寸,来设计零件。

下面是主要的代码(Vb.net版本):

Function ModifyActionModel() As Boolean
        Try
            Dim swModel As ModelDoc2
            Dim swEqnMgr As EquationMgr

            Dim eqName As String

            swApp = GetObject(, "SldWorks.Application")
            swModel = swApp.ActiveDoc

            If swModel Is Nothing Then

                MsgBox("当前模型为空。请打开后重试")

            End If
			
			'通过这个属性来检查零件是否能通过工具修改
            Dim ActionPartCode = swModel.CustomInfo2("", "CodeID")
            If ActionPartCode = "B0000001" Then


            Else

                MsgBox("当前模型不是工具生成的零件,所以无法修改。")

                Return False

            End If
			
            swEqnMgr = swModel.GetEquationMgr
            ProgressStatus.Value = 40
            Dim str1 As String = """"
			'通过修改方程式来控制模型
            For i = 0 To swEqnMgr.GetCount - 1

                eqName = Mid(swEqnMgr.Equation(i), 1, (InStrRev(swEqnMgr.Equation(i), "=")) - 1)

                If InStr(eqName, Chr(34) & "circle_R" & Chr(34)) Then swEqnMgr.Equation(i) = """circle_R""= " & Val(txtR.Text)
                If InStr(eqName, Chr(34) & "number_X" & Chr(34)) Then swEqnMgr.Equation(i) = """number_X""= " & Val(cobXQty.Text)
                If InStr(eqName, Chr(34) & "number_Y" & Chr(34)) Then swEqnMgr.Equation(i) = """number_Y""= " & Val(cobYQty.Text)
                If InStr(eqName, Chr(34) & "circle_x" & Chr(34)) Then swEqnMgr.Equation(i) = """circle_x""= " & Val(txtLeft.Text)
                If InStr(eqName, Chr(34) & "circle_Y" & Chr(34)) Then swEqnMgr.Equation(i) = """circle_Y""= " & Val(txtRight.Text)
                If InStr(eqName, Chr(34) & "box_Height" & Chr(34)) Then swEqnMgr.Equation(i) = """box_Height""= " & Val(txtHeight.Text)
                If InStr(eqName, Chr(34) & "circle_Dim" & Chr(34)) Then swEqnMgr.Equation(i) = """circle_Dim""= " & Val(Double.Parse(txtR.Text) + Double.Parse(txtGap.Text))

            Next
            ProgressStatus.Value = 80
            Dim ret As Integer

            ret = swEqnMgr.EvaluateAll
            ret = swModel.ForceRebuild3(True) 'Rebuid model
            ProgressStatus.Value = 100
            Return True
        Catch ex As Exception
            Return False

        End Try

    End Function

这样就完成了设计,至于工具编程涉及的功能我之前 的博文中都有写到。
比如修改尺寸,属性 ,方程式,压缩特征等等。。。

posted @
2021-08-10 16:59 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源

SolidWorks二次开发官方教程-API Fundamentals学习分享

SolidWorks二次开发官方教程-API Fundamentals学习分享

今天618,送大礼!
间断的花了快两个月时间,终于把官方的API基础知识学习完了。
下面是链接和截图:

API Fundamentals https://my.solidworks.com/training/elearning/68/api-fundamentals
这个访问需要正版Solidworks订阅用户。

文章目录

课程目录:

  1. Course Modules
  2. eCourse Overview
  3. Course Introduction
  4. Using the Macro Recorder (1 of 2)
  5. Using the Macro Recorder (2 of 2)
  6. Understanding the API Object Model (1 of 2)
  7. Understanding the API Object Model (2 of 2)
  8. Setting System Options and Document Properties
  9. Automating Part Design
  10. Automating Assembly Design
  11. Automating Drawing Development
  12. Using Selection and Traversal Techniques
  13. Adding Custom Properties and Attributes
  14. Installing and Using the SOLIDWORKS API SDK (1 of 2)
  15. Installing and Using the SOLIDWORKS API SDK (2 of 2)
  16. Customizing the SOLIDWORKS User Interface (1 of 2)
  17. Customizing the SOLIDWORKS User Interface (2 of 2)
  18. Understanding Notifications

我的学习PPT记录 截图:



官方提供的代码与练习资料:
我把我的学习记录与资料放在了新的开源库里:

下载地址:

https://gitee.com/painezeng/api-fundamentals

里面包括文字,教材与 对应的基本上每页的PPT。

大家可以去这里拿。

学习总结:

这是官方提供的原生的视频教程,市面上的官方教程也是参考这个来编写的。
这里面基本上是从零基础讲解的,很多基础知识都有涉及,包括api帮助的使用,宏特征,对象关系 等等。
当然也是全英文的,里面有一些跟着视频动手操作的交互过程,用来加深印象。
通过这一次系统的学习官方的API基础知识,我也是收获了不少。

posted @
2021-06-18 08:42 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源

SolidWorks二次开发-Z轴直线全部倒圆角

SolidWorks二次开发-Z轴直线全部倒圆角

做机械加工的小伙伴肯定都知道,像如下这个图加工的时候,我们肯定要倒些圆角 。
今天我们来做一个小功能,自动倒圆角 。

文章目录

思路:

	通过遍历一边,根据边的方向来判断是否需要倒圆角。 这个示例我们就要以根据边线的方向是沿Z轴的来判断这条边是要倒圆角的。

代码:

//遍历Z轴方向的直线
            SldWorks swApp = PStandAlone.GetSolidWorks();//连接solidworks
            swApp.CommandInProgress = true; //因为是exe测试,所以启动该选项,加快速度
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            SelectionMgr swSelMgr = default(SelectionMgr);
            Feature swFeature = null;

            swModel = (ModelDoc2)swApp.ActiveDoc; //获取当前零件

            swModelDocExt = (ModelDocExtension)swModel.Extension;

            //选择所有,零件中是选中所有的边。 swModelDocExt.SelectAll();

            swSelMgr = (SelectionMgr)swModel.SelectionManager;

            var edgeCount = swSelMgr.GetSelectedObjectCount();  //获取 已经选中的边数

            Debug.Print("总边数:" + edgeCount);

            int faceidStart = 10000;  //设定一个面的起始id,用于识别该面是否已经被获取到。

            //List<Face2> face2sList001 = new List<Face2>();
            //List<Face2> face2sList002 = new List<Face2>();

            List<Edge> ZEdges = new List<Edge>();

            for (int i = 1; i <= edgeCount; i++)
            {
                var thisEdge = (Edge)swSelMgr.GetSelectedObject(i);

                var swCurve = (Curve)thisEdge.GetCurve();

                var thisCurveP = thisEdge.GetCurveParams3();

                if (swCurve.IsLine() == true)
                {
                    var lineV = (double[])swCurve.LineParams;

                    Debug.Print($"Root Point-> X {lineV[0].ToString()} ,Y {lineV[1].ToString()} ,Z {lineV[2].ToString()}");
                    Debug.Print($"Direction Point-> X {lineV[3].ToString()} ,Y {lineV[4].ToString()} ,Z {lineV[5].ToString()}");

                    if (lineV[3] == 0 && lineV[4] == 0)
                    {
                        ZEdges.Add(thisEdge);
                    }
                }
            }

            swModel.ClearSelection();//清除掉所有选择的边

            // 重新选中 需要处理掉0.001的面
            for (int i = 0; i < ZEdges.Count; i++)
            {
                var faceEntity = (Entity)ZEdges[i];

                // faceEntity.Select4(true, selectData);

                faceEntity.SelectByMark(true, 1);
            }
            double AsyRadius1;
            double AsyRadius2;
            double AsyRadius3;
            double AsyRadius4;
            bool boolstatus;
            double[] radiis = new double[2];
            object radiiArray0;
            object conicRhosArray0;
            object setBackArray0;
            object pointArray0;
            object pointRhoArray0;
            object dist2Array0;
            object pointDist2Array0;

            conicRhosArray0 = 0;
            setBackArray0 = 0;
            pointArray0 = 0;
            pointRhoArray0 = 0;
            pointDist2Array0 = 0;

            var FilletFea = (Feature)swModel.FeatureManager.FeatureFillet3(195, 0.002, 0.010, 0, (int)swFeatureFilletType_e.swFeatureFilletType_Simple, (int)swFilletOverFlowType_e.swFilletOverFlowType_Default, (int)swFeatureFilletProfileType_e.swFeatureFilletCircular, 0, 0, 0,
            (setBackArray0), (pointArray0), (pointDist2Array0), (pointRhoArray0));
            FilletFea.Name = "AutoFillet";

结果:

小结:

这只是一个简单的功能,实际情况肯定是要复杂 很多,大家可以思考 一下,比如内边和外边如何判断?  

posted @
2021-06-11 10:36 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源

SolidWorks PDM二次开发—学习线路

SolidWorks PDM二次开发—学习线路

最近比较忙,所以博客的频率更新不太高。
Solidworks二次开发入门基本结束了,后面有新的知识点我还会持续更新的。

下面这个图列了个Solidworks PDM Professional(Solidworks EPDM)的二次开发大纲,后面有机会慢慢带大家入门。


SolidWorks PDM 二次开发

前提

  • 编程语言基础:C# / VB.net /VBA 基础,以及面向对象编程
  • 窗体应用程序/控制台/DLL
  • 方法/属性/事件/类/调试/集合…
  • SolidWorks PDM Professional 客户端
  • 本机管理员帐号
  • SolidWorks PDM管理员工具权限
  • Visual Studio 2017或以上(Community即可)
  • SolidWorks 使用基础
  • MSSQL查询

基础

  • PDM对象结构介绍

  • 学习方法
    API 帮助
    官方论坛

  • 开发方法
    Dispatch
    EXE
    DLL

  • Dispatch示例

  • 以序列号修改文件名

  • 与PDM系统连接
    输入用户名,密码,以及登陆库名称
    自动登陆-> 已经登陆的直接获取,未登陆的弹窗登陆

  • 遍历本地库名称

  • 获取文件的信息
    根目录与文件夹
    名称,版本 状态。。。
    通f过路径获取文件对象
    通过搜索获取文件

  • 遍历文件夹中的文件

  • 检入 检出 取消检出 文件

  • 变量

     	遍历变量名
     	文件卡变量
     		读取
     		修改
    
  • 插件的创建

  • 插件的调试

  • 增加菜单

     	右键调用
     	文件卡中调用
    
  • 读取文件引用

  • 获取 装配体中的 BOM 表

  • 工程图中的材料明细表

  • 批量操作
    检出
    写属性

  • 字典的使用

  • 事件
    状态
    预提交
    流程

  • 文件链接

  • 获取文件最新版本

  • 缓存

  • 从数据库获取变量值

  • 利用代码增加变量

  • 获取指定版本的变量值

posted @
2021-06-01 08:36 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源

SolidWorks二次开发—特定坐标系输出

SolidWorks二次开发—特定坐标系输出

这个功能比较简单,我就直接上代码了。


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

            ModelDoc2 swModel = (ModelDoc2)swApp.ActiveDoc;

            //设置用来导出文件的坐标系名称(需要装配体中有这个名称的坐标系)
            var setRes = swModel.Extension.SetUserPreferenceString(16, 0, "Coordinate System1");

            //设置导出版本

            int error = 0;

            int warnings = 0;
            //x_t的版本设置
            //swApp.SetUserPreferenceIntegerValue((int)swUserPreferenceIntegerValue_e.swParasolidOutputVersion, (int)swParasolidOutputVersion_e.swParasolidOutputVersion_161);

            swModel.Extension.SaveAs(@"D:\export.igs", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref error, ref warnings);
            swModel.Extension.SaveAs(@"D:\export.x_t", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref error, ref warnings);
            //Mark:SolidWorks 2018版本导出 step 用坐标系导出有bug!!! x_t igs没有问题
            //swModel.Extension.SaveAs(@"D:\export.step", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_Silent, null, ref error, ref warnings);

            MessageBox.Show("输出成功");
        }

导出完成后用Solidworks打开。

去掉这个选项,检查一下:

posted @
2021-05-10 09:48 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源

SolidWorks二次开发-关于录制宏保存的格式

SolidWorks二次开发-关于录制宏保存的格式

对于录制宏操作之后,之前的版本是可以直接另存了Vb.net 或者C# 的格式的文件的,虽然它只是VSTA的项目,但是语法基本是一样的。

刚接触SolidWorks二次开的小伙伴可能只知道swp这个原始格式。

而且Solidworks中还专门有个选项,如2018中

并且很多电脑上宏录制完成之后 ,根本就没有其它格式,只有swp.

这是为什么呢? 我们今天来看看官方的说明,打开API 帮助文件。

搜索一下VSTA这个关键字。

Prerequisite Software 
Installing VSTA 
Creating a .NET macro in SOLIDWORKS 2018 
Editing a .NET macro created in SOLIDWORKS 2018 
Upgrading a .NET macro created in SOLIDWORKS 2017 or earlier 
Running a .NET DLL created in SOLIDWORKS 2017 or earlier 
Prerequisite Software
Microsoft Visual Studio 2015 (Community, Professional, Premium or Enterprise version) must be installed in order to record, edit, or debug VB.NET and C# macros using VSTA (3.0) 2015 in SOLIDWORKS 2018. 
Notes: 

If you install a version of Microsoft Visual Studio that is later than 2015, you must ensure that a runtime version of Microsoft Visual Studio 2015 is also installed. 
If you do not install Microsoft Visual Studio 2015 before you install SOLIDWORKS 2018, you will not be able to record, edit, or debug VSTA 3.0 macros. You will only be able to run VSTA 3.0 DLLs. 
If you install Microsoft Visual Studio 2015 after you install SOLIDWORKS 2018, you must also install the VSTA runtime to record, edit, or debug VSTA 3.0 macros. 
Installing VSTA
SOLIDWORKS 2017 and earlier used VSTA 1.0 to create, edit, and run .NET macros. SOLIDWORKS 2018 supports both VSTA 1.0 and VSTA (3.0) 2015. SOLIDWORKS 2018 activates VSTA (3.0) 2015 by default on all Windows versions. If VSTA 3.0 is enabled, .NET macros created in previous releases are converted when they are edited in this release. If VSTA 3.0 is enabled, be sure to back up your VSTA 1.0 macros before editing them in SOLIDWORKS 2018. If you do not want to convert your .NET macros, you can still edit and run VSTA 1.0 macros in this release, provided you choose to install VSTA 1.0 during the SOLIDWORKS installation and then de-select Tools > Options > System Options > General > Enable VSTA VERSION 3.0.

If you need to edit or run VSTA 1.0 macros on:

Windows 8 or later machines:

During SOLIDWORKS 2018 installation, click Change above the Products window on the Summary screen. 
Expand SOLIDWORKS in the Product Selection window. 
Select Visual Studio Tools for Applications (VSTA). 
On the Summary screen, click Install Now. 
During installation, a message box appears with "An app on your PC needs the following Windows feature: .NET Framework 3.5 (includes .NET 2.0 and 3.0)." Click Download and install this feature. If you do not select to download and install this feature, then VSTA 1.0 will not work. 
Visual Studio Tools (1.0), Visual Studio Tools 2015, and other components are installed. 
Windows 7 machines:

During SOLIDWORKS 2018 installation, click Change above the Products window on the Summary page. 
Expand SOLIDWORKS in the Product Selection window. 
Visual Studio Tools for Applications (VSTA) is selected by default to install VSTA 2015 and VSTA 1.0. After installing .NET Framework 4.6.2, a system reboot occurs. When the system comes back up, Visual Studio Tools 2015 and the remaining components are installed. If VSTA is de-selected in the installer, then VSTA 1.0 is not installed. 
After installing SOLIDWORKS 2018:

Select Tools > Options > System Options > General > Enable VSTA VERSION 3.0 to configure VSTA (3.0) 2015. When opened, .NET macros created in previous releases are converted to VSTA 2015, and an instance of Visual Studio 2015 opens the converted .NET macro for editing. See Prerequisite Software.
  
If you prefer to use VSTA 1.0 to edit or run VSTA 1.0 macros created in previous releases, you must first activate VSTA 1.0 by de-selecting Tools > Options > System Options > General > Enable VSTA VERSION 3.0. 
Note: You  must have installed VSTA during SOLIDWORKS 2018 installation. 
  
Tools > Macro > Edit > Open > File name dropdown contains:
 
SW VBA Macros (*.swp) 
SW VSTA VB Macro (*.vbproj) 
SW VSTA C# Macro (*.csproj) 
SW Macros (*.swp, *.swb, *.csproj, *.vbproj)
  
Tools > Macro > New > Save As > Save as type dropdown contains:
 
SW VBA Macros (*.swp) 
SW VSTA VB Macro (*.vbproj) 
SW VSTA C# Macro (*.csproj)
  
If you did not select to install VSTA when you installed SOLIDWORKS, then all of the SOLIDWORKS macro dialog dropdowns contain only: 
        SW VBA Macros (*.swp)

You cannot modify or repair your SOLIDWORKS installation to add VSTA. You must uninstall and re-install SOLIDWORKS, selecting to install Visual Studio Tools for Applications (VSTA) during the installation. 
Creating a .NET macro in SOLIDWORKS 2018 using VSTA 3.0
Read Installing VSTA. 
In SOLIDWORKS, select Tools > Macro > New. 
In the Save As dialog, navigate to the directory where to save the project. 
Specify File name. 
In Save as type select SW VSTA VB Macro (*.vbproj) or SW VSTA C# Macro (*.csproj). 
Click Save. 
A new instance of Visual Studio 2015 opens. See Prerequisite Software. 
Solution Explorer shows:
The solution, VstaProjects. 
The project name. 
My Project 
References (sldworks and swconst interop assemblies added from install_dir\api\redist\) 
SolidWorksMacro.vb or SolidWorksMacro.cs. 
The SolidWorksMacro class is assigned a GUID attribute. If you change this attribute, the macro will not run. 
When you compile the macro, the project's configuration (Debug or Release) and output directory are not read. Only a release DLL is saved in project_folder/bin. 
Editing a .NET macro created in SOLIDWORKS 2018 using VSTA 3.0
Read Installing VSTA. 
In SOLIDWORKS, select Tools > Macro > Edit. 
In the Open dialog, navigate to the project folder. 
Select SW Macros in the project type dropdown. 
Select the *.vbproj or *.csproj project file. 
Click Open. 
A new instance of Visual Studio 2015 opens the project you selected. See Prerequisite Software. 
Upgrading a .NET macro created in SOLIDWORKS 2017 or earlier
Read Installing VSTA. 
If VSTA 3.0 is enabled, be sure to back up your VSTA 1.0 .NET macro files before editing them in SOLIDWORKS 2018. 
In order to successfully upgrade a .NET macro, the class name, "SolidWorksMacro", and the method name, "Execute", must be preserved. If you renamed those entities in your VSTA 1.0 macro, the macro cannot be upgraded to VSTA 2015. 
In SOLIDWORKS, select Tools > Macro > Edit. 
In the Open dialog, navigate to the SwMacro directory of the project folder created in a previous release. 
Select SW Macros in the project type dropdown. 
Select the *.vbproj or *.csproj project file. 
Click Open. 
A new project is created in project_folder/SwMacro/upgradedmacro. 
A new instance of Visual Studio 2015 opens the upgraded project. See Prerequisite Software. 
When you compile the macro, the project's configuration (Debug or Release) and output directory are not read. Only a release DLL is saved in project_folder/SwMacro/upgradedmacro/bin. 
The original macro is not modified or moved. 
Running a .NET DLL created in SOLIDWORKS 2017 or earlier
Read Installing VSTA. If you selected to install VSTA 1.0, and de-selected Tools > Options > System Options > General > Enable VSTA VERSION 3.0, you can edit or run VSTA 1.0 macros. If you did not install VSTA 1.0, then VSTA 2015 is active by default, and you must upgrade VSTA 1.0 macros to VSTA 2015: 

Follow the instructions in Upgrading a .NET macro created in SOLIDWORKS 2017 or earlier. 
Select Tools > Macro > Run. 
Select SW VSTA Macros (*.dll) in the file type dropdown. 
Navigate to project_folder\SwMacro\upgradedmacro\bin. 
Select the DLL. 
Click Open to run the macro. 

其实官方说了很多,其实很容易看懂的。
和安装(需要单独去选VSTA环境)以及选项都有关系。
因为我不常用这种方式,毕竟一部分操作通过宏也是录制不到的,而且用swp格式也非常方便,录好的代码自己再使用VS环境再写一次,能更好的加深印象,理解和消化掉知识点。

所以我还是建议大家不要纠结这个录制的格式,习惯就好了,花太多时间去弄这个没必要的环境,不划算。
而且直接用VS 比那个方便的多。

posted @
2021-04-08 15:24 
painezeng  阅读(
0)  评论(
0
编辑 
收藏 
举报  
来源