Insert Virtual Part from template creates an empty sketch

Library for macros
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

I would like to insert a new virtual part in a assembly based on a specific template, make the part and the assy origins coincident and the virtual part fixed.

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 origin to insert the new virtual part the new empty skecth at the bottom of the tree is not created, but assembly origin and the new virtual part origin are not coincident.

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).
by gupta9665 » Sat May 06, 2023 2:06 am
mp3-250 wrote: Fri May 05, 2023 3:16 am @gupta9665
I have just begun writing macro and there are a lot of things I still have to learn, could you explain me how to put the virtual part I just created in edit mode, and select the sketch without user input?

Something like this is not in context and does not work.

Code: Select all

  swAssy.InsertNewVirtualPart swFace, swComponent

  'how to put the new virtual part in edit mode?

  swAssy.SelectByID "Sketch2", "SKETCH", 0, 0, 0
  swAssy.EditDelete

Add below lines after swAssy.InsertNewVirtualPart swFace, swComponent

Code: Select all

    Dim sAssyName As String
    
    sAssyName = Mid(swAssy.GetPathName, InStrRev(swAssy.GetPathName, "\") + 1)
    sAssyName = Left(sAssyName, InStrRev(sAssyName, ".") - 1)

    If Not swComponent Is Nothing Then
      swAssy.Extension.SelectByID2 "Sketch1@" & swComponent.Name2 & "@" & sAssyName, "SKETCH", 0, 0, 0, False, 0, Nothing, 0
      swAssy.EditDelete
    End If
Go to full post
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 599

Re: Insert Virtual Part from template creates an empty sketch

Unread post by zwei »

I have not deal with virtual part alot so i might be wrong...
But i think this is the "default" behavior when inserting part

You might need some additional code to remove the sketch / fix the origin after inserting the virtual part
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Insert Virtual Part from template creates an empty sketch

Unread post by gupta9665 »

This is a default behavior when inserting a new virtual component (even manually). So you you can add the codes to delete the the sketch.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
DanPihlaja
Posts: 747
Joined: Thu Mar 11, 2021 9:33 am
Answers: 24
Location: Traverse City, MI
x 738
x 893

Re: Insert Virtual Part from template creates an empty sketch

Unread post by DanPihlaja »

The default behavior for inserting a part into an assembly, is that you select a plane for your first sketch to go onto.

Then it automatically enters the sketch.

So, if you are doing it programatically, then it is probably simply exiting the sketch in the program part of it.
-Dan Pihlaja
Solidworks 2022 SP4

2 Corinthians 13:14
User avatar
mattpeneguy
Posts: 1380
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2487
x 1888

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mattpeneguy »

Would the below command work if it is placed after the code to insert the virtual part?

Code: Select all

bRet = swModel.ForceRebuild3(False)
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

gupta9665 wrote: Thu May 04, 2023 2:17 am This is a default behavior when inserting a new virtual component (even manually). So you you can add the codes to delete the the sketch.
Yes and No... actually selecting the ORIGIN instead of the front plane the sketch is not created.
Probably the sketch fails on creating due to the missing plane I guess. It would be nice to have some more control over it.
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

mattpeneguy wrote: Thu May 04, 2023 2:53 pm Would the below command work if it is placed after the code to insert the virtual part?

Code: Select all

bRet = swModel.ForceRebuild3(False)
SW still insert an empty sketch at the end of the tree.
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Insert Virtual Part from template creates an empty sketch

Unread post by gupta9665 »

mp3-250 wrote: Fri May 05, 2023 1:04 am Yes and No... actually selecting the ORIGIN instead of the front plane the sketch is not created.
Probably the sketch fails on creating due to the missing plane I guess. It would be nice to have some more control over it.
Correct.

Well the option would be to edit the sketch and exists it. This will remove the sketch for being empty. Other option would be to delete it.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

@gupta9665
I have just begun writing macro and there are a lot of things I still have to learn, could you explain me how to put the virtual part I just created in edit mode, and select the sketch without user input?

Something like this is not in context and does not work.

Code: Select all

  swAssy.InsertNewVirtualPart swFace, swComponent

  'how to put the new virtual part in edit mode?

  swAssy.SelectByID "Sketch2", "SKETCH", 0, 0, 0
  swAssy.EditDelete
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

DanPihlaja wrote: Thu May 04, 2023 10:45 am The default behavior for inserting a part into an assembly, is that you select a plane for your first sketch to go onto.

Then it automatically enters the sketch.

So, if you are doing it programatically, then it is probably simply exiting the sketch in the program part of it.
@DanPihlaja the macro does not behave like "insert new part" it does not enter the sketch edit mode in a visible way, it leaves the sketch empty (and this is not allowed to the user either, empty sketches are not allowed normally or at least within our setup...) so I cannot exit the sketch during part creation.
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 599

Re: Insert Virtual Part from template creates an empty sketch

Unread post by zwei »

I believe you macro will need to

Activate/Edit the virtual part → delete the sketch

My understanding is that kind of SOLIDWORKS "out of box" functionality/limitation as virtual component origin will depends on your first sketch

See
https://help.solidworks.com/2017/englis ... part2.html

Example
https://help.solidworks.com/2017/englis ... ple_VB.htm
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
gupta9665
Posts: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

Re: Insert Virtual Part from template creates an empty sketch

Unread post by gupta9665 »

mp3-250 wrote: Fri May 05, 2023 3:16 am @gupta9665
I have just begun writing macro and there are a lot of things I still have to learn, could you explain me how to put the virtual part I just created in edit mode, and select the sketch without user input?

Something like this is not in context and does not work.

Code: Select all

  swAssy.InsertNewVirtualPart swFace, swComponent

  'how to put the new virtual part in edit mode?

  swAssy.SelectByID "Sketch2", "SKETCH", 0, 0, 0
  swAssy.EditDelete

Add below lines after swAssy.InsertNewVirtualPart swFace, swComponent

Code: Select all

    Dim sAssyName As String
    
    sAssyName = Mid(swAssy.GetPathName, InStrRev(swAssy.GetPathName, "\") + 1)
    sAssyName = Left(sAssyName, InStrRev(sAssyName, ".") - 1)

    If Not swComponent Is Nothing Then
      swAssy.Extension.SelectByID2 "Sketch1@" & swComponent.Name2 & "@" & sAssyName, "SKETCH", 0, 0, 0, False, 0, Nothing, 0
      swAssy.EditDelete
    End If
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

@gupta9665 thank you very much for your time!

In the meanwhile I have realized that I need to point at the whole @...@... chain to select the object in context and put together something to strip the assy extension etc, but your code is way more elegant and very compact!
I like it very much!
GHamstra
Posts: 1
Joined: Thu Sep 14, 2023 2:22 pm
Answers: 0
x 1

Re: Insert Virtual Part from template creates an empty sketch

Unread post by GHamstra »

If you are happy with inserting based on front plane (an InPlace mate which achieves the same alignment as an Origin to Origin mate with the align axis option) I created a macro to do this, I have it tied to a button on my assembly tab.

Code: Select all

'Forces variables to be explicitly declared before use
Option Explicit

'Code by Graham Hamstra
Dim swApp As SldWorks.SldWorks

Sub main()

Dim swModel As ModelDoc2
Dim swAssembly As AssemblyDoc
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim swTemplatePref As String
Dim swModelExt As ModelDocExtension
Dim swCallout As Callout
Dim swSelManager As SelectionMgr
Dim swFrontPlane As RefPlane
Dim insertedComp As Component2
Dim swSelectData As SelectData
Dim returnInfo As Long
Dim planeFeat As Feature
Dim swFeat As Feature

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

'verifies that the macro is running in an active assembly
If swModel.GetType = swDocASSEMBLY Then
    Set swAssembly = swModel
    'Exits in-context editing if macro was triggered while editing a part
    swAssembly.EditAssembly
Else
    MsgBox "This macro can only insert new parts into an assembly, please open an assembly before running this macro"
    End
End If



Set swModelExt = swModel.Extension
Set swSelManager = swModel.SelectionManager

'Selects front plane to use for "InPlace" mate
boolstatus = swModelExt.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, swCallout, swSelectOptionDefault)

'Exits macro if it could not select Front Plane prior to inserting virtual component
If boolstatus = False Then
    MsgBox "Failed to select Front Plane, please ensure that assembly contains the default plane named Front Plane before running this macro"
    End
End If

Set planeFeat = swSelManager.GetSelectedObject6(1, -1)
Set swFrontPlane = planeFeat.GetSpecificFeature2()

'Gets default part template to create virtual part with
swTemplatePref = swApp.GetUserPreferenceStringValue(swDefaultTemplatePart)

' Inserts New Virtual Part Into the Assembly
longstatus = swModel.InsertNewVirtualPart(swFrontPlane, insertedComp)

Set swSelectData = swSelManager.CreateSelectData()

'Selects newly created virtual part
boolstatus = insertedComp.Select4(False, swSelectData, False)
'Edits newly created virtual part
longstatus = swAssembly.EditPart2(True, False, returnInfo)

'Finds empty sketch automatically created in the virtual part and deletes it
Set swFeat = insertedComp.FirstFeature()
Do Until swFeat Is Nothing
    'ProfileFeature is the return solidworks gives when the feature is a sketch
    If swFeat.GetTypeName2 = "ProfileFeature" Then
       boolstatus = swSelManager.SuspendSelectionList
       boolstatus = swSelManager.AddSelectionListObject(swFeat, swSelectData)
       boolstatus = swModelExt.DeleteSelection2(swDelete_Absorbed)
       Set swFeat = swFeat.GetNextFeature()
    Else
        Set swFeat = swFeat.GetNextFeature()
    End If
Loop

'Triggers solidworks to save the part so that it can be named correctly
swApp.RunCommand swCommands_SaveAs, ""

End Sub
User avatar
mp3-250
Posts: 540
Joined: Tue Sep 28, 2021 4:09 am
Answers: 18
Location: Japan
x 601
x 282

Re: Insert Virtual Part from template creates an empty sketch

Unread post by mp3-250 »

thank you for the suggestion.
I noticed the virtual component insertion is very slow if the assembly is not empty...

I timed within the macro the operation and selecting a plane before insertion slows everything by 2 seconds, while without the reference selection take less than 0.2s. downside is the component origin and the assy origin are not coincident.
I need to make them coincident without losing too much time...
Post Reply