中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
Solidworks二次开发-05-装配体中插入零部件
作者:未知 时间:2005-07-27 21:42 出处:CSDN 责编:chinaitpower
              摘要:Solidworks二次开发-05-装配体中插入零部件

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

 

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

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:        (BSTR) Filename                       Document name or full path if not in current directory, including extension

Input:        (long) Type                                    Document type as defined in swDocumentTypes_e

Input:        (long) Options                              Mode in which to open the document as defined in swOpenDocOptions_e

Input:        (BSTR) Configuration                 Model configuration in which to open this document

Applies to parts and assemblies, not drawings

If this argument is empty or the specified configuration is not present in the model,

the model is opened in the last-used configuration.

Output:     (long) Errors                                 Load errors as defined in swFileLoadError_e

Output:     (long) Warnings                           Warnings or extra information generated during the open operation as defined in swFileLoadWarning_e

Return:    (LPDISPATCH) retval                  Pointer to a Dispatch object, the newly loaded ModelDoc2, or NULL if failed to open

 

这个函数参数1就是文档的全名,参数2是要插入的类型描述,其中0123分别表示:

0       swDocNONE:不是sw文件

1       swDocPART:零件

2       swDocASSEMBLY:装配体

3       swDocDRAWING:工程图

如果想使用swDocNONE,需要定义:

Public Enum swDocumentTypes_e

    swDocNONE = 0

    swDocPART= 1

    swDocASSEMBLY = 2

    swDocDRAWING=3

End Enum

参数3是打开文档的模式,一般我们就选择swOpenDocOptions_Silent  0 表示,当然还有只读、只看等选项

参数4是打开选项,一般置空

后面是两个OutPut,用来显示错误打开时的提示

函数返回一个指向打开文件的指针。

 

按照上面的要求我们在向装配体中插入一个零部件时,需要这样步骤:

1、得到装配体

2、使用opendoc6打开需要插入的零件

3、使用addcomponent4插入零件到装配体

我们上面的程序需要这样来修改一下,添加了一个打开文档的函数:

 

' ******************************************************************************

' insertpart 03/21/05 by arden

'插入零件1

'前提条件:在装配体所在文件夹中有零件“零件1存在,并且零件1有配置“配置1

' ******************************************************************************

Dim swApp As SldWorks.SldWorks

Dim Model As ModelDoc2

Dim YSBmodel As ModelDoc2

Dim pth As String

Dim strpath As String

Dim nErrors As Long

Dim nWarnings As Long

 

 

Sub insertpart()

Set swApp = Application.SldWorks

strpath = swApp.GetCurrentWorkingDirectory

Set Model = swApp.ActiveDoc

pth = strpath & "零件1.SLDPRT"

openYSB (pth)  在添加零部件之前,先打开它

Model.AddComponent4 pth, "配置1", 0, 0, 0

End Sub

 

'这个函数打开零件1

Sub openpart(ByVal pth As String)

Dim path As String

Dim newswapp As SldWorks.SldWorks

Set newswapp = Application.SldWorks

 

path = pth

Set YSBmodel = newswapp.OpenDoc6(path, 1, swOpenDocOPtions_Silent, "", nErrors, nWarnings)

YSBmodel.Visible = False  我不想看到零件1

End Sub

 

这样的做法我感觉比较笨~为了赶工程进度我没有再去寻找好的方法,如果您知道有好的方法请告知我,万分感谢。


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有