I am testing this macro, but I get an extra empty sketch at the bottom of the feature tree inside the virtual part. Is it avoidable?
https://thecadcoder.com/solidworks-vba- ... tual-part/
Code: Select all
Option Explicit
' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Variable for Solidworks Assembly
Dim swAssembly As SldWorks.AssemblyDoc
' Variable for Solidworks Component
Dim swComponent As SldWorks.Component2
' Variable for Solidworks Face
Dim swFace As SldWorks.Face2
' Program to Insert virtual part
Sub main()
' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox "Solidworks document is not opened."
Exit Sub
End If
' Set Solidworks Assembly document
Set swAssembly = swDoc
' Get the selected face and set it to the Solidworks Face variable
Set swFace = swDoc.SelectionManager.GetSelectedObject6(1, -1)
' Insert Virtual part
swAssembly.InsertNewVirtualPart swFace, swComponent
' If there are error
If swComponent Is Nothing Then
' Inform user and exit function.
MsgBox "Failed to add Virtual part."
Exit Sub
End If
End Sub
If I select the front datum plane to insert the new virtual part the new empty skecth at the bottom of the tree is created, and assembly origin and the new virtual part origin are coincident.
It seems that selecting a plane to insert the virtual part triggers a sketch creation (which is a behaviour disabled in my system option for new parts).