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
Cut list names for sheet metal bodies can be used to store important information, such as part number. This VBA macro allows to rename all flat pattern views of sheet metal in the active drawing sheet with the name of the respective cut-list item using SOLIDWORKS API.
Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swDraw As SldWorks.DrawingDoc
try:
OnErrorGoTocatchSet swDraw = swApp.ActiveDoc
IfNot swDraw IsNothingThen
RenameFlatPatternViews swDraw, swDraw.GetCurrentSheet
Else
Err.Raise vbError, "", "Please open drawing document"EndIfGoTofinallycatch:
MsgBox Err.Description & " (" & Err.Number & ")", vbCritical
finally:
EndSubSub RenameFlatPatternViews(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet)
Dim vViews AsVariant
vViews = GetSheetViews(draw, sheet)
IfNot IsEmpty(vViews) ThenDim i AsIntegerFor i = 0 To UBound(vViews)
Dim swView As SldWorks.view
Set swView = vViews(i)
If swView.IsFlatPatternView() Then
Debug.Print "Renaming " & swView.Name
Dim swBody As SldWorks.Body2
Set swBody = GetFlatPatternViewBody(swView)
Dim swCutListFeat As SldWorks.Feature
Dim activeConf AsString
activeConf = swView.ReferencedDocument.ConfigurationManager.ActiveConfiguration.Name
swView.ReferencedDocument.ShowConfiguration2 swView.ReferencedConfiguration
Set swCutListFeat = GetCutListFromBody(swView.ReferencedDocument, swBody)
swView.ReferencedDocument.ShowConfiguration2 activeConf
If swCutListFeat IsNothingThen
Err.Raise vbError, "", "Failed to find cut list for " & swView.Name
EndIf
swView.SetName2 swCutListFeat.Name
EndIfNextEndIfEndSubFunction GetFlatPatternViewBody(view As SldWorks.view) As SldWorks.Body2
Dim vVisComps AsVariant
vVisComps = view.GetVisibleComponents()
If IsEmpty(vVisComps) Then
Err.Raise vbError, "", view.Name & " doesn't have visible components"EndIfDim swComp As SldWorks.Component2
Set swComp = vVisComps(0)
Dim vFaces AsVariant
vFaces = view.GetVisibleEntities(swComp, swViewEntityType_e.swViewEntityType_Face)
If IsEmpty(vFaces) Then
Err.Raise vbError, "", view.Name & " doesn't have visible faces"EndIfDim swFace As SldWorks.Face2
Set swFace = vFaces(i)
Dim swBody As SldWorks.Body2
Set swBody = swFace.GetBody
Set GetFlatPatternViewBody = swBody
EndFunctionFunction GetCutListFromBody(model As SldWorks.ModelDoc2, body As SldWorks.Body2) As SldWorks.Feature
Dim swFeat As SldWorks.Feature
Dim swBodyFolder As SldWorks.BodyFolder
Set swFeat = model.FirstFeature
DoWhileNot swFeat IsNothingIf swFeat.GetTypeName2 = "CutListFolder"ThenSet swBodyFolder = swFeat.GetSpecificFeature2
Dim vBodies AsVariant
vBodies = swBodyFolder.GetBodies
Dim i AsIntegerIfNot IsEmpty(vBodies) ThenFor i = 0 To UBound(vBodies)
Dim swCutListBody As SldWorks.Body2
Set swCutListBody = vBodies(i)
If UCase(swCutListBody.Name) = UCase(body.Name) ThenSet GetCutListFromBody = swFeat
ExitFunctionEndIfNextEndIfEndIfSet swFeat = swFeat.GetNextFeature
LoopEndFunctionFunction GetSheetViews(draw As SldWorks.DrawingDoc, sheet As SldWorks.sheet) AsVariantDim vSheets AsVariant
vSheets = draw.GetViews()
Dim i AsIntegerFor i = 0 To UBound(vSheets)
Dim vViews AsVariant
vViews = vSheets(i)
Dim swSheetView As SldWorks.view
Set swSheetView = vViews(0)
If UCase(swSheetView.Name) = UCase(sheet.GetName()) ThenIf UBound(vViews) > 0 ThenDim swViews() As SldWorks.view
ReDim swViews(UBound(vViews) - 1)
Dim j AsIntegerFor j = 1 To UBound(vViews)
Set swViews(j - 1) = vViews(j)
Next
GetSheetViews = swViews
ExitFunctionEndIfEndIfNextEndFunction