Macro save as a STEP assembly by turning it into a part

Hello I am looking for an option in VBA to register an assembly in STEP but before saving it transforms the assembly into an external surface part

Hello

I don't have SW on hand right now to check but you should be able to use the SaveAs3 Method function (IModelDocExtension) with the option "ISldWorks::SetUserPreferenceIntegerValue" as indicated a little further down this page:

Kind regards

I just tried with this: 

        If CheckBoxSTEP.Value = True Then

               filename = LblCheminSTEP & ActiveConfig & ".STEP"
            
                boolstatus = swApp.SetUserPreferenceIntegerValue(swStepAP, 214)
                boolstatus = swApp.SetUserPreferenceIntegerValue(swSaveAssemblyAsPartOptions, swSaveAsmAsPart_ExteriorFaces)
                boolstatus = swModelDocExt.SaveAs(filename, 0, 0, swExportData, lErrors, lWarnings)
                
                If boolstatus Then
                    MsgBox "Enregistrement au format STEP réussi" & vbNewLine & filename
                Else
                    MsgBox "Echec de l'enregistrement au format STEP, Error code:" & lErrors, vbCritical, "AVERTISSEMENT"
                End If
        End If

but it doesn't change anything? he records in STEP but he creates the different pieces separately in the STEP.

I think you have to do it in several steps like in solidworks

1- Register the assembly as a part with external surface

2-Open the room

3-Step export the part

4-To erase or not the part with external surface

1 Like

Separate your steps as sbadenis indicated.

Okay I'll go with that, thank you. 
But I'm wondering how I'm going to get it to delete the piece automatically save once the step is created.

What I'm doing doesn't work. When I save the assembly as a part it creates as much body/solid as there are parts in the assembly. and I would like everything to be "merged" into 1 body when I register the assembly in parts or directly in STEP if it's possible.

I just saw this post on the solidworks forum which dates from 2012.

To test and give us feedback:

https://forum.solidworks.com/thread/55589

 

Here is the 1st part to save the assembly in parts

Dim swApp               As SldWorks.SldWorks
Dim swModel             As SldWorks.ModelDoc2
Dim swModelDocExt       As SldWorks.ModelDocExtension
Dim FilePath            As String
Dim sFilePath           As String
Dim PathSize            As Long
Dim PathNoExtension     As String
Dim NewFilePath         As String
Dim nErrors             As Long
Dim nWarnings           As Long

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelDocExt = swModel.Extension

FilePath = swModel.GetPathName
PathSize = Strings.Len(FilePath)
PathNoExtension = Strings.Left(FilePath, PathSize - 6)
NewFilePath = PathNoExtension & "SLDPRT"

swApp.SetUserPreferenceIntegerValue swSaveAssemblyAsPartOptions, swSaveAsmAsPart_ExteriorFaces
swModelDocExt.SaveAs NewFilePath, swSaveAsCurrentVersion, swSaveAsOptions_Silent, Nothing, nErrors, nWarnings

swApp.CloseDoc swModel.GetTitle

Set swModel = swApp.OpenDoc(NewFilePath, swDocPART)
Set swModel = swApp.ActiveDoc

sFilePath = PathNoExtension & "STEP"
 
swModel.SaveAs (sFilePath)

swApp.CloseDoc swModel.GetTitle

End Sub
'------------------

Then with open doc6 you should be able to open it and then export it in step

For the deletion of the file maybe with FSO  it is feasible

Edit: for the deletion of the file:

https://forum.solidworks.com/thread/61273

 

For open doc6:

http://help.solidworks.com/2018/English/api/sldworksapi/Open_Document_Example_VB.htm

it works, the only problem is that it transforms the assembly into a surface part as requested, and then when we save this part in STEP it recreates bodies for me.

 

Is it not possible to save so that there is only 1 body (that everything is merged/grouped into 1 single Imported block)?

Hello

You need to do:

- Opening the assembly.

- Saving this assembly in part.

- Opening of the newly created part.

- Combine the bodies with the InsertCombineFeature Method (IFeatureManager) function with the Enumeration swBodyOperationType_e set to 15903 (SWBODYADD). You have an example HERE.

- Save as a step.

Kind regards

2 Likes

I just did a test just like that before starting the macro and as soon as I'm on assemblies that are a little "complex" I get this error message when I combine.

 

Hello

Do the bodies you are trying to combine into 1 single body have an intersection?

If not, then this message is normal.

Kind regards

1 Like

Yes that's right, it's hardware (jacks or screws). There's no way for him to combine everything he can combine together, and what he can't combine too bad he leaves it like that and automatically of course?

Well yes, by doing loops and error management in your macro ...