DXF/DWG Model View Export

Library for macros
User avatar
MacroEnjoyer
Posts: 13
Joined: Thu Apr 04, 2024 9:20 am
Answers: 0
Location: Manchester, England
x 13
x 3

DXF/DWG Model View Export

Unread post by MacroEnjoyer »

Hi Everyone,

I've been setting up my macros for my new job and encountered an issue. I can't seem to find a macro that exports either dwg or dxf files from the model.

I managed to find one that exports the top view, but if the part was drawn on another plane it's incorrect for use on the CNC (Attached). I also found one that exports the selected sketch but if there's a cut-out (i.e. 2 sketches total) that's not overly useful. I also found that I can't use one that's meant for drawings as 1. Not every part has a drawing 2. The scale can sometimes be wrong (Not sure why).

It would be great if someone could point me in the right direction. Essentially here's my checklist of requirements:

- Export dwg or dxf from model.
- Takes the current model view so that part orientation doesn't matter.
- Renames the file the same as the model.

Perhaps another useful addition could be a dialog box that allows you decide the file type so that one macro can be used for 2 functions. Not sure how to implement this.
Attachments
ConvertDXF.swp
(35 KiB) Downloaded 95 times
Thanks,

ME
User avatar
bnemec
Posts: 1944
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2542
x 1400

Re: DXF/DWG Model View Export

Unread post by bnemec »

Sorry for dumb question, but what is the purpose of the dwg/dxf from the model? What is consuming the data?
User avatar
SPerman
Posts: 2055
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2225
x 1874
Contact:

Re: DXF/DWG Model View Export

Unread post by SPerman »

check out the "SaveAs3" method, and chose "dxf" as the extension. I think it will do what you want.

https://help.solidworks.com/2021/englis ... veAs3.html

Running it manually (File, Save As) you get these options:
image.png
My recommendation would be to change your workflow. Create drawings / views that are specific to the information you are trying to communicate, and export those as DWG/DXF.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
User avatar
MacroEnjoyer
Posts: 13
Joined: Thu Apr 04, 2024 9:20 am
Answers: 0
Location: Manchester, England
x 13
x 3

Re: DXF/DWG Model View Export

Unread post by MacroEnjoyer »

Cheers for the quick reply, good questions.

Basically, the reason for wanting model views instead of drawing is to do with scale. In the past when I've exported from drawing, if the scale for the sheet isn't set to 1:1, it gets confused and is the wrong size. Again, this is obviously something I could change manually. When saving from the model, this issue seems to be solved without intervention.

Also, I'm only using this tool as a way to send perimeter data to the CNC operator, so there is no real detail needed such as the drawing template. That's another reason why I don't need to export from the drawing. As I would need to go in to the file and delete lines as required.
Thanks,

ME
User avatar
AlexB
Posts: 499
Joined: Thu Mar 18, 2021 1:38 pm
Answers: 28
x 267
x 444

Re: DXF/DWG Model View Export

Unread post by AlexB »

Macros for exporting to flat patterns can get a bit complicated. In the past, I've approached this by creating a drawing and hiding/removing all of the title block information, border, etc.

Then I add a view to the drawing using IDrawingDoc:CreateFlatPatternViewFromModelView3 to place my flat pattern automatically onto the sheet.

Then I set the scale 1:1 and call 'SaveAs' to get the dxf output from the drawing.

Obviously there is a fair bit of code to accomplish these steps depending on your use case, but it's proven to provide the most consistent outputs from my experience.
User avatar
bnemec
Posts: 1944
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2542
x 1400

Re: DXF/DWG Model View Export

Unread post by bnemec »

So this DXF is for CNC cutting, laser or waterjet, etc?

We have never used drawing (view)s for 2D CNC dxf; Solid Edge or Solidworks. They have always been saved from the model. This is for various reasons, including scaling as you mentioned. However, it has been my experience that the most popular way to save a dxf for CNC is to make a drawing view, so both ways work. Up to you to decide which way works for you.

The Save As method is what you're looking for but it's called "ExportToDWG2" now and it's a method of the PartDoc interface.

You'll want this:
https://help.solidworks.com/2021/englis ... 4wLjAuMA..

IIRC it uses the system options for for exporting dxf. A guide for setting those via API is here:
https://help.solidworks.com/2021/englis ... ptions.htm
I set those every time the task runs. This way I don't need to worry about getting them all correct if I fire up a different machine for task host. I like being able to use the layer map file.

As with anytime using the help pages, make sure you have your version of Solidworks selected.
User avatar
MacroEnjoyer
Posts: 13
Joined: Thu Apr 04, 2024 9:20 am
Answers: 0
Location: Manchester, England
x 13
x 3

Re: DXF/DWG Model View Export

Unread post by MacroEnjoyer »

Thanks. Alex these are not sheet metal parts, so flat patterns will rarely be used.

Bnemec, I'm not sure I'm 100% following as I'm relatively new in the design world. This is for Laser Cutting and CNC. I'll follow those guides and get back to you if I run into trouble. Thanks very much!
Thanks,

ME
User avatar
bnemec
Posts: 1944
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2542
x 1400

Re: DXF/DWG Model View Export

Unread post by bnemec »

MacroEnjoyer wrote: Fri Apr 05, 2024 3:47 am Thanks. Alex these are not sheet metal parts, so flat patterns will rarely be used.

Bnemec, I'm not sure I'm 100% following as I'm relatively new in the design world. This is for Laser Cutting and CNC. I'll follow those guides and get back to you if I run into trouble. Thanks very much!
If you're doing laser cutting then I would encourage you to use sheet metal. I'm assuming flat, 2 axis laser cutting of sheets of material. I don't' know anything about files for things like tube lasers, any other kind of >2axis machining I assume needs CAM with machine specific post processor, but I don't know. Again, I'm assuming two axis laser. For that, we use sheet metal bodies and let Solidworks export the flat pattern from the model.

