首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

基于VB 的solidworks二次开发的API,该如何解决

2013-09-07 
基于VB的solidworks二次开发的API下面是我对solidworks中API一个过程的修改,进行向转配体中添加零件然后再

基于VB 的solidworks二次开发的API
下面是我对solidworks中API一个过程的修改,进行向转配体中添加零件然后再装配。
但是倒数第三行总是出现对象变量或with块未设置。请问是那出错了。先谢谢各位了,小弟注册,捧个场。

Option Explicit

Private Sub command1_click()
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swDocExt As ModelDocExtension
Dim swAssy As AssemblyDoc
Dim tmpPath As String
Dim tmpObj As SldWorks.ModelDoc2
Dim boolstat As Boolean
Dim strings As Variant
Dim swcomponent As SldWorks.Component2
Dim matefeature As SldWorks.Feature
Dim MateName As String
Dim FirstSelection As String
Dim SecondSelection As String
Dim Alignment As swMateAlign_e
Dim strCompName As String
Dim AssemblyTitle As String
Dim AssemblyName As String
Dim errors As Long
Dim warnings As Long
Dim mateError As Long
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
' Get title of assembly document
AssemblyTitle = swModel.GetTitle
' Split the title into two strings using the period (.) as the delimiter
strings = Split(AssemblyTitle, ".")
' You'll use AssemblyName when mating the component with the assembly
AssemblyName = strings(0)
Debug.Print AssemblyName
boolstat = True
Dim strCompModelname As String
strCompModelname = "零件2.sldprt"
' Because the component resides in the same folder as the assembly, get
' the assembly's path and use it when opening the component
tmpPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, ""))
' Open the component
Set tmpObj = swApp.OpenDoc6(tmpPath + strCompModelname, swDocPART, 0, "", errors, warnings)
' Check to see if the file is read-only or cannot be found; display error
' messages if either
If warnings = swFileLoadWarning_ReadOnly Then
MsgBox "This file is read-only."
boolstat = False
End If
If tmpObj Is Nothing Then
MsgBox "Cannot locate the file."
boolstat = False
End If
'Re-activate the assembly so that you can add the component to it


Set swModel = swApp.ActivateDoc2(AssemblyTitle, True, errors)
Set swAssy = swModel
' Add the camtest part to the assembly document.
' Currently only one option, swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,
' works for adding a part using AddComponent5. The other options, swAddComponentConfigOptions_e.swAddComponentConfigOptions_NewConfigWithAllReferenceModels and
' swAddComponentConfigOptions_e.swAddComponentConfigOptions_NewConfigWithAsmStructure, work only for adding assemblies using AddComponent5.
Set swcomponent = swAssy.AddComponent5(strCompModelname, swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", -1, -1, -1)
' Get the name of the component for the mate
strCompName = swcomponent.Name2()
' Create the name of the mate and the names of the planes to use for the mate
MateName = "mymate_" + strCompName
FirstSelection = "基准面1@" + strCompName & "@" + AssemblyName
SecondSelection = "基准面1@零件1-1@" + AssemblyName
Set swDocExt = swModel.Extension
swModel.ClearSelection2 (True)
' Select the planes for the mate
boolstat = swDocExt.SelectByID2(FirstSelection, "FACE", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)
boolstat = swDocExt.SelectByID2(SecondSelection, "FACE", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)
' Add the mate

Set matefeature = swAssy.AddMate3(swMateCOINCIDENT, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)
matefeature.Name = MateName
swModel.ViewZoomtofit2
End Sub
vb api solidworks
[解决办法]
Set matefeature = swAssy.AddMate3(swMateCOINCIDENT, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, mateError)


if not matefeature is nothing then
matefeature.Name = MateName
swModel.ViewZoomtofit2
end if

set后,可能返回Nothing,要加入判断
至于为何会返回Nothing,要查文档说明,solidwork的帮助文档有详细的说明

热点排行