Insert New Virtual Component Example (VB.NET)
This example shows how to insert a new part as a virtual component in
an assembly and save it to an external file.
'---------------------------------------------------------------------
' Preconditions:
' 1. Open:
'
<SolidWorks_install_dir>\samples\tutorial\smartcomponents\stepped_shaft.sldasm
' 2. A planar face on the assembly is selected.
'
' Postconditions: The new part is inserted as a virtual component in the assembly.
'
' NOTE: Because this assembly is used in a SolidWorks online tutorial, do
not
' save the assembly when you close it.
'---------------------------------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports
System
Imports
System.Diagnostics
Partial
Class
SolidWorksMacro
Dim
swModel As
ModelDoc2
Dim
swAssy As
AssemblyDoc
Dim
swComponent As
Component2
Dim
swSelMgr As
SelectionMgr
Dim
status As
Long
Sub
Main()
swModel = swApp.ActiveDoc
swAssy = swModel
' Get the pre-selected planar face
Dim
swFeature As
Face2
swSelMgr = swModel.SelectionManager
swFeature = swSelMgr.GetSelectedObject6(1, 0)
' Create the part and insert it as
a virtual component
'
in the assembly
status = swAssy.InsertNewVirtualPart(swFeature,
swComponent)
If
status = 1 Then
Debug.Print("Name
of virtual component: " & swComponent.Name2)
' Check to see if the part is a
virtual component
Debug.Print("Is
component virtual? " & swComponent.IsVirtual)
End
If
swModel.ClearSelection2(True)
End
Sub
Public
swApp As
SldWorks
End
Class