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

80酷酷网    80kuku.com

  插入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




 


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


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: