欢迎光临
我们一直在努力

Solidworks二次开发-05-装配体中插入零部件-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

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:        (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

 

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Solidworks二次开发-05-装配体中插入零部件-.NET教程,Asp.Net开发
分享到: 更多 (0)