日期:2014-01-06  浏览次数:20605 次

Solidworks二次开发--装配体中插入零部件
 


 


在往装配体中插入零部件时,我们使用addcomponent 函数。如果需要选定零部件的配置,则需要使用addcomponent4。



先学习下语法:




 


addcomponent4:



retval = AssemblyDoc.AddComponent4 ( compName, configName, x, y, z)




 


Input:         (BSTR) compName                    Path name of a loaded part or assembly to add as a component



Input:         (BSTR) configName                   Name of the configuration from which to load the component



Input:        (double) x                                      X coordinate of the component center



Input:         (double) y                                      Y coordinate of the component center



Input:        (double) z                                      Z coordinate of the component center



Output:     (LPCOMPONENT2) retval          Pointer to the Component2 object




 


需要注意的是:参数1为文件的全名(包括路径);参数2为文件的配置名称;当函数执行成功购返回一个指向该零件的指针。



于是我们可以如下写一个小程序,用来给装配体中插零件:



‘filename:insertPart.swp



‘write by
arden
2005-4-4



‘this function add a part called “零件1.SLDPRT” in CurrentWorkingDirectory



‘precondition is there has a part document called  “零件1.SLDPRT” in CurrentWorkingDirectory



‘and it has a configuration called “配置1”




 


Dim swApp As SldWorks.SldWorks



Dim Model As ModelDoc2



Dim pth As String



Dim strpath As String




 


Sub insertPart()



Set swApp = Application.SldWorks



strpath = swApp.GetCurrentWorkingDirectory  ‘当前工作路径



Set Model = swApp.ActiveDoc



pth = strpath & "零件1.SLDPRT"             ‘得到文件的FULLPATH全名



Model.AddComponent4 pth, "配置1", 0, 0, 0   ‘添加零部件



End Sub




 


然而,这个程序比不是想象中那么好用。为什么呢??回头看addcomponent4的remark,上面这样写:



The specified file must be loaded in memory. A file is loaded into memory when you load the file in your



SolidWorks session (SldWorks::OpenDoc6) or open an assembly that already contains the file.



就是说你想指定的插入的文件必须在调用函数之前已经在内存中加载了。



不习惯,你就不能直接打开多简单,没办法,我还没有找到好的方法,只能按人家的来:



看看下面的函数Opendoc6,它打开一个文档:




 


Opendoc6:



retval = SldWorks.OpenDoc6 ( filename, type, options, configuration, &Errors, &Warnings )



Input: