This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
By default file is saved or loaded in th same folder as the original file with the prefix _Layers.txt
CAD+
This macro is compatible with Toolbar+ and Batch+ tools so the buttons can be added to toolbar and assigned with shortcut for easier access or run in the batch mode.
In order to enable macro arguments set the ARGS constant to true
#Const ARGS = True
Path the path to text file to import or export as a macro argument.
Export
#Const ARGS = False'True to use arguments from Toolbar+ or Batch+ instead of the constantConst TOKEN_LAYER = "Layer: "Const TOKEN_DESCRIPTION = "Description: "Const TOKEN_COLOR = "Color: "Const TOKEN_PRINTABLE = "Printable: "Const TOKEN_STYLE = "Style: "Const TOKEN_VISIBLE = "Visible: "Const TOKEN_THICKNESS = "Thickness: "Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swDraw As SldWorks.DrawingDoc
Set swDraw = swApp.ActiveDoc
Dim filePath AsString#If ARGS ThenDim macroRunner AsObjectSet macroRunner = CreateObject("CadPlus.MacroRunner.Sw")
Dim param AsObjectSet param = macroRunner.PopParameter(swApp)
Dim vArgs AsVariant
vArgs = param.Get("Args")
filePath = CStr(vArgs(0))
#Else
filePath = swDraw.GetPathName
If filePath <> ""Then
filePath = Left(filePath, InStrRev(filePath, ".") - 1) & "_Layers.txt"Else
Err.Raise vbError, "", "If output file path is not specified file must be saved"EndIf#End IfIfNot swDraw IsNothingThen
ExportLayers swDraw, filePath
Else
Err.Raise vbError, "", "Open drawing"EndIfEndSubSub ExportLayers(draw As SldWorks.DrawingDoc, filePath AsString)
Dim swLayerMgr As SldWorks.LayerMgr
Set swLayerMgr = draw.GetLayerManager
Dim vLayers AsVariant
vLayers = swLayerMgr.GetLayerList
Dim fileNmb AsInteger
fileNmb = FreeFile
Open filePath For Output As #fileNmb
Dim i AsIntegerFor i = 0 To UBound(vLayers)
Dim layerName AsString
layerName = CStr(vLayers(i))
Dim swLayer As SldWorks.Layer
Set swLayer = swLayerMgr.GetLayer(layerName)
Dim RGBHex AsString
RGBHex = Right("000000" & Hex(swLayer.Color), 6)
Print #fileNmb, TOKEN_LAYER & swLayer.Name
Print #fileNmb, " " & TOKEN_DESCRIPTION & swLayer.Description
Print #fileNmb, " " & TOKEN_COLOR & CInt("&H" & Mid(RGBHex, 5, 2)) & " " & CInt("&H" & Mid(RGBHex, 3, 2)) & " " & CInt("&H" & Mid(RGBHex, 1, 2))
Print #fileNmb, " " & TOKEN_PRINTABLE & swLayer.Printable
Print #fileNmb, " " & TOKEN_STYLE & swLayer.Style
Print #fileNmb, " " & TOKEN_VISIBLE & swLayer.Visible
Print #fileNmb, " " & TOKEN_THICKNESS & swLayer.Width
Print #fileNmb, ""Next
Close #fileNmb
EndSub
Import
#Const ARGS = False'True to use arguments from Toolbar+ or Batch+ instead of the constantConst TOKEN_LAYER = "Layer: "Const TOKEN_DESCRIPTION = "Description: "Const TOKEN_COLOR = "Color: "Const TOKEN_PRINTABLE = "Printable: "Const TOKEN_STYLE = "Style: "Const TOKEN_VISIBLE = "Visible: "Const TOKEN_THICKNESS = "Thickness: "Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swDraw As SldWorks.DrawingDoc
Set swDraw = swApp.ActiveDoc
Dim filePath AsString#If ARGS ThenDim macroRunner AsObjectSet macroRunner = CreateObject("CadPlus.MacroRunner.Sw")
Dim param AsObjectSet param = macroRunner.PopParameter(swApp)
Dim vArgs AsVariant
vArgs = param.Get("Args")
filePath = CStr(vArgs(0))
#Else
filePath = swDraw.GetPathName
If filePath <> ""Then
filePath = Left(filePath, InStrRev(filePath, ".") - 1) & "_Layers.txt"Else
Err.Raise vbError, "", "If output file path is not specified file must be saved"EndIf#End IfIfNot swDraw IsNothingThen
ImportLayers swDraw, filePath
Else
Err.Raise vbError, "", "Open drawing"EndIfEndSubSub ImportLayers(draw As SldWorks.DrawingDoc, filePath AsString)
Dim swLayerMgr As SldWorks.LayerMgr
Set swLayerMgr = draw.GetLayerManager
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(filePath) ThenDim swCurrentLayer As SldWorks.Layer
Set file = fso.OpenTextFile(filePath)
DoUntil file.AtEndOfStream
Dim line AsString
line = file.ReadLine
Dim value AsStringIf IsToken(line, TOKEN_LAYER, value) ThenSet swCurrentLayer = swLayerMgr.GetLayer(value)
If swCurrentLayer IsNothingThen
swLayerMgr.AddLayer value, "", RGB(255, 255, 255), swLineStyles_e.swLineCENTER, swLineWeights_e.swLW_CUSTOM
Set swCurrentLayer = swLayerMgr.GetLayer(value)
EndIfIf swCurrentLayer IsNothingThen
Err.Raise vbError, "", "Failed to access layer " & value
EndIfElseIf swCurrentLayer IsNothingThen
Err.Raise vbError, "", "Current layer is not set"EndIfIf IsToken(line, TOKEN_DESCRIPTION, value) Then
swCurrentLayer.Description = value
ElseIf IsToken(line, TOKEN_COLOR, value) ThenDim vRgb AsVariant
vRgb = Split(value, " ")
swCurrentLayer.Color = RGB(CInt(Trim(CStr(vRgb(0)))), CInt(Trim(CStr(vRgb(1)))), CInt(Trim(CStr(vRgb(2)))))
ElseIf IsToken(line, TOKEN_PRINTABLE, value) Then
swCurrentLayer.Printable = CBool(value)
ElseIf IsToken(line, TOKEN_STYLE, value) Then
swCurrentLayer.Style = CInt(value)
ElseIf IsToken(line, TOKEN_VISIBLE, value) Then
swCurrentLayer.Visible = CBool(value)
ElseIf IsToken(line, TOKEN_THICKNESS, value) Then
swCurrentLayer.Width = CInt(value)
EndIfEndIfLoop
file.Close
Else
Err.Raise vbError, "", "File does not exist"EndIfEndSubFunction IsToken(txt AsString, token AsString, ByRef value AsString) AsBoolean
txt = Trim(txt)
If LCase(Left(txt, Len(token))) = LCase(token) Then
value = Trim(Right(txt, Len(txt) - Len(token)))
IsToken = TrueElse
value = ""
IsToken = FalseEndIfEndFunction