DXF Macro Coin Export

Hello I tried for a long time to create a simple macro to export in DXF the current view of my part (.part), no matter how much I try to use exporttoDWG2 I never manage to get my maccro to work, I start from far in VBA also I tried to hack several code that I found on other topics but nothing helps. and ChatGPT did not manage to meet my need, even though it seems simple.

Can't wait to read you the forum engineers :slight_smile:

ChatGPT ... But what do you all have with this thing now? It's never just a robot that copies and pastes bits of text that it sees. Brief

Have you tried to make a macro by learning by recording your movements when creating a DXF? It could allow you to see the basic functions that are called for your action

1 Like

It's a Gpt tool, it's much more than that in my opinion but yes it takes this information from a large database. in short²

Yes I started by doing this but even if I make a DXF of my part it outputs a code, I try again with the same part by deleting the DXF done beforehand, it launches but seems to do no action, and no DXF creation in output...

' ******************************************************************************
' macro recorded on 04/21/23 by CAO21
' ******************************************************************************
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("mon chemin.DXF", 0, 2)

' Redraw
Part.GraphicsRedraw2
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 22
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\templates\Mise en plan.drwdot", 0, 0, 0)
swApp.ActivateDoc2 "Mise en plan25 - Feuille1", False, longstatus
Set Part = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
myModelView.FrameLeft = 0
myModelView.FrameTop = 22
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
swApp.ActivateDoc2 "nom piece", False, longstatus
Set Part = swApp.ActiveDoc

' New View
Part.NameView "*En cours"
Dim myView As Object
Set myView = Part.CreateDrawViewFromModelView3("cheminpiece", "*En cours", 0, 0, 0)
boolstatus = Part.DeleteNamedView(*En cours)
Part.ClearSelection2 True
End Sub

here's the code that it comes out to me... and when I run it afterwards I get a synthaxis error on

boolstatus = Part.DeleteNamedView(*En cours)

Hello
For the syntax error, it's due to forgetting the quotation marks around *In progress. Without the quotation marks, it considers it to be a variable, so since it does not exist, or is empty, it returns an error.

2 Likes

Hello Cyril thank you for your answer, it solved the synthaxe error nevertheless the problem moves a little lower, when I launch the macro I have the message
next:
image

The line

Set myView = Part.CreateDrawViewFromModelView3("chemin de ma pièce.SLDPRT", "*En cours", 0, 0, 0)

is now highlighted,

When I run the macro, a blank drawing also opens.

Hello
After analysis, the macro is in my opinion not made for the need.
We should rather look at this code and adapt it if necessary.

Where can I find that? Copy public_documents \samples\tutorial\api\2012-sm.sldprt to ' c:\temp . ’

Normally this is in the public Documents folder of Windows.
C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS XXXX\samples\tutorial

Hello @leon.c

The attached macro should be able to save the current view of a part in DXF or DWG format.
The document must be open and document active.
Note that surface bodies are not taken into account in the SolidWorks export function.

Kind regards
PartToDxfDwg.swp (76.5 KB)

3 Likes

EDIT: it works perfectly well on 2 of the 3 stations, on the 3rd the macro works, but then impossible to create a new room, the problem persists until the opening of another room. Any idea where this bug could come from? (This is a detail, given that at the DXF creation stage we are theoretically no longer in a phase of creating new parts)

Hello

I have this same behavior on my PC, the menu associated with the creation function does not respond after running the macro.
By following the step-by-step execution, the creation of a new part is possible throughout the macro, up to the ExportToDWG2 function, and becomes impossible after...
It seems to be the cause of the problem, and I have no idea how to get around it.

2 Likes

It seems that this GPT didn't structure the code well, nevertheless it showed a good logic of {think}, create a mep of the current view and then export to dxf, not bad,

Option Explicit

Dim swApp As Object
Dim swModel As ModelDoc2
Dim swdrawing As DrawingDoc
Dim status As Boolean
Const modelPath As String = "C:\ProgramData\SOLIDWORKS\SOLIDWORKS 2018\templates\Drawing.drwdot"
Const nameView As String = "Current"

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If Not (swModel.GetType() = 1 Or swModel.GetType() = 2) Then
        MsgBox "Fonctional only on  'Part' or 'Assembly' documents.", vbCritical
        End
    End If
    Dim path As String
    path = swModel.GetPathName
    On Error GoTo err
    swModel.nameView nameView
    Set swdrawing = swApp.NewDocument(modelPath, 0, 0, 0)
    swdrawing.CreateDrawViewFromModelView3 path, nameView, 0, 0, 0
    Set swModel = swApp.ActiveDoc
    path = Left(path, InStr(1, path, ".")) + "dxf"
    Dim error As Long, warning As Long
    status = swModel.Extension.SaveAs(path, 0, 1, Nothing, error, warning)
    swApp.CloseDoc ""
    Set swModel = swApp.ActiveDoc
    swModel.DeleteNamedView nameView
    If status = False Then GoTo err
    MsgBox "Success", vbInformation
Exit Sub
err:
    MsgBox "Error in processing", vbCritical
End Sub

He would have been able to guess that the name (*In progress) belongs to the view palette, not to the template :grin:

Maybe because of the table of views, declared for 2 views but only 1 is defined,

Hello
I think it's more related to the fact of not emptying objects from memory.
The form remains active at home but doesn't prevent me from at least creating a new file (I haven't tried to do anything in it).
I think I should add "Set xxx = Nothing" at different points in the macro.

1 Like

@m.blt with this macro I don't have any problems after the export for the creation of new parts but I had the problem with another one, I didn't have time to look into it to see the difference between the two but if it helps.

DXF with bending line.swp (36 KB)