Those API help links I posted are in the context of .NET add-ins or stand alone apps, but AFAIK the VB in macros uses same/similar function calls.
User avatar
Frederick_Law
Posts: 1947
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1638
x 1468

Re: DXF/DWG Model View Export

Unread post by Frederick_Law »

Do you want a 3D DXF/DWG?
Or 2d Views?
Laser cut use 2D.
CNC mill use 3D and usually STEP file, not DXF/DWG.
User avatar
DavidWS
Posts: 23
Joined: Fri Jul 28, 2023 11:33 pm
Answers: 1
x 26
x 12

Re: DXF/DWG Model View Export

Unread post by DavidWS »

There is a macro widely circulated that will save all sheet metaal flat patterns out to dxf, with some options for naming and other settings.

This is very useful as it will travers an assy or part and export all components if they are sheet metal. This saves much time in the need to open each part and export them manually.

It would be great if this macro would incorporate non-sheetmetal components if they have a view defined (i.e. DXF_VIEW) in the desired export orientation

I believe it's located here:
https://www.codestack.net/solidworks-ap ... -patterns/
len_1962
Posts: 85
Joined: Fri Apr 09, 2021 9:55 am
Answers: 1
Location: Mesa, Arizona
x 76
x 42
Contact:

Re: DXF/DWG Model View Export

Unread post by len_1962 »

Frederick_Law wrote: Fri Apr 05, 2024 11:10 am CNC mill use 3D and usually STEP file, not DXF/DWG.
Frederick,

No, not True, HAAS and HURCO have DXF\DWG options that use them to program toolpath at the control.

Also most CAM software now read native SolidWorks\SolidEdge\Inventor\NX ...... straight into the software, but most of the time you'll need to be current on the CAM to read new versions of the CAD files.
User avatar
MacroEnjoyer
Posts: 13
Joined: Thu Apr 04, 2024 9:20 am
Answers: 0
Location: Manchester, England
x 13
x 3

Re: DXF/DWG Model View Export

Unread post by MacroEnjoyer »

Bnemec - ok, noted. I'm not sure why using sheet metal would be beneficial as these parts are not bent or any other process. They are just flat parts which require cutting. Sorry if I'm not being clear but it's hard to explain without showing you. We sub contract metal parts, and just CNC wood and acrylic, so to me using the sheet metal feature on a non-steel part seems unnecessary.

Frederick - Just 2D dxf, as the cnc is three axis, and therefore i just need to define the outer perimeter of the part and any internal cut-outs.

I've been using ChatGPT to develop the macro I need, I'll share it here but i'm not sure how helpful it'll be since no one else seems to have the issues i'm having ! :)
Thanks,

ME
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: DXF/DWG Model View Export

Unread post by josh »

The VBA example for ExportToDWG2 already does exactly what you want with just a few tiny modifications.

https://help.solidworks.com/2024/englis ... ple_VB.htm
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: DXF/DWG Model View Export

Unread post by josh »

Code from example with tweaks made:

Code: Select all

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(0) 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) = "*Current"
    'dataViews(1) = "*Front"
    

    varViews = dataViews
    

    'Export each annotation view to a separate drawing file
    swPart.ExportToDWG2 sPathName, sModelName, swExportToDWG_ExportAnnotationViews, True, varAlignment, False, False, 0, varViews
    

    'Export sheet metal to a single drawing file
    'options = 1   'include flat-pattern geometry
    'swPart.ExportToDWG2 sPathName, sModelName, swExportToDWG_ExportSheetMetal, True, varAlignment, False, False, options, Null
 

End Sub
DLZ_SWX_User
Posts: 51
Joined: Mon Dec 20, 2021 1:40 pm
Answers: 0
Location: Thumb Area of Michigan, USA
x 177
x 27

Re: DXF/DWG Model View Export

Unread post by DLZ_SWX_User »

Here is a link that has a macro that we use to retrieve DXF file for laser cutting from our assemblies and/or multibody parts. We also create all our laser cut parts using the sheet metal feature rather then boss extrude. Even those without bends. The macro we use is in the 4th post down. I believe it is one created by @artem and modified by @Jaylin Hochstetler and @mike miller. It work great for us most of the time.

https://www.cadforum.net/viewtopic.php?p=746#p746
User avatar
MacroEnjoyer
Posts: 13
Joined: Thu Apr 04, 2024 9:20 am
Answers: 0
Location: Manchester, England
x 13
x 3

Re: DXF/DWG Model View Export

Unread post by MacroEnjoyer »

Okay, thanks everyone. Super helpful.
Thanks,

ME
User avatar
DavidWS
Posts: 23
Joined: Fri Jul 28, 2023 11:33 pm
Answers: 1
x 26
x 12

Re: DXF/DWG Model View Export

Unread post by DavidWS »

DLZ_SWX_User wrote: Wed Apr 10, 2024 10:57 am Here is a link that has a macro that we use to retrieve DXF file for laser cutting from our assemblies and/or multibody parts. We also create all our laser cut parts using the sheet metal feature rather then boss extrude. Even those without bends. The macro we use is in the 4th post down. I believe it is one created by @artem and modified by @Jaylin Hochstetler and @mike miller. It work great for us most of the time.

https://www.cadforum.net/viewtopic.php?p=746#p746
This is a great macro and we use it successfully in all our projects.

I am trying to modify it to also export parts that are NOT sheet metal, but contain a user defined view called DXF

I have, so far, been unsuccessful as I am using chatGPT to write the code (I cannot write macros yet), but it fails every time so far.
Post Reply