基于vb 的solidworks 二次开发解决思路

基于vb 的solidworks 二次开发
本帖最后由 u011802624 于 2013-09-01 09:30:34 编辑
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