Add Component and Mate Example (VB.NET)
This example shows how to add a component and a mate to an assembly.
'---------------------------------------------------------------------
' Preconditions: Open
' install_dir\Program
Files\SolidWorks\samples\tutorial\toolbox\lens_mount.sldasm.
'
' Postconditions:
' 1. The specified component, camtest.sldprt, and a mate,
' top_coinc_camtest-1, are added to the assembly.
' 2. Examine the FeatureManager design tree to verify the mate.
'
' NOTE: Because the models are used elsewhere, do not save
' any changes when closing them.
'---------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Dim
swModel As ModelDoc2
Dim
swDocExt As ModelDocExtension
Dim
swAssy As AssemblyDoc
Dim
tmpPath As String
Dim
tmpObj As ModelDoc2
Dim
boolstat As Boolean
Dim
strings As Object
Dim
swcomponent As Component2
Dim
matefeature As 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
Sub
Main()
swApp
= CreateObject("SldWorks.Application")
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
= "camtest.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
tmpObj
= swApp.OpenDoc6(tmpPath + strCompModelname,
swDocumentTypes_e.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_e.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
swModel
= swApp.ActivateDoc2(AssemblyTitle,
True, errors)
swAssy
= swModel
'
Add the component 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.
swcomponent
= swAssy.AddComponent5(strCompModelname,
swAddComponentConfigOptions_e.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
= "top_coinc_" + strCompName
FirstSelection
= "Top@" + strCompName & "@" + AssemblyName
SecondSelection
= "Front@" + AssemblyName
swDocExt
= swModel.Extension
swModel.ClearSelection2(True)
'
Select the planes for the mate
boolstat
= swDocExt.SelectByID2(FirstSelection, "PLANE", 0, 0, 0, True,
1, Nothing, swSelectOption_e.swSelectOptionDefault)
boolstat
= swDocExt.SelectByID2(SecondSelection, "PLANE", 0, 0, 0, True,
1, Nothing, swSelectOption_e.swSelectOptionDefault)
'
Add the mate
matefeature
= swAssy.AddMate3(swMateType_e.swMateCOINCIDENT,
swMateAlign_e.swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False,
mateError)
matefeature.Name = MateName
swModel.ViewZoomtofit2()
End
Sub
Public
swApp As SldWorks
End Class