Export assembly to DXF

Hello

We would need to export assembly files to DXF (1:1 scale)
No mass export but in the idea, it would rather be an export under design

First problem, as you may know, it is impossible to directly export an assembly to DXF (at least to my knowledge in 2017)
We may have the possibility to make a macro, to export to a part and then export to DXF, here second problem because we have no knowledge about macros

At our disposal, we have myCADtools or myPDMtools
But we don't really know all the capabilities of these tools.

Do you have any leads to give?
Thanks in advance,
Séb 

Hello

I found a long time ago this macro on the forum that saves all the files of an assembly in PDF; If you know what you are doing you can adapt 


pdf_des_composants_de_lassemblage.swp

Thank you ac cobra 427 for your file
But I can't launch it, nothing runs I have the impression, no PDF file is generated
And in terms of macro editing, I'm a marble

Edit: I just understood that you need a drw plan already created
As a result, the macro executes

It is not possible to export an assembly to a dxf, it is necessary to go through a drawing with the choice of views to be exported.

Of course, the MEP will have to be re-registered as it progresses.

Not sure that a macro will save you a lot of time on the issue. It's as quick to put down your 3 views and export yourself ^me as it is to press a button and let the macro do its thing.

For the MyCad tool, same thing, not really of interest for the subject

Hello

I have a macro that DXFs parts and assemblies.

All you have to do is create an empty background (path to be indicated in the macro) and the macro records in dxf at scale 1.

I hope this will answer your request

a+


dxf.txt
1 Like

Thank you very much gwygwy
This is already a tremendous step forward!

DXF from assembly generates correctly
I just have to get to position the assembly in 0,0,0 (by necessarily the origin of the 3D)
And to rename the file

Is it possible from a macro to have a dialog box to write the name of the desired file? or retrieve the name of a SW property of the file?

1 Like

Hello

A small example HERE to retrieve a custom property...

Otherwise for a dialog box that is waiting for a response:

Dim maValeur As String
maValeur = InputBox("Ma question ?", "Mon titre", "Ma valeur par défaut")

Kind regards

Thank you d.roger for the complement,

So if I want the generated DXF to be named by "myValue"
Do I need to modify the Part.SaveAs block in the macro sent by gwygwy ?

    
'--- Saving the dxf and closing the temporary
    MEP Part.SaveAs PathNoExtension & ". DXF"
    Part.ClearSelection2 True
    Set Part = swApp.ActiveDoc
    swApp.CloseDoc swDraw.GetTitle 'Quits without backing up the MEP.

 

 .

I am also having difficulty positioning my assembly in 0,0,0
By default, the origin of the DXF is always in the center of my assembly

Yes, you need to modify the Part.SaveAs block to recalculate the PathNoExtension variable.

A small example:

Dim maValeur As String
maValeur = InputBox("Ma question ?", "Mon titre", "Ma valeur par défaut")
    
Dim FilePath As String
Dim FileTitle As String
Dim PathSize As Long
Dim TitleSize As Long
Dim PathNoExtension As String
FilePath = Part.GetPathName
FileTitle = Part.GetTitle
PathSize = Strings.Len(FilePath)
TitleSize = Strings.Len(FileTitle)
PathNoExtension = Strings.Left(FilePath, PathSize - TitleSize)

MsgBox PathNoExtension & maValeur & ".dxf"

Kind regards

Thank you very much d.roger

Running the macro from an assembly,
I replaced the Part with sw.model

FilePath = Part.GetPathName
FileTitle = Part.GetTitle

The result is really not bad,
All that remains is to position the whole thing in 0,0,0
And to record the DXF in a file in raw and not to the impalement of the 3D

What do you mean by "All you have to do is position the whole thing in 0,0,0"? A screenshot of what you get and what you would like would be welcome.

And to save the dxf in a raw folder you have to rework the variable "PathNoExtension" as you wish.

On the left, dxf from the macro
A right, manually modified dxf with repositioning of the projection on the origin

Regarding the path, I scratched a little and it's ok;)


origine.png

OK, so let's see if something like the following suits you:

Option Explicit

