I’m trying export every layer as a separate SVG. I’ve found this working script that creates text on each layer..
' creates text of the layername on each layer (create a few layers and then run this.)
Sub LayerNamed()
Dim lyr As Layer
Dim n As Long
n = 0
For Each lyr In ActivePage.Layers
lyr.CreateArtisticText 0, n, lyr.Name
n = n + 1
Next lyr
End Sub
that works fine, but I can’t figure out how to get my export script to select and export ONLY the shapes on each layer and not ALL the shape..
I end up with “layer1.svg”, “layer2.svg” , “layer3.svg” BUT they have all of the layers. So every svg is the same. I can't work out the proper command to select only the layer and then export it etc..
Sub ExportSVG()
Dim TT As String
Dim OrigSelection As ShapeRange
Set OrigSelection = ActiveSelectionRange
Dim expopt As StructExportOptions
Set expopt = CreateStructExportOptions
expopt.UseColorProfile = False
Dim expflt As ExportFilter
Set expflt = ActiveDocument.ExportEx("C:\Temp\" & lyr.Name & ".svg", cdrSVG, cdrAllPages, expopt)
expflt.Finish
In the ActiveDocument.ExportEx command there is an "ExportRange" parameter which you have set to "cdrAllPages". Have a look at the API reference - you might want to use "cdrSelection" instead which should do the trick.
Your (working) code should be:
1 2 3 4 5 6 7 8 9 10 11 12
Sub ExportSVG() Dim lyr As Layer, expopt As StructExportOptions, expflt As ExportFilter For Each lyr In ActivePage.Layers lyr.Shapes.All.CreateSelection Set expopt = CreateStructExportOptions expopt.UseColorProfile = False Set expflt = ActiveDocument.ExportEx("C:\Temp\" & lyr.Name & ".svg", cdrSVG, cdrSelection, expopt) expflt.Finish Next lyr End Sub
I think it will also export a guides layer.If you do not need it you can exclude it (by name) inside the iteration...
You simply need to use Layer.Prinable to make a layer in a saved file visible or not.See the following example:
Sub ExportByLayer()Dim CurPg As Page, pg As PageDim CurLy As Layer, LastLy As Layer, DoOut As Boolean, HasLastLy As BooleanDim LayerList As String, TempArr() As StringDim i As Integer, j As IntegerDim ExpOpt As StructExportOptions, ExpFlt As ExportFilterSet CurPg = ActivePageLayerList = ""For Each CurLy In CurPg.Layers If CurLy.Printable Then 'This will store the attributes for later restoring If LayerList <> "" Then LayerList = LayerList + ";" LayerList = LayerList + CurLy.Name End If CurLy.Printable = False ' set all layers first to not printableNext CurLyHasLastLy = FalseFor Each CurLy In CurPg.Layers DoOut = Not CurLy.IsSpecialLayer ' you can also filter by layer names If DoOut Then If HasLastLy Then LastLy.Printable = False ' 'Hide' the previousely use layer CurLy.Printable = True 'make it 'Visible' Set ExpOpt = CreateStructExportOptions 'Limit the export only to the current page: Set ExpFlt = ActiveDocument.ExportEx("C:\Temp\2017\" & CurLy.Name & ".svg", cdrSVG, cdrCurrentPage, ExpOpt) ExpFlt.Finish 'Export Set LastLy = CurLy HasLastLy = True End IfNext CurLyIf LayerList = "" Then 'Nothing to doElseIf InStr(LayerList, ";") = 0 Then 'There had been only one layer prinable before CurPg.Layers(LayerList).Printable = TrueElse TempArr = Split(LayerList, ";") For i = LBound(TempArr) To UBound(TempArr) CurPg.Layers(TempArr(i)).Printable = True Next iEnd If 'Original settings of 'Printable' is restoredEnd Sub
This works not only for .SVG!