IFC VBA Export and Deleted Items

Hello 

I created a macro on Solidworks that allows you to retrieve a list of assemblies, reposition them, and save them as sldw assemblies and IFCs.

However, the program works in the generated IFCs, all the elements deliberately greyed out in the assemblies are visible.

When I manually save an IFC I get a message asking me if I want to solve the hidden/deleted elements, I check no and I get the desired result.

Do you know how to check this box automatically in VBA ?

Thank you

1 Like

Maybe if you follow the code it would be easier.

The order used at least and/or the full code code to be at the top.

Then it will be easier to answer you.

1 Like

Hello;

sbadenis  is right, without your code or the command used it is not easy to answer correctly... I'm trying anyway...

Normally, these options are included in the "Saveas..." "

Options, ExportData, AdvancedSaveAsOptions, Errors, Warnings)

Use the  swSaveAsOptions_Silent (in the options) to mute solidworks messages

Source: https://help.solidworks.com/2020/...swSaveAsOptions_e.html

Kind regards

Hello

Thank you for your help.


Here is the part of the code concerned:

If False = swModel.Extension.SaveAs(AssIFC & ".IFC", swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent + swSaveAsOptions_e.swSaveAsOptions_AvoidRebuildOnSave, Nothing, errors, warnings) Then
        Err.Raise vbError, "", "Impossible d'exporter l'IFC. Erreur n°: " & errors
End If

 

By juggling options, I don't get what I'm looking for.

This time it 's @Maclane right, the saveAs function is obsolete:

https://help.solidworks.com/2020/english/api/draftsightapi/interop.dsautomation~interop.dsautomation.idocument~saveas.html

So you have to turn to the saveAs3:

https://help.solidworks.com/2020/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IModelDocExtension~SaveAs3.html

I tried with SaveAs3 but there is no option that corresponds to what I am looking for:

 

After a quick test, this code works for me:

Enum IfcFormat_e
    Ifc2x3 = 23
    Ifc4 = 4
End Enum

Const OUT_FILE_PATH As String = "C:\Test.ifc"

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As SldWorks.ModelDoc2
    Set swModel = swApp.ActiveDoc
    
    If Not swModel Is Nothing Then
        
        ExportIfc swModel, OUT_FILE_PATH, IfcFormat_e.Ifc4
        
    Else
        MsgBox "Please open the model"
    End If
    
End Sub

Sub ExportIfc(model As SldWorks.ModelDoc2, path As String, format As IfcFormat_e)
    
    Dim curIfcFormat As Integer
    curIfcFormat = swApp.GetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swSaveIFCFormat)

    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swSaveIFCFormat, format
    
    Dim errors As Long
    Dim warnings As Long
    
    If False = model.Extension.SaveAs3(path, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent, Nothing, Nothing, errors, warnings) Then
        Err.Raise vbError, "", "Failed to export file. Error code: " & errors
    End If
    
    swApp.SetUserPreferenceIntegerValue swUserPreferenceIntegerValue_e.swSaveIFCFormat, curIfcFormat
        
End Sub

Source:

https://www.codestack.net/solidworks-api/import-export/export-ifc/

adapted to saveas3

Thank you sbadenis

The problem is not that the export doesn't work, the code is correct, only I don't have what I want as output.

An explanation:

I have an assembly with two parts:

The desired configuration with the greyed out sphere:

What I get after running the program:

 

And the generated IFC file:

 

The program does not take into account my solidworks configuration.

Hello;

Well on the other hand the management of Configurations was not part of the question asked....
To find out the Configuration Quantity:

Dim swModel As ModelDoc2
Dim ConfigCount as string

ConfigCount = swModel.GetConfigurationCount

....
Then to retrieve the Name of all your Configurations:

Dim swModel As ModelDoc2
Dim AllConfNames as variant

AllConfNames = swModel.GetConfigurationNames()

...
To get the Active Configuration Name:

Dim swModel As ModelDoc2
Dim ConfActName as string

ConfActName = swModel.GetActiveConfiguration.Name


... All you have to do is build a loop in the macro provided by sbadenis and you're done.

Kind regards.

Hello

This message is not caught by the saveAs function. SW reactivates the deleted component and suddenly the export is "not compliant" with what Ka.Couff wants .

This is not a configuration problem, so the response is not adequate ;)

We should look at SW events if we can pick up this message and do a treatment. 

2 Likes

@Cyril.F

Yes, that's it
I had tried to record the macro by launching the manipulation manually but there is nothing that displays the message:

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

' Save As
longstatus = Part.SaveAs3("XXX.IFC", 0, 0)
End Sub

Hello

I don't have much time at the moment to look at this but if you can't intercept the message there is always the possibility to delete the component in the deleted state, do the export and close without saving.

The API may not allow access to this message at this time (SW adds access to functions as SW is released).

1 Like

Hello

To do that you would have to be able to select the deleted items, but same, I couldn't find any code for that :/

Look at this code that seems to work for me (use a windows command:

https://www.codestack.net/solidworks-api/document/assembly/components/select-suppressed/

Or else via the solidworks commands you can check the components in the deleted state with this command:

http://help.solidworks.com/2020/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.icomponent2~issuppressed.html?verRedirect=1

Hello 

Thanks @sbadenis, it works!