Sub main()

    Dim swApp           As SldWorks.SldWorks
    Dim swModel         As SldWorks.ModelDoc2
    Dim swDraw          As SldWorks.DrawingDoc
    Dim swView          As SldWorks.View
    Dim retval          As String
    Dim FilePath        As String
    Dim FileTitle       As String
    Dim PathSize        As Long
    Dim TitleSize       As Long
    Dim PathNoExtension As String
    Dim maValeur        As String

    Set swApp = CreateObject("SldWorks.Application")
    Set swModel = swApp.ActiveDoc

'--- Création du nom du fichier
    FilePath = swModel.GetPathName
    FileTitle = swModel.GetTitle
    PathSize = Strings.Len(FilePath)
    TitleSize = Strings.Len(FileTitle)
    PathNoExtension = Strings.Left(FilePath, PathSize - TitleSize)
    
    maValeur = InputBox("Veuillez indiquer le nom du fichier dxf ?", "Macro Asm To DXF", "MonFichier")

    PathNoExtension = PathNoExtension & maValeur & ".dxf"

'--- Calcul du décalage de l'origine
    Dim vBox                    As Variant
    Dim swAssy                  As SldWorks.AssemblyDoc
    Dim X_max                   As Double
    Dim X_min                   As Double
    Dim Y_max                   As Double
    Dim Y_min                   As Double
    Dim Z_max                   As Double
    Dim Z_min                   As Double

    If swModel.GetType() <> swDocASSEMBLY Then Exit Sub
    
    Set swAssy = swModel
    vBox = swAssy.GetBox(swBoundingBoxIncludeRefPlanes)

    X_max = vBox(3)
    X_min = vBox(0)
    Y_max = vBox(4)
    Y_min = vBox(1)
    Z_max = vBox(5)
    Z_min = vBox(2)

    Dim decalX As Double
    Dim decalY As Double
    decalX = (X_max - X_min) / 2
    decalY = (Y_max - Y_min) / 2
    
'--- Création de la mise en plan
    retval = swApp.GetUserPreferenceStringValue(swDefaultTemplateDrawing)
    Set swDraw = swApp.NewDocument(retval, 0, 0, 0)

'--- Insertion de la vue
    Set swView = swDraw.CreateDrawViewFromModelView3(swModel.GetPathName, "*Face", decalX, decalY, 0)
    
'--- Rend invisible les annotations de plis
    swView.ShowSheetMetalBendNotes = False

'--- Force la vue à l'échelle 1:1
    Dim swSheet As Sheet
    Dim status As Boolean
    Set swView = swDraw.GetFirstView
    Set swSheet = swDraw.GetCurrentSheet
    status = swSheet.SetScale(1, 1, True, True)
    
'--- Sauvegarde du dxf et fermeture de la MEP temporaire
    swDraw.SaveAs PathNoExtension & ".DXF"
    swDraw.ClearSelection2 True
    swApp.CloseDoc swDraw.GetTitle

End Sub

I didn't handle saving the dxf file in another folder...

Kind regards

Hello

@sjoly_TRI

I'm glad my macro helped you move forward

A+

Thank you d.roger
A repositioning is done but it's not well set to x0 y0 (CF image)

I admit that I don't understand the principle of decalX and decalY with the xMax xMin (...)


2020-10-27_16_02_01-draftsight_enterprise_-_monfichier1.dxf_.png

Finally, I just found the reason,
We have a skeleton of shots that are out of 3D
And the conversion with the Xmax Xmin... take into account the plans

 


2020_10_27_16_16_58_solidworks_premium_2017_x64_edition_r_a.106568.sldasm_lecture_seule_.png

The principle is to calculate the dimensions of the "BoundingBox" of the assembly and to move the view by half of them since by default the origin of the view is the center of the assembly.

Try replacing the line:

vBox = swAssy.GetBox(swBoundingBoxIncludeRefPlanes)

by it:

vBox = swAssy.GetBox(False)

whenever you have a plan that would get in the way...

Kind regards

Daniel

Indeed, it is better this way
Thank you very much to all of you for the time spent and for your help!

1 Like