Export Solidworks model views to dxf file layers instead of sheets

Anyone know of a way to export Solidworks model views into a single dxf file with each view into different layers? The default is exported into different sheets but I need them into layers.

Or something like a macro to convert the dxf sheets into layers after it is exported.

Never tried it but here is a thought.

Create a drawing template with all the desired views (pre defined views). Put them each on their individual layer. Now create the drawing for the model using that template and export as DXF. If this works then it can be easily automated using a macro in case you have to do it repeatedly.

I am not sure that it is even possible from the part/assembly level, even with a macro (because in the custom map file, there is no option for view (even at the drawing level)

I played around with it, but I couldn’t figure out how to map each view…

You might be able to add each view to a drawing, change the line type or color of each drawing view, then map the different line types/colors to layers in the map file. But I am not sure if this will be less work that the following option.

You might have to use a 3rd party software to do it. Or open the DXF inside Solidworks and then add each view to layers, then resave the DXF file.

Thanks, will try the drawing template and play around.

I just tested the template method and it works.
Views on Layer DXF.PNG

I was not able to change the different views of the drawing into layers, could only put all the views into the same layer. Is there a way to do this without converting the views to blocks/sketches?

That is correct, you can change the layer of a specific component that is shown in the drawing, but it changes it for every view that component is shown in.

I stand corrected. You CAN do it. see post below by AlexB

I thought that you could select the edges of a part and change the layer of individual edges, but it doesn’t look like you can do that.

You CAN change the view to a sketch, and then change the layers that way. But this breaks the link to the 3D part.

Yes you can do it.

How?

Every time I tried to change a component to a different layer, it changed it for every single view. I even toggling layer for a specific view didn’t do anything, just changed the active layer.

If you edit the component line font for the item shown in the view, you can select the layer and check “From Selection” to apply it to only the selected item in the selected view. It will move all of it’s items to that layer prior to export.


After export to dxf (GIF, click to view)
Views On Different Layers.gif

I stand corrected! Thank you!

Thank you for adding the images/explanation. I replied from the phone and didn’t have SW in front at that moment.

Update.

I couldn’t find the API to change the layer for the Component Line Font but was able to do it from this example:
https://help.solidworks.com/2016/english/api/sldworksapi/put_assembly_components_in_drawing_view_on_different_layers_example_vb.htm

Here is what came up with:
*created the drawing templates with predefined views
*macro to take active model part and insert model into all the views of the drawing template
*save the file as DXF and close the drawing

Thanks for the help!

Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main()
Dim boolstatus As Boolean
Dim swDrawing As SldWorks.DrawingDoc
Dim selMan As SldWorks.SelectionMgr
Dim drwView As SldWorks.View
Dim swDrawComp As SldWorks.DrawingComponent
Dim sRevision As String
Dim fileName As String
Dim longstatus As Long

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

'drawing template with predefined views
Set swDrawing = swApp.NewDocument(“I:\Solidworks\DXF_LAYERS.drwdot”, swDwgPaperSizes_e.swDwgPaperBsize, 0, 0)

'checks if active doc is a part
If (swModel.GetType <> swDocPART) Then GoTo CLEAN_UP
'inserts the current active model into the drawing template
swDrawing.InsertModelInPredefinedView swModel.GetPathName()

Set selMan = swDrawing.SelectionManager
'selects view and change view part layer
boolstatus = swDrawing.Extension.SelectByID2(“Drawing View1”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “FRONT”

boolstatus = swDrawing.Extension.SelectByID2(“Drawing View2”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “TOP”

boolstatus = swDrawing.Extension.SelectByID2(“Drawing View3”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “RIGHT”

boolstatus = swDrawing.Extension.SelectByID2(“Drawing View4”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “ISO”

boolstatus = swDrawing.Extension.SelectByID2(“Drawing View5”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “LEFT”

boolstatus = swDrawing.Extension.SelectByID2(“Drawing View6”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “BOTTOM”

boolstatus = swDrawing.Extension.SelectByID2(“Drawing View7”, “DRAWINGVIEW”, 0, 0, 0, False, 0, Nothing, 0)
Set drwView = selMan.GetSelectedObject6(1, 0)
Set swDrawComp = drwView.RootDrawingComponent
swDrawComp.layer = “BACK”

'clear any selection
swModel.ClearSelection2 True

'fileName for dxf out put
fileName = swModel.GetPathName
fileName = Left(fileName, InStrRev(fileName, “.”) - 1) & “.dxf”

’ Save to dxf and close drawing
Set swModel = swDrawing
longstatus = swModel.SaveAs3(fileName, 0, 2)
swApp.QuitDoc (swModel.GetTitle)

'Clean up files
CLEAN_UP:
Set drwView = Nothing
Set swDrawing = Nothing
Set swModel = Nothing
End Sub

I’m sure that this macro can be refined further to work without pre defined views and layers template. Will try my hands when I get some free time.

I stumbled onto this question while looking for something else. Putting drawing views on different layers is possible through the API. At one time you could even put detail views on a different layer. Been working since SW2020.

High level steps:
Set swDrawComp = swView.RootDrawingComponent ’ get the component
intLayer = swLayerMgr.AddLayer(sLyrName, swView.Name + " Layer", BlackRGB, 0, 0) 'create a layer for it
swDrawComp.Layer = sLyrName 'set it to the layer
swDrawCompChild.Layer = sLyrName 'set all children of the view to same layer

If you want to provide a drawing and model to test what it looks like I can provide an example dxf of it.