Macro export dxf part family

Hello colleagues,

I'm coming to you because I'm a macro quiche and I have one that I would have liked to improve. It works perfectly when I run it and make all my dxf of the part family but I can't modify it so that it takes out the bend lines in the press.   

Thank you in advance for your help :-)

Dim swApp As Object
Option Explicit
Sub main()
'Déclarations :
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim config                  As SldWorks.Configuration
Dim vConfNameArr            As Variant
Dim sConfigName             As String
Dim i                       As Long
Dim bShowConfig             As Boolean
Dim bRebuild                As Boolean
Dim bRet                    As Boolean
Dim FilePath                As String
Dim PathSize                As Long
Dim PathNoExtension         As String
Dim NewFilePath             As String
Dim Value_                 As String
Dim ResolvedValOut          As String
Dim cusPropMgr              As SldWorks.CustomPropertyManager
Dim wasResolved             As Boolean
Dim Error As Long
Set swApp = CreateObject("SldWorks.Application") 'Lancement de SW
Set swModel = swApp.ActiveDoc 'Récuperation du modèle actif dans SW

vConfNameArr = swModel.GetConfigurationNames 'Création de la liste des configurations
For i = 0 To UBound(vConfNameArr) 'Boucle la liste : de l'élément 0 jusqu'au nombre d'élément dans la liste (Ubound)
    Set config = swModel.GetActiveConfiguration
    Set cusPropMgr = config.CustomPropertyManager
    sConfigName = vConfNameArr(i) 'Recupère l'élément N°i de la liste
    bShowConfig = swModel.ShowConfiguration2(sConfigName) 'Affiche la configuration
    Error = cusPropMgr.Get5("TYPE", True, Value_, ResolvedValOut, wasResolved) 'Récupère la valeur de la proriété "" dans la variable "Value_"
    bRebuild = swModel.ForceRebuild3(False) 'Reconstruction du modèle
    FilePath = swModel.GetPathName 'Récupère le chemin du fichier SW
    PathSize = Strings.Len(FilePath) 'Compte le nombre de caractères du chemin
    
        PathNoExtension = Strings.Left(FilePath, PathSize - 6) 'Récupère le nom de la pièce en enlevant .Sldrt
        NewFilePath = Left(FilePath, InStrRev(FilePath, "\")) & "" & (sConfigName) & ".DXF" 'Remplace le nom par Type + Lg + Nom de la config (sans Flat pattern).dxf
        bRet = swModel.ExportFlatPatternView(NewFilePath, 1) 'Exporte le déplié
    
Next i 'Passe à la prochaine config
End Sub

 


export_dxf_famille_de_piece_-_ac_cobra.swp

Good evening

Unless I'm mistaken, the option is not accessible in this old method.

 You have to use the ExportToDWG2 method , the non-optimized code simply taken from the help is the following:

Dim swApp As Object
Option Explicit
Sub main()
'Déclarations :
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim config                  As SldWorks.Configuration
Dim swPart                  As SldWorks.PartDoc
Dim vConfNameArr            As Variant
Dim sConfigName             As String
Dim i                       As Long
Dim bShowConfig             As Boolean
Dim bRebuild                As Boolean
Dim bRet                    As Boolean
Dim FilePath                As String
Dim PathSize                As Long
Dim PathNoExtension         As String
Dim NewFilePath             As String
Dim Value_                 As String
Dim ResolvedValOut          As String
Dim cusPropMgr              As SldWorks.CustomPropertyManager
Dim wasResolved             As Boolean
Dim Error As Long
Dim varAlignment As Variant
 Dim dataAlignment(11) As Double
 Dim varViews As Variant
 Dim dataViews(1) As String
Set swApp = CreateObject("SldWorks.Application") 'Lancement de SW
Set swModel = swApp.ActiveDoc 'Récuperation du modèle actif dans SW
Set swPart = swModel

vConfNameArr = swModel.GetConfigurationNames 'Création de la liste des configurations
For i = 0 To UBound(vConfNameArr) 'Boucle la liste : de l'élément 0 jusqu'au nombre d'élément dans la liste (Ubound)
    Set config = swModel.GetActiveConfiguration
    Set cusPropMgr = config.CustomPropertyManager
    sConfigName = vConfNameArr(i) 'Recupère l'élément N°i de la liste
    bShowConfig = swModel.ShowConfiguration2(sConfigName) 'Affiche la configuration
    Error = cusPropMgr.Get5("TYPE", True, Value_, ResolvedValOut, wasResolved) 'Récupère la valeur de la proriété "" dans la variable "Value_"
    bRebuild = swModel.ForceRebuild3(False) 'Reconstruction du modèle
    FilePath = swModel.GetPathName 'Récupère le chemin du fichier SW
    PathSize = Strings.Len(FilePath) 'Compte le nombre de caractères du chemin
    
        PathNoExtension = Strings.Left(FilePath, PathSize - 6) 'Récupère le nom de la pièce en enlevant .Sldrt
        NewFilePath = Left(FilePath, InStrRev(FilePath, "\")) & "" & (sConfigName) & ".DXF" 'Remplace le nom par Type + Lg + Nom de la config (sans Flat pattern).dxf
        'bRet = swModel.ExportFlatPatternView(NewFilePath, 1) 'Exporte le déplié
    dataAlignment(0) = 0#
    dataAlignment(1) = 0#
    dataAlignment(2) = 0#
    dataAlignment(3) = 1#
    dataAlignment(4) = 0#
    dataAlignment(5) = 0#
    dataAlignment(6) = 0#
    dataAlignment(7) = 1#
    dataAlignment(8) = 0#
    dataAlignment(9) = 0#
    dataAlignment(10) = 0#
    dataAlignment(11) = 1#
     

    varAlignment = dataAlignment
     

    dataViews(0) = "*Current"
    dataViews(1) = "*Front"
     

    varViews = dataViews

    swPart.ExportToDWG2 NewFilePath, FilePath, swExportToDWG_ExportSheetMetal, True, varAlignment, False, False, 13, varViews
    
Next i 'Passe à la prochaine config
End Sub

 

I think on the other hand that the filepath and pathsize variables should be removed from the loop processing since this macro works file by file if I understand correctly.

@Cyril.f . actually the macro I use creates the flatpathern in a room of several configurations by saving it with the name of the configuration, the only thing missing is the fold line in the DXF.

I just tested the macro that works, it creates the DXF with the fold lines but no longer creates the unfolded configurations . 

Hello

I didn't look more than that last night, but isn't it rather than unfolded configurations that the macro does and no longer does the dxf of folded configurations?

In fact the macro I had found done: creation of the dxf in unfolded without the fold lines and the configuration of the unfolded of all the configurations of the part and the one you shared no longer create the configurations of the unfolded in the configurations but made all the unfolded with the fold lines

I can have a sample file because I don't have one on hand. I just had a file with an unfolded conf and the default conf and it looped well on both "configurations" but it seems to me that the two dxfs were in unfolded visual (linked to the macro code export line).

Attached are example files but it is under 2020 one with the gonfig unfolded and the other without


lot_de_cornieres.sldprt
lot_de_cornieres_avec_deplie.sldprt

Good evening

On the attached code it should meet the need.

Option Explicit

'Déclarations :
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim config                  As SldWorks.Configuration
Dim cusPropMgr              As SldWorks.CustomPropertyManager
Dim swPart                  As SldWorks.PartDoc
Dim i                       As Long
Dim PathSize                As Long
Dim Error                   As Long
Dim bShowConfig             As Boolean
Dim bRebuild                As Boolean
Dim bRet                    As Boolean
Dim wasResolved             As Boolean
Dim FilePath                As String
Dim PathNoExtension         As String
Dim NewFilePath             As String
Dim Value_                  As String
Dim ResolvedValOut          As String
Dim sConfigName             As String
Dim varAlignment            As Variant
Dim vConfNameArr            As Variant
Dim dataAlignment(11)       As Double
Sub main()

Set swApp = CreateObject("SldWorks.Application") 'Lancement de SW
Set swModel = swApp.ActiveDoc 'Récuperation du modèle actif dans SW
Set swPart = swModel
FilePath = swModel.GetPathName 'Récupère le chemin du fichier SW
PathSize = Strings.Len(FilePath) 'Compte le nombre de caractères du chemin
PathNoExtension = Strings.Left(FilePath, PathSize - 6) 'Récupère le nom de la pièce en enlevant .Sldrt

vConfNameArr = swModel.GetConfigurationNames 'Création de la liste des configurations
For i = 0 To UBound(vConfNameArr) 'Boucle la liste : de l'élément 0 jusqu'au nombre d'élément dans la liste (Ubound)
    If InStr(UCase(vConfNameArr(i)), "SM-FLAT-PATTERN") = 0 Then 'Vérification si Flat Pattern est dans le nom de la configuration
        Set config = swModel.GetActiveConfiguration
        Set cusPropMgr = config.CustomPropertyManager
        sConfigName = vConfNameArr(i) 'Recupère l'élément N°i de la liste
        bShowConfig = swModel.ShowConfiguration2(sConfigName) 'Affiche la configuration
        Error = cusPropMgr.Get5("TYPE", True, Value_, ResolvedValOut, wasResolved) 'Récupère la valeur de la proriété "" dans la variable "Value_"
        bRebuild = swModel.ForceRebuild3(False) 'Reconstruction du modèle
        NewFilePath = Left(FilePath, InStrRev(FilePath, "\")) & "" & (sConfigName) & ".DXF" 'Remplace le nom par Type + Lg + Nom de la config (sans Flat pattern).dxf
        dataAlignment(0) = 0#
        dataAlignment(1) = 0#
        dataAlignment(2) = 0#
        dataAlignment(3) = 1#
        dataAlignment(4) = 0#
        dataAlignment(5) = 0#
        dataAlignment(6) = 0#
        dataAlignment(7) = 1#
        dataAlignment(8) = 0#
        dataAlignment(9) = 0#
        dataAlignment(10) = 0#
        dataAlignment(11) = 1#
        varAlignment = dataAlignment
        swPart.ExportToDWG2 NewFilePath, FilePath, swExportToDWG_ExportSheetMetal, True, varAlignment, False, False, 13, Null
    End If
Next i 'Passe à la prochaine config

End Sub

 

I'm sorry but the macro doesn't generate the unfolded configurations, here is my piece with the my macro. Look at the configurations before you launch it and you'll understand; I would like this one to add the fold line in the DXF


lot_de_cornieres.sldprt
export_dxf_famille_de_piece_-_ac_cobra.swp

Hello

I have the impression that we don't understand each other or the macro doesn't work the same way on my computer and yours.

This line: swPart.ExportToDWG2 NewFilePath, FilePath, swExportToDWG_ExportSheetMetal, True, varAlignment, False, False, 13, Null

Exports the unfolded by adding the fold lines.

Below are the results according to the macros used.


1_-_30x30x2_lg_1825_x120-macro_ac_cobra.jpg
1_-_30x30x2_lg_1825_x120-macro_modifie.jpg

Yes, the developed ones are ok, but when you launch the one I use, the unfolded configurations are automatically created in the room, hence my previous post. If you look at my configurations and you launch my macro, you will see the unfolded configurations being created, whereas in the one you created it is not.

Hello

Ok we didn't understand each other so we just have to pass the argument 1 to 0 in the line bRet = swModel.ExportFlatPatternView(NewFilePath, 1).

The revised code below:

Option Explicit

'Déclarations :
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim config                  As SldWorks.Configuration
Dim cusPropMgr              As SldWorks.CustomPropertyManager
Dim i                       As Long
Dim PathSize                As Long
Dim Error                   As Long
Dim bShowConfig             As Boolean
Dim bRebuild                As Boolean
Dim bRet                    As Boolean
Dim wasResolved             As Boolean
Dim FilePath                As String
Dim PathNoExtension         As String
Dim NewFilePath             As String
Dim Value_                  As String
Dim ResolvedValOut          As String
Dim sConfigName             As String
Dim vConfNameArr            As Variant
Sub main()

Set swApp = CreateObject("SldWorks.Application") 'Lancement de SW
Set swModel = swApp.ActiveDoc 'Récuperation du modèle actif dans SW
FilePath = swModel.GetPathName 'Récupère le chemin du fichier SW
PathSize = Strings.Len(FilePath) 'Compte le nombre de caractères du chemin
PathNoExtension = Strings.Left(FilePath, PathSize - 6) 'Récupère le nom de la pièce en enlevant .Sldrt

vConfNameArr = swModel.GetConfigurationNames 'Création de la liste des configurations
For i = 0 To UBound(vConfNameArr) 'Boucle la liste : de l'élément 0 jusqu'au nombre d'élément dans la liste (Ubound)
    Set config = swModel.GetActiveConfiguration
    Set cusPropMgr = config.CustomPropertyManager
    sConfigName = vConfNameArr(i) 'Recupère l'élément N°i de la liste
    bShowConfig = swModel.ShowConfiguration2(sConfigName) 'Affiche la configuration
    Error = cusPropMgr.Get5("TYPE", True, Value_, ResolvedValOut, wasResolved) 'Récupère la valeur de la proriété "" dans la variable "Value_"
    bRebuild = swModel.ForceRebuild3(False) 'Reconstruction du modèle
    NewFilePath = Left(FilePath, InStrRev(FilePath, "\")) & "" & (sConfigName) & ".DXF" 'Remplace le nom par Type + Lg + Nom de la config (sans Flat pattern).dxf
    bRet = swModel.ExportFlatPatternView(NewFilePath, 0) 'Export le déplié
Next i 'Passe à la prochaine config

End Sub

 

1 Like

Hello @Cyril.f,

 

sorry for the delay but I just came back from vacation; I tested it and it works perfectly. Thank you very much for taking the time :-) for my problem.