Solidworks API Creating a Virtual Part in an Assembly

Hello community.

 

In order to develop a macro within my company, I would like to automate the creation of virtual parts in assembly.

However, I can't seem to develop the code that allows this action.

 

Below is the beginning of my research.

 

Dim swApp As Object
Sub main()

Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc
Dim swModelTitle As SldWorks.ModelDoc2


     
'-------------------------------------------------------------------------------------------------------------
'Verifying that an asm file document is open
'-------------------------------------------------------------------------------------------------------------

     If Not swmodel Is Nothing Then' if a SW file is opened
     Debug.Print "a SW file is opened" 'then msg debug and continue

     Else: MsgBox ("There is no SW file open, please open an assembly and restart the macro") 'if no SW file open => msg
Exit Sub
    End If
    

     
     Dim type_doc As String
     type_doc = swDocumentTypes_e.swDocPART
     Debug.Print type_doc
     
     If swmodel. GetType = swDocumentTypes_e.swDocASSEMBLY Then
     Debug.Print "The open file is an assembly file"
     Else: Debug.Print "the open file is not an assembly file, please open an assembly and restart the macro"
     MsgBox ("the open file is not an assembly file, please open an assembly and restart the macro")
Exit Sub
      End If
      

'----------------------------------------------------------------------------------------------------------
'Retrieving the file name
'----------------------------------------------------------------------------------------------------------
Dim Path As String
Dim name_asm As String
name_asm = swmodel. GetTitle
path = swmodel. GetPathName
Debug.Print "filename: " & name_asm
Debug.Print "path:" & path
Sun nom_asm
nom_asm = Left(name_asm, (InStrRev(name_asm, ".", -1, vbTextCompare) - 1))
Debug.Print nom_asm


'-----------------------------------------------------------------------------------------------------------
'Creation of imprint parts
'-----------------------------------------------------------------------------------------------------------


Dim nom_pe As String
nom_pe = "EMP_" & nom_asm
Debug.Print "part name imprint: " & nom_pe
Sun new_part

boolstatus = swmodel. Extension.SelectByID2("Face Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0) 'Selecting Face Plane
new_part = swmodel. InsertNewVirtualPart(boolstatus, nom_pe)

swmodel. ForceRebuild3 True
swmodel. ViewZoomtofit2


End Sub

 

 

The error code obtained on the line new_part = ....

 

Do you have any idea what I'm doing wrong???

I admit that the example of the API help leaves me perplexed...

 

 

 

 


sans_titre.png

Hello

A basic rule of programming is to correctly declare your variables.

In this case, the line on which there is an error:

Sun to kick off the declaration

New_part the variable name

as to set the type

... a type

 

So you're missing the type. I think it's the type of piece to see in the help...

1 Like

Hello

A priori you are mixing the name with the component itself.

Exchange:

Dim nom_pe As String

In 

Dim nom_pe As Component2

And delete the line:

nom_pe = "EMP_" & nom_asm

Edit: see the corrected macro in PJ.

Edit 2:

If you want to change the name, you have to do the following:

nom_pe.Name2 = EMP_" & nom_asm

I updated the macro in PC.


macro_piece_virtuelle.pdf
1 Like

@ remrem

I'm a beginner in prog and still have big gaps

I thought that not putting a... left the choice to the prog to take the most suitable type of variable.

 

@.PL

I still get the same error message.

 

Is the error on the same line?

In the 3D of SolidWorks, is there a side selected?

Edit: try the modification as an attachment.

If there is an error, please specify on which line please.


macro_piece_virtuelle.txt

yes always on the same line.

I have the front plane that is selected in the previous line.

 

In fact, that's why you have to get into the habit of declaring all the variables correctly.

If we don't declare the variables, they use the default variant type. This can cause functionality and performance issues...

1 Like

always the line below in error

new_part = swmodel. InsertNewVirtualPart(boolstatus, nom_pe)
 

Runtime Error 13 Type Mismatch

 

 

Stupid question that surely has nothing to do with the problem, but the "_" is not a source of error?

I don't think so because I used it a lot in a previous macro I did earlier.

 

I didn't have any problems too much.

 

After all, given my inexperience, I can't guarantee anything...

 

1 Like

I probably won't be able to test it until tomorrow night.

 @.PL

 

No worries, it's already very nice to try to help me...

I will continue my research and tests no matter what and will post if it evolves.

 

 

Good evening

Looking at the API help, the variable e-types in parameter are not good.

In my opinion, the right handwriting is the one in the attached file (tested and functional).


correction_macro_piece_virtuelle.txt

@lyric.

 

Indeed, it works wonderfully.

A big thank you to you.

For the moment I admit that I don't really understand the why and the how. I'll look at these lines of code to understand the ins and outs.)  

 

Thanks again

 

Happydad

Hello Happydad,

To understand, a quick look at the API help by putting the InsertNewVirtualPart function as a search criterion will explain largely where the problem came from.

The type of variable expected as a parameter by this function is explained and this is where the problem comes from (first incompatible parameter).

The txt dot of lyric doesn't work when I copy the text into a blank macro in vba (pb of accents among others).

Can someone post the .swp or .swb please??

' The functional macro gives this when fixed ;)

Dim swApp As Object
Sub main()
Set swApp = Application.SldWorks
Set swmodel = swApp.ActiveDoc
Dim swModelTitle As SldWorks.ModelDoc2
'-------------------------------------------------------------------------------------------------------------
'Verifying that an asm file document is open
'-------------------------------------------------------------------------------------------------------------
     If Not swmodel Is Nothing Then' if a SW file is opened
     Debug.Print "a SW file is opened" 'then msg debug and continue
     Else: MsgBox ("There is no SW file open, please open an assembly and restart the macro") 'if no SW file open => msg
Exit Sub
    End If
     Dim type_doc As String
     type_doc = swDocumentTypes_e.swDocPART
     Debug.Print type_doc
     If swmodel. GetType = swDocumentTypes_e.swDocASSEMBLY Then
     Debug.Print "The open file is an assembly file"
     Else: Debug.Print "the open file is not an assembly file, please open an assembly and restart the macro"
     MsgBox ("the open file is not an assembly file, please open an assembly and restart the macro")
Exit Sub
      End If
'----------------------------------------------------------------------------------------------------------
'Retrieved©artion of the file name
'----------------------------------------------------------------------------------------------------------
Dim Path As String
Dim name_asm As String
name_asm = swmodel. GetTitle
path = swmodel. GetPathName
Debug.Print "filename: " & name_asm
Debug.Print "path:" & path
Sun nom_asm
nom_asm = Left(name_asm, (InStrRev(name_asm, ".", -1, vbTextCompare) - 1))
Debug.Print nom_asm
'-----------------------------------------------------------------------------------------------------------
'Creation of imprint parts
'-----------------------------------------------------------------------------------------------------------
Dim nom_pe As SldWorks.Component2
Sun swSelMgr As SldWorks.SelectionMgr
Dim swPlaneFeature As SldWorks.Feature
Dim swPlane As SldWorks.RefPlane
Sun new_part As Long
Dim boolstatus As Boolean

Set swSelMgr = swmodel. SelectionManager
boolstatus = swmodel. Extension.SelectByID2("Face Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0) 'Selecting Face Plane
Set swPlaneFeature = swSelMgr.GetSelectedObject6(1, -1)
Set swPlane = swPlaneFeature.GetSpecificFeature2

new_part = swmodel. InsertNewVirtualPart(swPlane, nom_pe)

nom_pe. Name2 = "To EMP_" & nom_asm

swmodel. ForceRebuild3 True
swmodel. ViewZoomtofit2
End Sub