Macro API - DXF Export of Volume Parts (without Flat-Patern)

Hello

I use the method: ExportFlatPatternView Method (IPartDoc) a lot

https://help.solidworks.com/2019/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.ipartdoc~exportflatpatternview.html

to export sheet metal developed to be sent to plasma/laser/water jet...

 

On the other hand, we sometimes draw simple parts (let's imagine a simple washer) in volume and not in sheet metal.

This method used on a solid part exports the part well as expected but always taking the original front view of the part.

So if we draw the washer on the front view, it's OK if we draw it on the side view, we will have a DXF of the edge of the washer (a long rectangle and not two concentric circle).

If we update the view to say that this is "this" the front view, it takes it into account in SW (we click on front view we have the right view) but for the export it doesn't change anything.

 

Do you have an idea to be able to export the right eyesight every time?

Or another technique that allows you to export DXF parts for NC cutting without having to go through a Drawing.

(indeed the method we use so far is to make a drawing on a 1:1 scale without a cartridge and then export it in DXF but it's not at all efficient).

Thank you in advance for your advice on this subject.

Rafael

Haaa I just saw that there are in fact:

https://help.solidworks.com/2019/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IPartDoc~ExportToDWG2.html

and that annotation views can be exported.

I'm looking at what we can do with it and I'll post the results here.

1 Like

Hello

Yes it's the ExportToDWG2 function that you should use for what you want to do, this function replaces the ExportFlatPatternView function since the 2017 version of SW.

Kind regards

Hello

Here's my code:

je le met dans un post suivant car on est limité à 6000 caractères sur le Forum

and output:

So two problems:

1- If I don't define a view in dataView(0), the volume export works, if I define one in the following way:

    'Variable pour l'export DXF/DXG
    Dim varViews As Variant
    Dim dataViews(2) As String
    
    'Vues d'annotation que l'on veux exporter en DXF
    dataViews(0) = "*Current"
    dataViews(1) = "*Front"
    dataViews(2) = "*DXF"
    varViews = dataViews

 

So my export doesn't work anymore...

 ElseIf flagTole = 0 Then 'Si on est en volumique:
                                    'value = instance.ExportToDWG2(FilePath, ModelName, Action, ExportToSingleFile, Alignment, IsXDirFlipped, IsYDirFlipped, SheetMetalOptions, Views)
                                    varViews = dataViews
                                    swPart.ExportToDWG2 dxfFilePath, filePath, swExportToDWG_ExportAnnotationViews, False, varAlignment, False, False, 0, varViews
                                    bRet = swPart.ExportToDWG2(dxfFilePath, filePath, swExportToDWG_ExportAnnotationViews, True, varAlignment, False, False, 0, varViews)
                                    If bRet Then
                                        Debug.Print "On a exporté la pièce de volumique en DXF:  " & dxfFilePath
                                    Else
                                        Debug.Print ("Erreur enregistrement DXF: " & dxfFilePath)
                                    End If
                                End If 'Fin du If flagTole

swPart.ExportToDWG2 --> returns FALSE

Even if I only put a front view...

My goal is to create an annotation view called "DXF" in the volume parts that we want to send to cut. Indeed, some are drawn in the context of the assembly and are therefore not centered on the origin, nor on the basic plans and in this case it is appropriate for the draughtsman to choose the view he uses for the cutting. A bit like when you do "Export face" in DXF.

I can't use: swExportToDWG_ExportSelectedFacesOrLoops  because I don't think I can find the right face automatically.

(unless I can automatically select the face that has the largest surface area.... but hey it's not great and it may be time-consuming)

 

2- For sheet metal parts, I export each configuration well, it works cool. On the other hand, I would like to be sure that it uses the geometry of the part in its developed state with the functions that we have associated with the developed state (in the unfolded state folder). Because small modifications are often made only for cutting, for example:

- Small fold marks are added to the end of the folds

- We hide some faces, such as the countersinks that we do in rework, otherwise we end up with a dxf with two concentric circles, and the goal is to have only closed contours.

In my code posted above, I was going through the configurations to find the FLATs, but the sheet metal parts don't have a FLAT configuration until you create the drawing. The use of swExportToDWG suits me well because even if the part doesn't have a flat pattern config, the export is done well.

 

There you have it, your advice is welcome on the other parts of the code as well.

Thank you very much!

Here is a code snippet completely truncated/degreased because we must be less than 600char:

I have left only the parts that interest us for this subject:

Option Explicit

Public Enum swSMBendState_e
    swSMBendStateNone = 0       '  No bend state - not a sheet metal part
    swSMBendStateSharps = 1     '  Bends are in the sharp state - bends currently not applied
    swSMBendStateFlattened = 2  '  Bends are flattened
    swSMBendStateFolded = 3     '  Bends are fully applied
End Enum

Public Enum swSMCommandStatus_e
    swSMErrorNone = 0               '  No errors
    swSMErrorUnknown = 1            '  Failed for an unknown reason
    swSMErrorNotAPart = 2           '  Sheet metal commands only apply to SolidWorks parts
    swSMErrorNotASheetMetalPart = 3 '  Part contains no sheet metal features
    swSMErrorInvalidBendState = 4   '  Invalid bend state was specified
End Enum


Sub main()

    'Objet SW
    Dim swApp               As SldWorks.SldWorks
    Dim swModel             As SldWorks.ModelDoc2
    Dim swPart              As SldWorks.PartDoc
    Dim swExportPDFData     As SldWorks.ExportPdfData
    Dim swView              As ModelView

    Dim flagTole            As Long
        
    'Bitmasq pour régler l'export DXF
    Dim exportFlatPat As Long
    Dim dataAlignment(11) As Double
    Dim varAlignment As Variant
    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

    'Variable pour l'export DXF/DXG
    Dim varViews As Variant
    Dim dataViews(2) As String
    
    'Vues d'annotation que l'on veux exporter en DXF
    dataViews(0) = "*Current"
    dataViews(1) = "*Front"
    dataViews(2) = "*DXF"
    varViews = dataViews
    
    'On crée un instance de SW
    Set swApp = CreateObject("SldWorks.Application")

######### TRONQUE ICI car le forum refuse quand c'est trop long ##########

                If flagAsm Or flagPart Then
                
                    'et qu'on veut un DXF
                    If flagPart And flagDXF Then
                        flagTole = swModel.GetBendState
                        If flagTole = 0 Then
                            Debug.Print "La pièce est volumique:  " & flagTole
                        Else
                            Debug.Print "La pièce est en tôlerie:  " & flagTole
                        End If
                    End If
                
                    'On récupère le nom des configs
                    vConfNameArr = swModel.GetConfigurationNames
                    
                    'Pour chaque config : ---------------------------------------------
                    For i = 0 To UBound(vConfNameArr)
                        sConfigName = vConfNameArr(i)
                        Debug.Print "On traite la config:  " + sConfigName
                        
                        
                        'Pour chaque config pas FLAT : ---------------------------------------------
                        If InStr(sConfigName, "FLAT") = 0 Then
                            bShowConfig = swModel.ShowConfiguration2(sConfigName)
                            
                            
                            'Export DXF
                            If flagPart And flagDXF Then
                                dxfFilePath = exportPathNoExtention & "-" & sConfigName & ".DXF"
                                Set swPart = swModel
                                If flagTole <> 0 Then 'Si on est en tôlerie:
                                    exportFlatPat = 1
                                    bRet = swPart.ExportToDWG2(dxfFilePath, filePath, swExportToDWG_ExportSheetMetal, True, varAlignment, False, False, exportFlatPat, Null)
                                    If bRet Then
                                        Debug.Print "On a exporté la pièce de tôlerie en DXF:  " & dxfFilePath
                                    Else
                                        Debug.Print ("Save DXF : " & bRet)
                                    End If
                            
                                ElseIf flagTole = 0 Then 'Si on est en volumique:
                                    varViews = dataViews 'ici j'ai remis ca juste pour test mais c'est déjà fait plus haut, ou est le meilleur endroit ?
                                    'Reference de la fonction pour mémoire: value = instance.ExportToDWG2(FilePath, ModelName, Action, ExportToSingleFile, Alignment, IsXDirFlipped, IsYDirFlipped, SheetMetalOptions, Views)
                                    bRet = swPart.ExportToDWG2(dxfFilePath, filePath, swExportToDWG_ExportAnnotationViews, False, varAlignment, False, False, 0, varViews)
                                    If bRet Then
                                        Debug.Print "On a exporté la pièce de volumique en DXF:  " & dxfFilePath
                                    Else
                                        Debug.Print ("Erreur enregistrement DXF: " & dxfFilePath)
                                    End If
                                End If 'Fin du If flagTole
                                
                            End If 'Fin du if flagPart et FlagDXF
                        

            

 

Hello

Point 1: Yes, it's not obvious on the examples found in the help, but to export the views, you have to name them exactly as they are in the palette of drawings, so for example:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim sModelName As String
Dim sPathName As String
Dim varAlignment As Variant
Dim dataAlignment(11) As Double
Dim varViews As Variant
Dim dataViews(10) As String
Dim options As Long

Sub main()

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

    sModelName = swModel.GetPathName
    sPathName = swModel.GetPathName
    sPathName = Left(sPathName, Len(sPathName) - 6)
    sPathName = sPathName + "dxf"

    Set swPart = swModel

    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) = "*Face"
    dataViews(1) = "*Dessus"
    dataViews(2) = "*Dessous"
    dataViews(3) = "*Droite"
    dataViews(4) = "*Gauche"
    dataViews(5) = "*Arrière"
    dataViews(6) = "*Isométrique"
    dataViews(7) = "*Dimétrique"
    dataViews(8) = "*Trimétrique"
    dataViews(9) = "*En cours"
    dataViews(10) = "dxf"

    varViews = dataViews

    swPart.ExportToDWG2 sPathName, sModelName, swExportToDWG_ExportAnnotationViews, False, varAlignment, False, False, 0, varViews

End Sub

We can see that the view named dxf does not have the * sign in front of its name and that the other views are in French.

Point 2: I didn't look ... [EDIT] you can try by just adding the export of a view named "Unfolded State" so by adding a line dataViews(11) = "Unfolded State" without forgetting to modify the line "Dim dataViews(11) As String" [END EDIT]

Kind regards